Adding items to menus: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(/Resources/)
mNo edit summary
Line 50: Line 50:
**[http://xulplanet.com/references/elemref/ref_menuitem.html menuitem], [http://xulplanet.com/references/elemref/ref_menupopup.html menupopup], [http://xulplanet.com/references/elemref/ref_popup.html popup] elements reference;
**[http://xulplanet.com/references/elemref/ref_menuitem.html menuitem], [http://xulplanet.com/references/elemref/ref_menupopup.html menupopup], [http://xulplanet.com/references/elemref/ref_popup.html popup] elements reference;
**Main Tutorial pages on [http://www.xulplanet.com/tutorials/xultu/menubar.html simple menu bars]  and [http://xulplanet.com/tutorials/xultu/popups.html popup menus].
**Main Tutorial pages on [http://www.xulplanet.com/tutorials/xultu/menubar.html simple menu bars]  and [http://xulplanet.com/tutorials/xultu/popups.html popup menus].
*[http://lxr.mozilla.org/aviarybranch/source/browser/base/content/browser.js#3537 nsContextMenu]
*[http://lxr.mozilla.org/aviarybranch/source/browser/base/content/browser.js#3537 nsContextMenu]
*Sample extensions:  
*Sample extensions:  
**[http://bugmenot.mozdev.org/ BugMeNot]
**[http://bugmenot.mozdev.org/ BugMeNot]
**[http://showimage.mozdev.org/ ShowImage]
**[http://showimage.mozdev.org/ ShowImage]
**[http://quicknote.mozdev.org/ QuickNote]
**[http://quicknote.mozdev.org/ QuickNote]

Revision as of 17:17, 9 December 2004

Adding items to Mozilla menus

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

To add an item to Mozilla menu 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 Mozilla menu, use DOM Inspector. The Tools menu in Firefox is menu_ToolsPopup, in Seamonkey and in Thunderbird (as of 07/2004) 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 XulPlanet. See Resources section below.

Making context menu items appear only when on link, image etc.

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 more unique name than initOverlay(). See Show Failed URL extension by Pike as the simplest real-life example of using window.addEventListener("load").
    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. In the contextPopupShowing() function adjust visibility of the menu items - either manually:
    function contextPopupShowing() {
      var menuitem = document.getElementById("context-item1");
      if(menuitem)
        menuitem.hidden = !gContextMenu.isTextSelected;
    }

    ...or using a 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 aviod clashing with other extensions)

Resources