Browse by Month RSS Feed
  • September 2010
  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010

  • Filed under: Code, JavaScript @ 10:57 pm

    Apparently Firefox 3.7 will be providing a substantially better way to attach code to key events in an extension's life (like installation, uninstallation, update etc), but in the meantime we're stuck with manually attaching our code to observers that detect those events. A little cumbersome, but workable.

    Anyway, this is taken from this page on songbirdnest.com. I just made a couple of minor tweaks needed to their code: it looks like they just copied and pasted it from elsewhere, so a couple of vars - Ci and Cc - weren't defined. The following code works as a standalone component.

    Read their page for some caveats and other comments.

    var myUninstallObserver = {
        _uninstall : false,
        _tabs : null,
        Ci: Components.interfaces,
        Cc: Components.classes,

        observe : function(subject, topic, data) {
            if (topic == "em-action-requested") {
                // extension has been flagged to be uninstalled
                subject.QueryInterface(Ci.nsIUpdateItem);
                if (subject.id == "yourID@whatever.com") {
                    if (data == "item-uninstalled") {
                        this._uninstall = true;
                    } else if (data == "item-cancel-action") {
                        this._uninstall = false;
                    }
                }
            } else if (topic == "quit-application-granted") {
                // we're shutting down, so check to see if we were flagged for uninstall - if we were, then cleanup here
                if (this._uninstall) {
                    // code here!
                }
                this.unregister();
            }
        },

        register: function() {
            var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
            observerService.addObserver(this, "em-action-requested", false);
            observerService.addObserver(this, "quit-application-granted", false);
        },
       
        unregister : function() {
            var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
            observerService.removeObserver(this, "em-action-requested");
            observerService.removeObserver(this, "quit-application-granted");
        }
    }

    myUninstallObserver.register();

    Comments (0)

    No Comments »

    No comments yet.

    RSS feed for comments on this post. TrackBack URI

    Leave a comment