![](/img/trans.png)
[英]Firefox Restartless Addon: What information are available to the addon from AddonManager?
[英]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);
Cc
和Ci
是大多數mozilla和附加組件代碼使用的Components.classes
和Components.interfaces
快捷方式。 在附加SDK中,您可以通過Chrome Authority獲得這些。
編輯我完全忘記ShellExcute
也將處理URL。 而且您只詢問了“文件類型”,順便說一句。
無論如何,要打開隨機URI,可以使用nsIExternalProtocolService
。
要使用默認處理程序(也可以是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);
如果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.