如何从Firefox的插件执行Windows命令? [英] How to execute a windows command from firefox addon?

查看:153
本文介绍了如何从Firefox的插件执行Windows命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何执行一个Windows命令,并使用Firefox插件显示它的输出?

例如:
ping www.stackoverfow.com

我只是想探索更多在firefox插件开发中,通过执行二进制文件(或)可执行文件打包在一起,或者运行一个windows命令。

nsIProcess 。在你的情况下,事情变得更复杂,因为你不知道你想运行哪个应用程序 - 它通常是 c:\ windows \ system32 \ ping.exe 但是你不能确定。如果你不想自己解析 PATH 环境变量,你可以使命令行shell为你做:

Components.utils.import(resource://gre/modules/FileUtils.jsm);

var env = Components.classes [@ mozilla.org/process/environment;1]
.getService(Components.interfaces.nsIEnvironment);
var shell = new FileUtils.File(env.get(COMSPEC));
var args = [/ c,ping stackoverflow.org];

var process = Components.classes [@ mozilla.org/process/util;1]
.createInstance(Components.interfaces.nsIProcess);
process.init(shell);
process.runAsync(args,args.length);

供参考: COMSPEC 环境变量 nsIEnvironment

请注意,您无法从流程中收回数据,只能在完成时得到通知,并了解是否失败。如果你想得到命令的输出,你将不得不将输出重定向到一个文件(运行 ping stackoverflow.org> c:\\temp\\foo.txt 通过shell命令),然后读出这个文件。


How to execute a windows command and display it's output using firefox addon?

For example: ping www.stackoverfow.com

I am just trying to explore more in firefox addon development by executing a binary file (or) executable packaged together or else running a windows command.

解决方案

You would use nsIProcess for that. In your case things are made more complicated because you don't know which application you want to run - it will usually be c:\windows\system32\ping.exe but you cannot be sure. If you don't want to parse the PATH environment variable yourself you can make the command line shell do it for you:

Components.utils.import("resource://gre/modules/FileUtils.jsm");

var env = Components.classes["@mozilla.org/process/environment;1"]
                    .getService(Components.interfaces.nsIEnvironment);
var shell = new FileUtils.File(env.get("COMSPEC"));
var args = ["/c", "ping stackoverflow.org"];

var process = Components.classes["@mozilla.org/process/util;1"]
                        .createInstance(Components.interfaces.nsIProcess);
process.init(shell);
process.runAsync(args, args.length);

For reference: COMSPEC environment variable, nsIEnvironment.

Note that you cannot receive data back from the process, you can merely get notified when it finishes and learn whether it failed. If you want to get the output of the command you will have to redirect the output to a file (run ping stackoverflow.org > c:\\temp\\foo.txt command via shell) and read out that file afterwards.

这篇关于如何从Firefox的插件执行Windows命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