Files
webextensions-examples/menu-demo
Pedro Celeste 9433f842d6 Code now reflects newer versions of browser.menus API (#523)
* Extension now is up-to-date with the Menus API

In the previous version of this extension, the author used a comment to
explain that the `browser.menus.onClicked.addListener` event listener
doesn't pass the current check state of a checkbox menu.

To solve this, an additional variable checkedState was used to keep
track of the current value of the context menu.

But now, this function is supported by the menus API in Firefox.

I changed the code to reflect how you would use it with the recent
versions, making it easier for new developers to learn this
functionality.

* Comment now reflects new function body
2023-04-21 14:14:58 +12:00
..
2017-09-15 13:45:26 -07:00
2017-09-08 16:07:36 -07:00
2017-09-08 16:07:36 -07:00

menu-demo

A demo of the menus API.

This add-on injects JavaScript into web pages. The addons.mozilla.org domain disallows this operation, so this add-on will not work properly when it's run on pages in the addons.mozilla.org domain.

This add-on uses the menus namespace to access the functions it needs to create menu items. Note that Chrome, Edge, and Opera all use the contextMenus namespace for this, so this extension will not work in these browsers. For compatibility with these browsers, Firefox also offers the contextMenus namespace, so to make this extension work with other browsers, use contextMenus.

What it does

This add-on adds several items to the browser's context menu:

  • one shown when there is a selection in the page, that logs the selected text to the browser console when clicked.
  • one shown in all contexts, that is removed when clicked.
  • two "radio" items that are shown in all contexts. These items are grouped using a separator item on each side. One radio item adds a blue border to the page, the other adds a green border. Note that these buttons only work on normal web pages, not special pages like about:debugging.
  • one "checkbox" item, shown in all contexts, whose title is updated when the item is clicked.
  • one item that uses the "commands" property to open the add-on's sidebar.

It also adds one item to the browser's "Tools" menu.

What it shows

  • How to create various types of menu item:
    • normal
    • radio
    • separator
    • checkbox
  • How to use contexts to control when an item appears.
  • How to update an item's properties.
  • How to remove an item.