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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 == "[email protected]") {
 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();