25 January 2011 - 0:39Restartless add-on example code
mfinkle’s recent posts [starkravingfinkle.org] on restartless add-ons reminded me that I was going to write a more detailed post about my restartless GitHub repository, but I’ve been distracted with another project.
So briefly, I’ve put together some helper functions [github.com] that have been useful for developing various restartless add-ons. I’ve organized the repository to have a bunch of “examples/” branches that build on top of other named branches that implement helper functions.
These first few are very simple examples:
- examples/autoReload: saves a step when developing a restartless addon by automatically re-enabling when you disable the add-on from about:addons
- examples/ensureEnabled: the install method always gets executed even when disabled, so this code makes sure the add-on runs on install; useful when combined with the following..
- examples/oneTime: run once and disable itself; useful for debugging problems on other people’s Firefox without doing the usual “copy/paste this into the Error Console”
The next couple helper functions have been incredibly useful for the restartless add-ons I’ve written:
- unload: add a callback to run when the add-on is disabled by calling unload(callback). This function makes it easy to ensure Firefox returns to its original state when a restartless add-on is disabled (which is very important for developing as you’re likely to disable and re-enable many times!) (example)
- watchWindows: add a callback to run when the main Firefox browser window is open (navigator:browser) by calling watchWindows(callback). The callback will be called immediately for existing windows and future windows, so put whatever browser UI modification code you want inside the callback. (example)
One last helper in the repository is the getString function that adds some l10n support to get the correct .properties file for the current locale. It shows off getting additionally packaged files and picking the right locale and some plural forms. I’ve put together a silly example at examples/l10nDialogs.
With all these helper functions, Erik Vold and Nils Maier helped put together some code that loads packaged scripts. We ended up with some code that makes use of getResourceURI so that it correctly works if the addon is .xpi packaged or an extension proxy file. Separating out the code this way lets you better organize the code and makes it easier to share.