MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-dev-survey-2018-1

Esta tradução está incompleta. Por favor, ajude a traduzir este artigo do Inglês.

Essa opção da interface do utilizador adiciona um ou mais itens a um menu de contexto do navegador.

Example of content menu items added by a WebExtension, from the context-menu-demo example

You would use this option to expose features that are relevant to specific browser or web page contexts. For example, you could show features to open a graphic editor when the user clicks on an image or offer a feature to save page content when part of a page is selected. You can add plain menu items, checkbox items, radio button groups, and separators to menus. Once a context menu item has been added using contextMenus.create it's displayed in all browser tabs, but you can hide it by removing it with contextMenus.remove.

Especificar itens do menu de contexto

You manage context menu items programmatically, using the contextMenus API. However, you need to request the contextMenus permission in your manifest.json to be able to take advantage of the API.

"permissions": ["contextMenus"]

You can then add (and update or delete) the context menu items in your extension's background script. To create a menu item you specify an id, its title, and the context menus it should appear on:

browser.contextMenus.create({
  id: "log-selection",
  title: browser.i18n.getMessage("contextMenuItemSelectionLogger"),
  contexts: ["selection"]
}, onCreated);

Your extension then listens for clicks on the menu items. The passed information about the item clicked, the context where the click happened, and details of the tab where the click took place can then be used to invoke the appropriate extension functionality.

browser.contextMenus.onClicked.addListener(function(info, tab) {
  switch (info.menuItemId) {
    case "log-selection":
      console.log(info.selectionText);
      break;
    ...
  }
})

Exemplos

O repositório de wexemplos das Extensões da Web no GitHub, contém vários exemplos de extensões que utilizam os itens do menu de contexto:

Etiquetas do documento e contribuidores

Etiquetas: 
 Contribuidores para esta página: mansil
 Última atualização por: mansil,