Ryan
Ryan

Reputation: 10049

firefox extension puzzle

This is something that is puzzling the heck out of me, my code:

        var this_version=null;              
        try 

    {


// Firefox 4 and later; Mozilla 2 and later
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID("[email protected]", function(addon) {
           // alert("My extension's version is r" + addon.version);
           this_version = addon.version;
      });
    }
    catch (ex) {
        // Firefox 3.6 and before; Mozilla 1.9.2 and before
        var em = Components.classes["@mozilla.org/extensions/manager;1"]
                 .getService(Components.interfaces.nsIExtensionManager);
        var addon = em.getItemForID("[email protected]");
        //alert("My extension's version is rr" + addon.version);
       this_version = addon.version;
    }
    alert("aa:"+this_version);

    /* #### End ### */


            if (prefManager.getCharPref("extensions.mafiaafire.quickfilter_ver") != this_version) 
                {



                    //prefManager.setCharPref("extensions.mafiaafire.quickfilter_ver",this.version);
                    alert("bb:"+this_version);
                    //gBrowser.selectedTab = gBrowser.addTab(prefManager.getCharPref("extensions.mafiaafire.quickfilter_ver_change_url"));    
                }

I am on FF 4, and the first alert is always giving me null and the second alert the correct version.

But if I take out the first alert, then the second alert is giving me null!!!!

It just does not make any sense.

Upvotes: 1

Views: 177

Answers (1)

Tyler
Tyler

Reputation: 22116

The key is that the Firefox 4 code is asynchronous.

AddonManager.getAddonByID("[email protected]", function(addon) {
  // alert("My extension's version is r" + addon.version);
  this_version = addon.version;
});

means, go get the version of this addon, and when you get it, let me know by calling this function here. When you get to the first alert, the addon manager hasn't gotten it yet. Since it takes a second or so before you click OK on the alert, it does have it by the time the second alert comes.

Upvotes: 4

Related Questions