gpt4 book ai didi

javascript - 从 Firefox Addon 执行 ShellExecute

转载 作者:行者123 更新时间:2023-11-28 19:40:35 24 4
gpt4 key购买 nike

在我的 Firefox 扩展中,我想使用 Windows 中该文件类型的“默认查看器”打开某些文件。所以基本上类似于 ShellExecute('OPEN') Windows API 的函数调用。是否可以?如果可以,如何实现?

最佳答案

文件

最接近的是nsIFile::launch 。但是,它并未针对每个可以想象的平台实现(但至少针对 Windows、OSX、GTK/Gnome 及其兼容、KDE ​​和 Android 实现)。

但是,您不能使用 ::launch 来指示操作系统(特别是 Windows)使用 open 以外的动词,因此没有等同于例如ShellExecute(..., "编辑", ...).

以下是有关如何使用它的示例:

try {
var file = Services.dirsvc.get("Desk", Ci.nsIFile);
file.append("screenshot.png");
file.launch();
}
catch (ex) {
// Failed to launch because e.g. the OS returned an error
// or the file does not exist,
// or this function is simply not implemented for a particular platform.
}

您当然也可以从“原始”路径创建一个 nsIFile 实例,例如(我在 OSX 上):

var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);

CcCi 是大多数 Mozilla 和 Components.classesComponents.interfaces 的快捷方式附加组件代码使用。在附加 SDK 中,您可以通过 Chrome Authority 获取这些内容.

URI

编辑我完全忘记了ShellExcute也会处理URL。顺便说一句,您确实只询问了“文件类型”。

无论如何,要打开随机 URI,您可以使用 nsIExternalProtocolService

选项 1 - 使用默认处理程序(不一定是操作系统处理程序)启动

要使用默认处理程序(也可以是 Web 协议(protocol)处理程序或类似处理程序)启动,您可以使用以下代码。请注意,当用户尚未选择协议(protocol)的默认值时,这可能会显示“选择应用程序”对话框。

var uri = Services.io.newURI("https://google.com/", null, null);
var eps = Cc["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Ci.nsIExternalProtocolService);
// You're allowed to omit the second parameter if you don't have a window.
eps.loadURI(uri, window);

选项 2 - 使用操作系统默认处理程序启动(如果有)

如果 Firefox 可以找到特定协议(protocol)的操作系统默认处理程序,那么代码将启动该默认处理程序无需用户交互,这意味着您应该格外小心,不要启动可能造成损害的任意 URI (例如 vbscript:...)!

var uri = Services.io.newURI("https://google.com/", null, null);
var eps = Cc["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Ci.nsIExternalProtocolService);
var found = {};
var handler = eps.getProtocolHandlerInfoFromOS(uri.scheme, found);
if (found.value && handler && handler.hasDefaultHandler) {
handler.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
// You're allowed to omit the second parameter if you don't have a window.
handler.launchWithURI(uri, window);
}

关于javascript - 从 Firefox Addon 执行 ShellExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25138536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com