MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/d6d7ff2e2f9c

Add-ons

Listening for Load and Unload

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

Starting from Firefox 53, no new legacy add-ons will be accepted on addons.mozilla.org (AMO) for desktop Firefox and Firefox for Android.

Starting from Firefox 57, only extensions developed using WebExtensions APIs will be supported on Desktop Firefox and Firefox for Android.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to use WebExtensions APIs if they can. See the "Compatibility Milestones" document for more information.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

To follow this tutorial you'll need to have learned the basics of jpm.

If your add-on exports a function called main(), then that function will be called whenever the add-on is loaded, and it will be passed an object containing a string describing the reason it was loaded as well as any arguments passed to it. If your add-on exports a function called onUnload(), then that function will be called when the add-on is unloaded, and it will be passed a string describing the reason it was unloaded.

You don't have to use exports.main() or exports.onUnload(). You can just place your add-on's code at the top level instead of wrapping it in a function assigned to exports.main(). It will be loaded in the same circumstances, but you won't get access to the load/unload reason or arguments.

exports.main()

Your add-on's main.js code is executed as soon as it is loaded. It is loaded when it is installed, when it is enabled, or when Firefox starts.

If your add-on exports a function called main(), that function will be called immediately after the overall main.js is evaluated, and after all top-level require() statements have run (so generally after all dependent modules have been loaded).

exports.main = function (options, callbacks) {};

options is an object describing the parameters with which your add-on was loaded.

options.loadReason

options.loadReason is one of the following strings describing the reason your add-on was loaded:

install
enable
startup
upgrade
downgrade

exports.onUnload()

If your add-on exports a function called onUnload(), that function will be called when the add-on is unloaded.

exports.onUnload = function (reason) {};

reason

reason is one of the following strings describing the reason your add-on was unloaded:

uninstall
disable
shutdown
upgrade
downgrade

If your add-on is disabled, then uninstalled, your onUnload listener will only be called once, with the disable reason. This happens because disabling an add-on also disables its onUnload listeners, so there is no listener remaining when the add-on is uninstalled. For more, see bug 627432, in particular comment 12 on that bug.

Document Tags and Contributors

 Contributors to this page: wbamberg, _6a68, sumaiya, nearwood, Canuckistani
 Last updated by: wbamberg,