繁体   English   中英

从Firefox插件中执行ShellExecute

[英]Perfom a ShellExecute from Firefox Addon

在Firefox扩展程序中,我想使用Windows中该文件类型的“默认查看器”打开某些文件。 因此,基本上类似于Windows API的ShellExecute('OPEN')函数调用。 可能吗? 如果是这样,那将如何实现?

档案

最接近的是nsIFile::launch 但是,并非所有可能的平台都实现了该功能(但至少在Windows,OSX,GTK / Gnome和兼容的KDE和Android上实现了该功能)。

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

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

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获得这些。

URIs

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

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

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

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

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可以找到特定协议的操作系统默认处理程序,则代码将在没有用户交互的情况下启动该默认处理程序,这意味着您应格外小心,不要启动可能有害的任意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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM