Add a Menu Item to Firefox
The SDK doesn't yet provide an API to add new menu items to Firefox.
But it's extensible by design, so anyone can build and publish
modules for add-on developers to use. Luckily, Erik Vold has written
that enables us to add menu items.
This tutorial does double-duty. It describes the general method for
using an external, third-party module in your add-on, and it
describes how to add a menu item using the
menuitems module in particular.
First, create a new add-on. Make a directory called "clickme" wherever you
like, navigate to it and run
mkdir clickme cd clickme cfx init
The usual directory structure will be created:
Create a directory under "clickme" called "packages".
Then download the
menuitems package from
https://github.com/erikvold/menuitems-jplib and extract it into the "packages" directory you just created:
mkdir packages cd packages tar -xf ../erikvold-menuitems-jplib-d80630c.zip
If third-party modules only depend on SDK modules, you can use them right away, but if they depend on other third-party modules, you'll have to install those dependencies as well.
In the package's main directory you'll find a file called "package.json".
Open it up and look for an entry named "dependencies". The entry for the
menuitems package is:
This tells us that we need to install the
which we can do by downloading it from
and adding it under the
packages directory alongside
The documentation for the
tells us to create a menu item using
MenuItem(). Of the options
MenuItem(), we'll use this minimal set:
id: identifier for this menu item
label: text the item displays
command: function called when the user selects the item
menuid: identifier for the item's parent element
insertbefore: identifier for the item before which we want our item to appear
Next, we have to declare our dependency on the
In your add-on's
package.json add the line:
Note that due to
bug 663480, if you
dependencies line to
package.json, and you use any modules from
the SDK, then you must also declare your dependency on that built-in package,
"dependencies": ["menuitems", "addon-sdk"]
Now we're done. Run the add-on and you'll see the new item appear in the
Tools menu: select it and you'll see
info: clicked appear in the
Third-party modules are a great way to use features not directly supported by the SDK, but because third party modules typically use low-level APIs, they may be broken by new releases of Firefox.