Uninstall Events for Firefox Toolbars
August 18th, 2009
No comments
Recently I came across the need to run a script during the request to uninstall a Firefox toolbar (specifically for metric keeping purposes). I could find almost zero documentation on this, so I’m posting a full example here. This is basically accomplished by registering your observer on the toolbar’s initialization and then observing for the requested action:
function InitToolbar()
{
// Register for uninstalls
myUninstallObserver.register();
}
var myUninstallObserver = {
_uninstall : false,
_tabs : null,
observe : function(subject, topic, data) {
if (topic == "em-action-requested")
{
// We flagged the extension to be uninstalled
subject.QueryInterface(Ci.nsIUpdateItem);
// The following id should match up to the id in the install.rdf file
if (subject.id == "toolbar@mattjwilson.com")
{
if (data == "item-uninstalled")
{
this._uninstall = true;
}
else
{
if (data == "item-cancel-action")
{
this._uninstall = false;
}
}
}
}
else
{
if (topic == "quit-application-granted")
{
// FireFox is shutting down
if (this._uninstall)
{
// - - - - - - - - - - - - - - - - - - - - -
//
// CODE TO RUN DURING UNINSTALLATION 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");
}
}
window.addEventListener("load", function() {InitToolbar()}, false);