2
我想在Firefox中獲取我的擴展的目錄。檢查Firefox中是否存在代碼模塊
在Firefox 3中它的工作原理是這樣的:
var file = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager)
.getInstallLocation("{my guid}");
在Firefox 4中,您需要使用新的附加組件管理器這樣的:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("{my guid}", function(addon) {
addon.getResourceURL("file name"));
});
因爲我希望我的擴展器能夠同時兼容Firefox 3.x和Firefox 4.x及更高版本,我如何檢查AddonManager.jsm是否可用?
在Firefox 3中,您也可以只執行'Application.extensions.get(guid).version'來檢查Application.extensions是否被定義,這是一個很好的方法。 – MatrixFrog 2011-06-10 17:23:57
但我該如何檢查它是否被定義? – 2011-06-13 14:23:22
undefined是一個falsy值,所以只是'if(Application.extensions){/ *代碼爲Firefox 3.6 * /}其他{/ *代碼爲Firefox 4 * /}' – MatrixFrog 2011-06-13 19:12:18