joiky
小狐狸
小狐狸
  • UID46412
  • 注册日期2014-06-19
  • 最后登录2014-09-21
  • 发帖数4
  • 经验5枚
  • 威望0点
  • 贡献值4点
  • 好评度0点
阅读:2362回复:9

请问firefox执行应用程序后如何获取应用的输出?

楼主#
更多 发布于:2014-07-18 21:19
执行应用后,怎么获取应用的输出呢(不是exitValue)?
 
exec: function (bin, args, func) {
    try {
        var file=Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
        var process = Components.classes["@mozilla.org/process/util;1"]
            .createInstance(Components.interfaces.nsIProcess);  
            
        file.initWithPath(bin);
        process.init(file);  
        
        if (typeof args === "undefined")
            args = [];
            
        alert(bin + "" + args.join("\n"));
        if (typeof func === "function")
            process.runAsync(args, args.length, {
                observe: function(subject, topic, data) {   // data 是null
                    alert("xxx" + data);
                    process = subject.QueryInterface(Components.interfaces.nsIProcess);
                    func(topic == "process-finished", process.exitValue, data);
                }  
            });
        else
            process.run(false, args, args.length);
    } catch (e) {
        alert("exec error: " + e);
    }  
}
aaaa007cn
千年狐狸
千年狐狸
  • UID23968
  • 注册日期2008-05-03
  • 最后登录2022-03-07
  • 发帖数1924
  • 经验1138枚
  • 威望1点
  • 贡献值232点
  • 好评度164点
1楼#
发布于:2014-07-19 09:12
hzzhaiqi
火狐狸
火狐狸
  • UID30112
  • 注册日期2009-08-18
  • 最后登录2015-09-15
  • 发帖数279
  • 经验180枚
  • 威望1点
  • 贡献值44点
  • 好评度28点
2楼#
发布于:2014-07-19 09:50
采用 console.log 直接输出也没找到
console.log(subject, topic, data)
joiky
小狐狸
小狐狸
  • UID46412
  • 注册日期2014-06-19
  • 最后登录2014-09-21
  • 发帖数4
  • 经验5枚
  • 威望0点
  • 贡献值4点
  • 好评度0点
3楼#
发布于:2014-07-20 17:22
aaaa007cn:获取 stdout、stderr 的输出?
搜了下
http://stackoverflow.com/questions/7325750/mozilla-extension-reading-stdout-of-external-proces...
回到原帖
实现没找到好办法,难道还真只有重定向?头疼啊。。
joiky
小狐狸
小狐狸
  • UID46412
  • 注册日期2014-06-19
  • 最后登录2014-09-21
  • 发帖数4
  • 经验5枚
  • 威望0点
  • 贡献值4点
  • 好评度0点
4楼#
发布于:2014-07-20 17:23
hzzhaiqi:采用 console.log 直接输出也没找到
console.log(subject, topic, data)
回到原帖
嗯,数据根本没获取出来。
aaaa007cn
千年狐狸
千年狐狸
  • UID23968
  • 注册日期2008-05-03
  • 最后登录2022-03-07
  • 发帖数1924
  • 经验1138枚
  • 威望1点
  • 贡献值232点
  • 好评度164点
5楼#
发布于:2014-07-20 21:10
joiky:实现没找到好办法,难道还真只有重定向?头疼啊。。回到原帖
从那两个链接来看
重定向是最简单的 workaround 了
hzzhaiqi
火狐狸
火狐狸
  • UID30112
  • 注册日期2009-08-18
  • 最后登录2015-09-15
  • 发帖数279
  • 经验180枚
  • 威望1点
  • 贡献值44点
  • 好评度28点
6楼#
发布于:2014-07-20 21:18
joiky:实现没找到好办法,难道还真只有重定向?头疼啊。。回到原帖
应该有其它方法的。我用 pentadactyl 可以获取到 cmd 的输出信息。

这是 pentadactyl 下的实现方法,供你参考下
// TODO: when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is
// fixed use that instead of a tmpfile
/**
 * Runs *command* in a subshell and returns the output. The shell used is
 * that specified by the 'shell' option.
 *
 * @param {string|[string]} command The command to run. This can be a shell
 *      command string or an array of strings (a command and arguments)
 *      which will be escaped and concatenated.
 * @param {string} input Any input to be provided to the command on stdin.
 * @param {function(object)} callback A callback to be called when
 *      the command completes. @optional
 * @returns {object|null}
 */
