MozillaZine

Check if a (Firefox 3.5) extension is enabled/disabled

Talk about add-ons and extension development.
stimpy84

User avatar
 
Posts: 15
Joined: November 29th, 2010, 7:16 am
Location: Utrecht, The Netherlands

Post Posted November 30th, 2010, 3:52 pm

Hi fellow extension developers! I'm working on an extension that has a few conflicting extensions out there. 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:

Code: Select all
function disableExtension(id) {
    var man = Components.classes["@mozilla.org/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:

Code: Select all
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?

ake79
 
Posts: 1335
Joined: February 17th, 2007, 6:05 am

Post Posted November 30th, 2010, 11:35 pm

My extensions: Save File to | ThumbsDown

stimpy84

User avatar
 
Posts: 15
Joined: November 29th, 2010, 7:16 am
Location: Utrecht, The Netherlands

Post Posted December 1st, 2010, 1:50 pm

Thanks for pointing me in the right direction. I had taken a look at the documentation, but had evidently overlooked the "enabled" attribute

I have now fixed it by replacing the if block containing the TODO with:

Code: Select all
    if (ext) {
        if (ext.enabled) disableExtension(id);
    }


The follow-up question is, of course, how to do this in Fx 1.* and 2.*. As extIApplication was introduced in Fx 3, I get "Application is not defined" in Firefox 2. I'm having a hard time finding documentation going that far back.

ake79
 
Posts: 1335
Joined: February 17th, 2007, 6:05 am

Post Posted December 1st, 2010, 2:15 pm

Are you seriously trying to support ff2 and older versions? Even ff3.0 is not supported anymore.

But if you are:

http://www.oxymoronical.com/experiments ... ionManager. Note version selection at the top of the page.

viewtopic.php?f=19&t=631306

http://mxr.mozilla.org/
My extensions: Save File to | ThumbsDown

stimpy84

User avatar
 
Posts: 15
Joined: November 29th, 2010, 7:16 am
Location: Utrecht, The Netherlands

Post Posted December 1st, 2010, 5:03 pm

I have noticed that 5% of my users are Fx 2.0 and older, so yes, I want to maintain compatibility for as long as there's a demand for it.

Thanks so much, this answers my question completely! I now have code that works for 2.0-4.0 (I haven't tried 1.5 and older):

Code: Select all
var ext;
if (typeof Application != 'undefined') {
    if (Application.extensions) {
        // Gecko 1.9.2 and older
        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.org/extensions/manager;1"].getService(Ci.nsIExtensionManager);
    if (extMgr) {
        ext = extMgr.getItemForID(id);
    }
    var extMgrDs = extMgr.datasource;
    if (extMgrDs) {
        var rdfSvc = Cc["@mozilla.org/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.org/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:

Code: Select all
disableExtension: function(id) {
    var man = Components.classes["@mozilla.org/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:

Code: Select all
restart: function() {
    var appStartup = Components.classes["@mozilla.org/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();
    }
}


See also my (updated) answer on StackOverflow. Thanks again, Atte!

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 3 guests