Skip to content

Latest commit

 

History

History
269 lines (186 loc) · 11.2 KB

menus.rst

File metadata and controls

269 lines (186 loc) · 11.2 KB

menus

The menus API first appeared in Thunderbird 66 (see bug 1503421). It is basically the same as the Firefox menus API, but modified to suit Thunderbird. Note that the similar contextMenus API will not be added to Thunderbird.

Use the browser.menus API to add items to the browser's menus. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.

Permissions

  • menus
  • menus.overrideContext

Note

The permission menus is required to use menus.

Functions

create(createProperties, [callback])

Creates a new context menu item. Note that if an error occurs during creation, you may not find out until the creation callback fires (the details will be in runtime.lastError).

  • createProperties (object)
    • [checked] (boolean) The initial state of a checkbox or radio item: true for selected and false for unselected. Only one radio item can be selected at a time in a given group of radio items.
    • [command] (string) Specifies a command to issue for the context click. Currently supports internal command _execute_browser_action.
    • [contexts] (array of :ref:`menus.ContextType`) List of contexts this menu item will appear in. Defaults to ['page'] if not specified.
    • [documentUrlPatterns] (array of string) Lets you restrict the item to apply only to documents whose URL matches one of the given patterns. (This applies to frames as well.) For details on the format of a pattern, see Match Patterns.
    • [enabled] (boolean) Whether this context menu item is enabled or disabled. Defaults to true.
    • [icons] (object)
    • [id] (string) The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension.
    • [onclick] (function) A function that will be called back when the menu item is clicked. Event pages cannot use this.
    • [parentId] (integer or string) The ID of a parent menu item; this makes the item a child of a previously added item.
    • [targetUrlPatterns] (array of string) Similar to documentUrlPatterns, but lets you filter based on the src attribute of img/audio/video tags and the href of anchor tags.
    • [title] (string) The text to be displayed in the item; this is required unless type is 'separator'. When the context is 'selection', you can use %s within the string to show the selected text. For example, if this parameter's value is "Translate '%s' to Pig Latin" and the user selects the word "cool", the context menu item for the selection is "Translate 'cool' to Pig Latin".
    • [type] (:ref:`menus.ItemType`) The type of menu item. Defaults to 'normal' if not specified.
    • [viewTypes] (array of ViewType) List of view types where the menu item will be shown. Defaults to any view, including those without a viewType.
    • [visible] (boolean) Whether the item is visible in the menu.
  • [callback] (function) Called when the item has been created in the browser. If there were any problems creating the item, details will be available in runtime.lastError.

Returns a Promise fulfilled with:

  • integer or string The ID of the newly created item.

update(id, updateProperties)

Updates a previously created context menu item.

  • id (integer or string) The ID of the item to update.
  • updateProperties (object) The properties to update. Accepts the same values as the create function.
    • [checked] (boolean)
    • [contexts] (array of :ref:`menus.ContextType`)
    • [documentUrlPatterns] (array of string)
    • [enabled] (boolean)
    • [icons] (object)
    • [onclick] (function)
    • [parentId] (integer or string) Note: You cannot change an item to be a child of one of its own descendants.
    • [targetUrlPatterns] (array of string)
    • [title] (string)
    • [type] (:ref:`menus.ItemType`)
    • [viewTypes] (array of ViewType)
    • [visible] (boolean) Whether the item is visible in the menu.

remove(menuItemId)

Removes a context menu item.

  • menuItemId (integer or string) The ID of the context menu item to remove.

removeAll()

Removes all context menu items added by this extension.

overrideContext(contextOptions)

Show the matching menu items from this extension instead of the default menu. This should be called during a 'contextmenu' DOM event handler, and only applies to the menu that opens after this event.

  • contextOptions (object)
    • [context] (string) ContextType to override, to allow menu items from other extensions in the menu. Currently only 'tab' is supported. showDefaults cannot be used with this option.
    • [showDefaults] (boolean) Whether to also include default menu items in the menu.
    • [tabId] (integer) Required when context is 'tab'. Requires 'tabs' permission.

Values for context:

  • tab

Note

The permission menus.overrideContext is required to use overrideContext.