system: function system(command, input, callback) {
    util.dactyl.echomsg(_("io.callingShell", command), 4);
 
    let { shellEscape } = util.closure;
 
    return this.withTempFiles(function (stdin, stdout, cmd) {
        if (input instanceof File)
            stdin = input;
        else if (input)
            stdin.write(input);
 
        function result(status, output) ({
            __noSuchMethod__: function (meth, args) this.output[meth].apply(this.output, args),
            valueOf: function () this.output,
            output: output.replace(/^(.*)\n$/, "$1"),
            returnValue: status,
            toString: function () this.output
        });
 
        function async(status) {
            let output = stdout.read();
            for (let f of [stdin, stdout, cmd])
                if (f.exists())
                    f.remove(false);
            callback(result(status, output));
        }
 
        let shell = io.pathSearch(storage["options"].get("shell").value);
        let shcf = storage["options"].get("shellcmdflag").value;
        util.assert(shell, _("error.invalid", "'shell'"));
 
        if (isArray(command))
            command = command.map(shellEscape).join(" ");
 
        // TODO: implement 'shellredir'
        if (config.OS.isWindows && !/sh/.test(shell.leafName)) {
            command = "cd /D " + this.cwd.path + " && " + command + " > " + stdout.path + " 2>&1" + " < " + stdin.path;
            var res = this.run(shell, shcf.split(/\s+/).concat(command), callback ? async : true);
        }
        else {
            cmd.write("cd " + shellEscape(this.cwd.path) + "\n" +
                    ["exec", ">" + shellEscape(stdout.path), "2>&1", "<" + shellEscape(stdin.path),
                     shellEscape(shell.path), shcf, shellEscape(command)].join(" "));
            res = this.run("/bin/sh", ["-e", cmd.path], callback ? async : true);
        }
 
        return callback ? true : result(res, stdout.read());
    }, this, true);
},
 
/**
 * Creates a temporary file context for executing external commands.
 * *func* is called with a temp file, created with {@link #createTempFile},
 * for each explicit argument. Ensures that all files are removed when
 * *func* returns.
 *
 * @param {function} func The function to execute.
 * @param {Object} self The 'this' object used when executing func.
 * @returns {boolean} false if temp files couldn't be created,
 *     otherwise, the return value of *func*.
 */
withTempFiles: function withTempFiles(func, self, checked, ext, label) {
    let args = array(util.range(0, func.length))
                .map(bind("createTempFile", this, ext, label)).array;
    try {
        if (!args.every(util.identity))
            return false;
        var res = func.apply(self || this, args);
    }
    finally {
        if (!checked || res !== true)
            args.forEach(f => { f.remove(false); });
    }
    return res;
},

/**
  * Runs an external program.
  *
  * @param {File|string} program The program to run.
  * @param {[string]} args An array of arguments to pass to *program*.
  */
 run: function run(program, args, blocking, self) {
     args = args || [];
 
     let file = this.pathSearch(program);
 
     if (!file || !file.exists()) {
         util.dactyl.echoerr(_("io.noCommand", program));
         if (callable(blocking))
             util.trapErrors(blocking);
         return -1;
     }
 
     let process = services.Process(file.file);
     process.run(false, args.map(String), args.length);
 
     let deferred = Promise.defer();
 
     if (callable(blocking))
         // Deprecated.
         deferred.promise.then(blocking);
     else if (blocking) {
         // Deprecated?
         while (process.isRunning)
             util.threadYield(false, true);
         return process.exitValue;
     }
 
     let timer = services.Timer(
         function () {
             if (!process.isRunning) {
                 timer.cancel();
                 deferred.resolve(process.exitValue);
             }
         },
         100, services.Timer.TYPE_REPEATING_SLACK);
 
     return deferred.promise;
 },
aaaa007cn
千年狐狸
千年狐狸
  • UID23968
  • 注册日期2008-05-03
  • 最后登录2022-03-07
  • 发帖数1924
  • 经验1138枚
  • 威望1点
  • 贡献值232点
  • 好评度164点
7楼#
发布于:2014-07-20 22:45
这就是重定向啊
// TODO: implement 'shellredir'
从这里开始
hzzhaiqi
火狐狸
火狐狸
  • UID30112
  • 注册日期2009-08-18
  • 最后登录2015-09-15
  • 发帖数279
  • 经验180枚
  • 威望1点
  • 贡献值44点
  • 好评度28点
8楼#
发布于:2014-07-21 00:02
aaaa007cn:这就是重定向啊
// TODO: implement 'shellredir'
从这里开始
回到原帖


好吧,算是明白了  shell 重定向 的意思。
joiky
小狐狸
小狐狸
  • UID46412
  • 注册日期2014-06-19
  • 最后登录2014-09-21
  • 发帖数4
  • 经验5枚
  • 威望0点
  • 贡献值4点
  • 好评度0点
9楼#
发布于:2014-07-21 15:02
hzzhaiqi:好吧,算是明白了  shell 重定向 的意思。回到原帖
的确是写文件,不过还是很感谢,参考一下。
游客

返回顶部