javascript - Check if a Firefox 3.5 add-on is enabled - Stack Overflow

In order inform the user of possible conflicts, I'd like to have my add-on check if another add-on

In order inform the user of possible conflicts, I'd like to have my add-on check if another add-on is installed and enabled. If so, I can disable either it or my own at the user's bequest:

function disableExtension(id) {
    var man = Components.classes["@mozilla/extensions/manager;1"];
    if (man) {
        man = man.getService(Components.interfaces.nsIExtensionManager);
    }
    if (man) {
        man.disableItem(id);
    } else {
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID(id, function(addon) {
            addon.userDisabled = true;
        });
    }
}

But I first, of course have to check if a certain other add-on is installed. Presently, I do this as follows:

if (Application.extensions) {
    // Gecko 1.9.2 and older
    ext = Application.extensions.get(id);
    if (ext) {
        // TODO check if extension is also enabled
        disableExtension(id);
    }
} else {
    // Gecko 2.0.0 and newer
    Application.getExtensions(function(extensions) {
        ext = extensions.get(id);
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID(id, function(addon) {
            if (!addon.userDisabled) {
                disableExtension(id);
            }
        });
    })
}

The code for Firefox 4 (the else-statement) works fine. For older versions of Firefox (3.5 and older), I can't for the life of me figure out how to determine if the extension is in fact installed.

Does anybody know how to do this?

In order inform the user of possible conflicts, I'd like to have my add-on check if another add-on is installed and enabled. If so, I can disable either it or my own at the user's bequest:

function disableExtension(id) {
    var man = Components.classes["@mozilla/extensions/manager;1"];
    if (man) {
        man = man.getService(Components.interfaces.nsIExtensionManager);
    }
    if (man) {
        man.disableItem(id);
    } else {
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID(id, function(addon) {
            addon.userDisabled = true;
        });
    }
}

But I first, of course have to check if a certain other add-on is installed. Presently, I do this as follows:

if (Application.extensions) {
    // Gecko 1.9.2 and older
    ext = Application.extensions.get(id);
    if (ext) {
        // TODO check if extension is also enabled
        disableExtension(id);
    }
} else {
    // Gecko 2.0.0 and newer
    Application.getExtensions(function(extensions) {
        ext = extensions.get(id);
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID(id, function(addon) {
            if (!addon.userDisabled) {
                disableExtension(id);
            }
        });
    })
}

The code for Firefox 4 (the else-statement) works fine. For older versions of Firefox (3.5 and older), I can't for the life of me figure out how to determine if the extension is in fact installed.

Does anybody know how to do this?

Share Improve this question edited Nov 30, 2010 at 22:47 Paul Lammertsma asked Nov 28, 2010 at 3:24 Paul LammertsmaPaul Lammertsma 38.3k17 gold badges137 silver badges189 bronze badges 1
  • The only thing that es to mind is running through extensions.ini and looking for the extension's getInstallLocation(), but I don't like that approach at all. – Paul Lammertsma Commented Nov 28, 2010 at 3:29
Add a ment  | 

2 Answers 2

Reset to default 5

The answer was trivial; I had overlooked the enabled attribute for extIExtension.

I resolved it as follows:

var ext;
if (typeof Application != 'undefined') {
    if (Application.extensions) {
        // Gecko 1.9.0 - 1.9.2
        ext = Application.extensions.get(id);
        if (ext) {
            if (ext.enabled) disableExtension(id);
        }
    } else {
        // Gecko 2.0.0 and newer
        Application.getExtensions(function(extensions) {
            ext = extensions.get(id);
            Components.utils.import("resource://gre/modules/AddonManager.jsm");
            AddonManager.getAddonByID(id, function(addon) {
                if (!addon.userDisabled) {
                    disableExtension(id);
                }
            });
        })
    }
} else {
    // Gecko 1.8.0
    var extMgr = Cc["@mozilla/extensions/manager;1"].getService(Ci.nsIExtensionManager);
    if (extMgr) {
        ext = extMgr.getItemForID(id);
    }
    var extMgrDs = extMgr.datasource;
    if (extMgrDs) {
        var rdfSvc = Cc["@mozilla/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
        if (rdfSvc && ext) {
            var source = rdfSvc.GetResource("urn:mozilla:item:" + ext.id);
            var property = rdfSvc.GetResource("http://www.mozilla/2004/em-rdf#isDisabled");
            var target = rdfSvc.GetLiteral("true");
            var disabled = extMgrDs.HasAssertion(source, property, target, true);
            if (!disabled) {
                disableExtension(id);
            }
        }
    } else if (typeof className != "undefined") {
        // Opens the add-on window
        BrowserOpenAddonsMgr();
    }
}

Where disableExtension() is:

disableExtension: function(id) {
    var man = Components.classes["@mozilla/extensions/manager;1"];
    if (man) {
        man = man.getService(Components.interfaces.nsIExtensionManager);
    }
    if (man) {
        man.disableItem(id);
        restart();
    } else {
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID(id, function(addon) {
            addon.userDisabled = true;
            restart();
        });
    }
}

And restart() is:

restart: function() {
    var appStartup = Components.classes["@mozilla/toolkit/app-startup;1"];
    if (appStartup) {
        appStartup = appStartup.getService(Components.interfaces.nsIAppStartup);
    }
    if (appStartup) {
        appStartup.quit(appStartup.eAttemptQuit | appStartup.eRestart);
    } else if (typeof Application != 'undefined') {
        if (Application.restart) Application.restart();
    }
}

This hasn't been tested on Firefox 1.0-1.5, but works on:

  • Firefox 4.0b7
  • Firefox 3.6
  • Firefox 3.5
  • Firefox 3.0
  • Firefox 2.0

Credit for helping me out goes to Atte Kemppilä.

Very informative write-up.
Note that you can use the "enabled" attribute for Gecko 2 as well. This makes the code somewhat simpler.

   if (Application.extensions) {
        // Gecko 1.9.0 - 1.9.2
        ext = Application.extensions.get(id);
        if (ext) {
            if (ext.enabled) disableExtension(id);
        }
    } else {
        // Gecko 2.0.0 and newer
        Application.getExtensions(function(extensions) {
            ext = extensions.get(id);
            if (ext.enabled) disableExtension(id);
        });
    }

For best practice you can of course move the repeated code to an external function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744899920a4599916.html

相关推荐

  • javascript - Check if a Firefox 3.5 add-on is enabled - Stack Overflow

    In order inform the user of possible conflicts, I'd like to have my add-on check if another add-on

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信