Adding a Toolbar Button in a Bootstrapped Firefox Extension
Adding a toolbar button in a Firefox extension is simple. Since the Firefox user interface is described using XUL, it is enough to inject a snippet of XUL into the “toolbar palette” in browser.xul. Traditionally this would be done with a XUL overlay. Experienced XUL developers know that a button added like this won’t show up in the toolbar unless it is added explicitly by the user (using View/Toolbars/Customize…). This can be solved by adding the button to the toolbar manually and saving the toolbar’s “currentset”, which contains the list of buttons it contains. This should be done on first run only to ensure that the user’s settings are preserved if they remove the button later.
But what if you are writing a bootstrapped extension? XUL overlays are not supported, so you have to add the button to the toolbar palette by hand. Coordinating the toolbar palette, the visible toolbar and the toolbar’s persistent current set isn’t rocket science, but it is a bit tricky. You want to make sure that the button is added to the toolbar on first run and is visible on subsequent runs. At the same time, it should be possible to remove it and add it back using the toolbar customization feature.
After some trial and error, as well as plundering Dmitry Gutov‘s nicely written Replace Bookmark extension for ideas, I came up with the following function:
Remember that you should remove the button from the palette and toolbar manually when the extension is disabled or uninstalled to avoid memory leaks.
« Going Functional - Going Functional: Map »