javascript - 从 greasemonkey 启动本地可执行文件

标签 javascript greasemonkey local-files

下面是我正在尝试的代码(加上一些变体),有一个对话框要求我的许可,但仍然出错

Error: Permission denied for to get property XPCComponents.classes

unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); 

var file = unsafeWindow.Components.classes["@mozilla.org/file/local;1"]
    .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("d:\\test.bat");

var process = unsafeWindow.Components.classes["@mozilla.org/process/util;1"]
    .createInstance(Components.interfaces.nsIProcess);
process.init(file);

var args = ["argument1", "argument2"];
process.run(false, args, args.length);

这是不可能的吗?

最佳答案

@Jano 的回答是正确的,但您仍然可以调用 .bat使用自定义协议(protocol)处理程序的文件,例如 myprotocol://parameters .也在这里解释:How to run local program (exe) via Chrome via HTML/javascript

将此 key 添加到您的注册表:

HKEY_CLASSES_ROOT
   myprotocol
      (Default) = "URL:Test Protocol"
      URL Protocol = ""
      shell
         open
            command
               (Default) = "d:\test.bat" "%1"

并在 .bat 中捕获参数:

set param=%1
echo Parameter is "%param:~13,100%

在哪里:~13,100裁剪参数的前 13 个字符 ( myprotocol:// )

然后在您的脚本中,只需在 window.location 上使用自定义协议(protocol) URL , $.ajax或分配给 <a>href .

关于javascript - 从 greasemonkey 启动本地可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5773313/

相关文章:

javascript - 如何访问 HTML 表中的 JS 变量

javascript - 有什么方法可以在不触发 Change 事件的情况下重新填充 Html Select 的选项(使用 jQuery)?

javascript - 使用 waitForKeyElements 用跨度包裹文本会破坏样式吗?

javascript - GreaseMonkey - 在同一选项卡中运行的两个脚本之间共享数据

json - 在Mac Catalyst中运行的iOS应用程序中的DropInteraction中接收JSON文件

javascript - 如何在IE9中使用ajax加载本地文件

ios - Nativescript + iOS webview + 本地文件

javascript - 使用 .after() 添加 html 关闭和打开标签

javascript - Typescript strictNullChecks 跨函数检查

javascript - Greasemonkey 改变 <span> 颜色