How to run a clean-up script when your extension is uninstalled
Right now there is no support in the Firefox Extension Manager for running a script when the extension is uninstalled. Such script might be useful if your extension needs to perform some clean up, like removing some temporary files or registry entries.
I’ve managed to find a work around for this:
- Install an
unload
event handler in your overlay Javascript:
addEventListener("unload", overlayUnloaded, false);
TheoverlayUnloaded
function will be called each time the user closes the browser window. - In the
overlayUnloaded
function, ask the extension manager (nsIExtensionManager
) whether your extension is intoBeUninstalled
state. - If so, the user has uninstalled your extension – go ahead and run your clean-up code.
A related technique is determining that your extension has been updated, and something went wrong.
I can provide the complete source code (in Javascript) for this technique on request.
August 20th, 2004 at 2:47 pm
Awesome, I woul definitally like this for my QuickNOte/BBCode and WebMailCompose extensions, as the firefox uninstaller doesn’t get rid of prefs that I want removed.
Thanks once again alex.
December 1st, 2004 at 2:48 pm
Thanks too. I was looking for a post uninstall extension script or event but soon realized it didn’t exist yet. I would appreciate seeing code on using nsIExtensionManager for this purpose. Let’s hope an onuninstall event is added to make handling easier.
May 5th, 2005 at 5:24 pm
Could you send me your example “uninstall” extension hook code?
May 5th, 2005 at 6:53 pm
The code for this technique can be found in my FirefoxView extension.
June 6th, 2005 at 5:08 am
I’m not any big programmer or anything but sometimes when i uninstall an extension from firefox and i check my profile folder i still see folders containing scripts n other stuff from the uninstalled extensions.. I’m not sure if this is supposed to be like tht, but if the solution u stated above can clean up those unnecesary files then yes it is a good idea that should be followed up
July 12th, 2005 at 11:07 am
Great idea,
I was looking for this functionality and found ‘Adding entries to the uninstall log’ on MozillaZine. Unfortunately that method needs to add each file separately to the Uninstall file in the extension directory. In my case I save a lot of web pages (implementing my own cache) and it would be too much files to add to the list. It seems to be much easier to delete my cache’s root directory recursively from a script on uninstall.
Thanks a lot.
August 9th, 2005 at 3:23 am
Nice concept, but I am a little dumb here. I am a newbie, could you give me complete step by step to do this clean up with the code? I really appreciate your help.
August 16th, 2005 at 12:47 am
nice… at the moment i have a strong interest in something that works from the other direction — is it possible to run code on installation that can clean out an old version of your extension? (if a change of GUIDs becomes necessary, for instance)