开发者

Check if code module exists in Firefox

开发者 https://www.devze.com 2023-03-12 05:28 出处:网络
I want to get the directory of my extension in Firefox. In Firefox 3 it works like this: var 开发者_StackOverflow社区file = Components.classes[\"@mozilla.org/extensions/manager;1\"]

I want to get the directory of my extension in Firefox.

In Firefox 3 it works like this:

var 开发者_StackOverflow社区file = Components.classes["@mozilla.org/extensions/manager;1"]
    .getService(Components.interfaces.nsIExtensionManager)
    .getInstallLocation("{my guid}");

In Firefox 4 you need to use the new Addon Manager like this:

Components.utils.import("resource://gre/modules/AddonManager.jsm");

AddonManager.getAddonByID("{my guid}", function(addon) {
    addon.getResourceURL("file name"));
});

Since I want my extension to be compatible with both Firefox 3.x and Firefox 4.x and above, how can I check if the AddonManager.jsm is available?


One way is to do

if (Application.extensions) {
  // code for Firefox 3.6
} else {
  // code for Firefox 4+
}

but it's a bit weird since the Firefox 4 API is asynchronous and the Firefox 3 API is synchronous.

0

精彩评论

暂无评论...
验证码 换一张
取 消