MozillaZine

Adding items to menus

From MozillaZine Knowledge Base

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

Note: Information in this section applies to Mozilla Suite, Firefox and Thunderbird and other XUL-based applications. When I say Mozilla, I mean one of these three applications.

This tutorial describes steps necessary to add a static or a dynamic menu item to Mozilla from an extension. It's created for beginner extension developers; if you're a user, you probably want to search for existing extensions that do what you need.

Creating the overlay

To add an item to an existing menu of a Mozilla-based application without editing its source files, you first need to create an overlay.

The following lines in the overlay will add two items to contentAreaContextMenu popup menu (the menu that appears when you right-click a web page):

<popup id="contentAreaContextMenu">
  <menuitem id="context-item1" label="Item 1" oncommand="alert('Item1');"/>
  <menuitem id="context-item2" label="Item 2" oncommand="alert('Item2');"/>
</popup>

These lines in the overlay add an item to Firefox "Tools" menu:

<menupopup id="menu_ToolsPopup">
  <menuitem id="example-item" oncommand="alert('Hello!');" label="Hi" accesskey="i"/>
</menupopup>

You can specify position of the item being inserted with insertafter/insertbefore/position attributes [1]

To find out id of specific menu, use DOM Inspector. The "Tools" menu in Firefox is menu_ToolsPopup, in Seamonkey and in Thunderbird it's taskPopup.

The simplest extension that adds an item to content area context menu is BugMeNot. Adding items to Tools menu example can be seen in QuickNote. Read more about <popup>, <menupopup> and <menuitem> at XUL Planet. See Resources section below.

Dynamically showing context menu items

The simplest example extension is ShowImage.

  1. Add a JS script to your overlay.
  2. Add the following line in the beginning of the script. This line gets executed when the overlay is being loaded and it tells Mozilla to call the initOverlay() function when the window your overlay is attached to finishes loading. Note, you should give your real function a more unique name than initOverlay().
    window.addEventListener("load", initOverlay, false);
  3. In the initOverlay() function add a popupshowing event listener for the menu you are extending, like this:
    function initOverlay() {
      var menu = document.getElementById("contentAreaContextMenu");
      menu.addEventListener("popupshowing", contextPopupShowing, false);
    }
    This tells Mozilla to call contextPopupShowing() every time the contentAreaContextMenu popup is about to show.
  4. You are now in a position to adjust the visibility of the menu items for the popup menu, within the contextPopupShowing() function. Visibility will often be determined on the basis of some browser condition or selected page element. In the example below, the menu item will only appear when some page text is selected. Setting visibility can either be done manually:
    function contextPopupShowing() {
      var menuitem = document.getElementById("context-item1");
      if(menuitem)
        menuitem.hidden = !gContextMenu.isTextSelected;
    }

    ...or by using the convenience function nsContextMenu.showItem():

    function contextPopupShowing() {
      gContextMenu.showItem("context-item1", gContextMenu.isTextSelected);
    }

    Look in nsContextMenu.js (Seamonkey) or browser.js (Firefox) for the definition of nsContextMenu.

(remember that you need to use unique identifiers instead of generic contextPopupShowing to avoid conflicts with other extensions)

Resources