refresh()

Updates the extension items in the shown menu, including changes that have been made since the menu was shown. Has no effect if the menu is hidden. Rebuilding a shown menu is an expensive operation, only invoke this method when necessary.

Events

onClicked(info, [tab])

Fired when a context menu item is clicked.

  • info (:ref:`menus.OnClickData`) Information about the item clicked and the context where the click happened.
  • [tab] (:ref:`tabs.Tab`) The details of the tab where the click took place. If the click did not take place in a tab, this parameter will be missing.

onShown(info, tab)

Fired when a menu is shown. The extension can add, modify or remove menu items and call menus.refresh() to update the menu.

  • info (object) Information about the context of the menu action and the created menu items. For more information about each property, see OnClickData. The following properties are only set if the extension has host permissions for the given context: linkUrl, linkText, srcUrl, pageUrl, frameUrl, selectionText.
    • contexts (array of :ref:`menus.ContextType`) A list of all contexts that apply to the menu.
    • editable (boolean)
    • menuIds (array of None) A list of IDs of the menu items that were shown.
    • [frameUrl] (string)
    • [linkText] (string)
    • [linkUrl] (string)
    • [mediaType] (string)
    • [pageUrl] (string)
    • [selectionText] (string)
    • [srcUrl] (string)
    • [targetElementId] (integer)
    • [viewType] (ViewType)
  • tab (:ref:`tabs.Tab`) The details of the tab where the menu was opened.

onHidden()

Fired when a menu is hidden. This event is only fired if onShown has fired before.

Properties

ACTION_MENU_TOP_LEVEL_LIMIT

The maximum number of top level extension items that can be added to an extension action context menu. Any items beyond this limit will be ignored.

Types

ContextType

The different contexts a menu can appear in. Specifying 'all' is equivalent to the combination of all other contexts except for 'tab'.

string

Values for ContextType:

  • all
  • page
  • frame
  • selection
  • link
  • editable
  • password
  • image
  • video
  • audio
  • browser_action
  • tab
  • message_list
  • folder_pane

ItemType

The type of menu item.

string

Values for ItemType:

  • normal
  • checkbox
  • radio
  • separator

OnClickData

Information sent when a context menu item is clicked.

object

  • editable (boolean) A flag indicating whether the element is editable (text input, textarea, etc.).
  • menuItemId (integer or string) The ID of the menu item that was clicked.
  • modifiers (array of string) An array of keyboard modifiers that were held while the menu item was clicked.
  • [button] (integer) An integer value of button by which menu item was clicked.
  • [checked] (boolean) A flag indicating the state of a checkbox or radio item after it is clicked.
  • [displayedFolder] (:ref:`accounts.MailFolder`) The displayed folder, if the context menu was opened in the message list.
  • [frameId] (integer) The id of the frame of the element where the context menu was clicked.
  • [frameUrl] (string) The URL of the frame of the element where the context menu was clicked, if it was in a frame.
  • [linkText] (string) If the element is a link, the text of that link.
  • [linkUrl] (string) If the element is a link, the URL it points to.
  • [mediaType] (string) One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements.
  • [pageUrl] (string) The URL of the page where the menu item was clicked. This property is not set if the click occured in a context where there is no current page, such as in a launcher context menu.
  • [parentMenuItemId] (integer or string) The parent ID, if any, for the item clicked.
  • [selectedFolder] (:ref:`accounts.MailFolder`) The selected folder, if the context menu was opened in the folder pane.
  • [selectedMessages] (:ref:`messages.MessageList`) The selected messages, if the context menu was opened in the message list.
  • [selectionText] (string) The text for the context selection, if any.
  • [srcUrl] (string) Will be present for elements with a 'src' URL.
  • [targetElementId] (integer) An identifier of the clicked element, if any. Use menus.getTargetElement in the page to find the corresponding element.
  • [viewType] (ViewType) The type of view where the menu is clicked. May be unset if the menu is not associated with a view.
  • [wasChecked] (boolean) A flag indicating the state of a checkbox or radio item before it was clicked.

Values for modifiers:

  • Shift
  • Alt
  • Command
  • Ctrl
  • MacCtrl