这篇翻译不完整。请帮忙从英语翻译这篇文章

在浏览器菜单中添加条目。
 
此API基于Chrome的“contextMenus”API构建,该API可让Chrome扩展程序将项目添加到浏览器的上下文菜单中。 browser.menus API为Chrome的API添加了一些功能,特别是可以将项目添加到浏览器的“工具”菜单以及上下文菜单中。
 
在Firefox 55之前,这个API最初也被命名为contextMenus,并且这个名字被保留为别名,所以你可以使用contextMenus编写在Firefox和其他浏览器中工作的代码。
 
你需要拥有“menus”(或别名" contextMenus ")权限来使用此API。

创建菜单项

使用 menus.create()方法创建一个菜单项。你需要传递一个包含条目选项的对象,它包括条目的id,类型,和需要显示出来的文本值。

绑定一个监听器到contextMenus.onClicked事件来监听你菜单项目的点击事件。此监听器会传递一个contextMenus.OnClickData,它包含该事件的详细信息。

你可以根据在调用create()时所传递的参数中使用不同的type值来创建四种不同类型的菜单:

  • "normal":只显示为一个标签的菜单项
  • "checkbox":一个表示二进制状态的菜单项。 它在菜单项旁边显示一个复选标记。 点击该菜单项切换复选标记。监听器会被传递两个额外的属性:“checked”,指示当前是否被选中,以及“wasChecked”,指示在此点击事件发生前是否被选中。
  • "radio":表示一组选项之一的上下文菜单项。 类似于复选框,它也在菜单项旁边显示一个复选标记,监听它的监听器也会被传递“checked”和“wasChecked”。 但是,如果您创建多个单选项,则这些项目将作为一组单选:组内只能选择一项,点击菜单项来选中它。
  • "separator":用于分割菜单的分割线。

如果您创建了多个上下文菜单项目或多个工具菜单项目,则这些项目将被放置在子菜单中。 子菜单的父项将标有扩展名。 例如,下面是一个名为“Menu Demo”的扩展,添加了两个上下文菜单项:

图标

如果你使用 "icons" manifest key 为你的扩展指定一个图标,你的菜单项的旁边就会显示一个指定的图标。浏览器会尝试在普通分辨率下使用16 x 16像素的图标,在高分辨率下使用32 x 32像素的图标:

你可以通过调用 menus.create() 时指定icons选项来给子菜单项设置图标。

例子

下面是一个包含四个项目的菜单,他们分别是:一个普通选项,两个周围有分割线的单选,和一个复选框。单选框使用了自定义图标。

你可以使用以下代码创建一个这样的子菜单:

browser.menus.create({
  id: "remove-me",
  title: browser.i18n.getMessage("menuItemRemoveMe"),
  contexts: ["all"]
}, onCreated);

browser.menus.create({
  id: "separator-1",
  type: "separator",
  contexts: ["all"]
}, onCreated);

browser.menus.create({
  id: "greenify",
  type: "radio",
  title: browser.i18n.getMessage("menuItemGreenify"),
  contexts: ["all"],
  checked: true,
  icons: {
    "16": "icons/paint-green-16.png",
    "32": "icons/paint-green-32.png"
  }
}, onCreated);

browser.menus.create({
  id: "bluify",
  type: "radio",
  title: browser.i18n.getMessage("menuItemBluify"),
  contexts: ["all"],
  checked: false,
  icons: {
    "16": "icons/paint-blue-16.png",
    "32": "icons/paint-blue-32.png"
  }
}, onCreated);

browser.menus.create({
  id: "separator-2",
  type: "separator",
  contexts: ["all"]
}, onCreated);

var checkedState = true;

browser.menus.create({
  id: "check-uncheck",
  type: "checkbox",
  title: browser.i18n.getMessage("menuItemUncheckMe"),
  contexts: ["all"],
  checked: checkedState
}, onCreated);

类型

contextMenus.ContextType
The different contexts a menu can appear in. Possible values are: "all", "audio", "browser_action", "editable", "frame", "image", "link", "page", "page_action", "password", "selection", "tab", "video".
contextMenus.ItemType
The type of menu item: "normal", "checkbox", "radio", "separator".
contextMenus.OnClickData
Information sent when a context menu item is clicked.

属性

contextMenus.ACTION_MENU_TOP_LEVEL_LIMIT
The maximum number of top level extension items that can be added to a context menu item whose ContextType is "browser_action" or "page_action".

函数

contextMenus.create()
Creates a new context menu item.
contextMenus.update()
Updates a previously created context menu item.
contextMenus.remove()
Removes a context menu item.
contextMenus.removeAll()
Removes all context menu items added by this extension.

事件

contextMenus.onClicked
Fired when a context menu item is clicked.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxOperaFirefox for Android
ACTION_MENU_TOP_LEVEL_LIMITChrome Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ACTION_MENU_TOP_LEVEL_LIMIT
Edge Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ACTION_MENU_TOP_LEVEL_LIMIT
Firefox Full support 55
Full support 55
Full support 48
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ACTION_MENU_TOP_LEVEL_LIMIT
Opera Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ACTION_MENU_TOP_LEVEL_LIMIT
Firefox Android No support No
ContextTypeChrome Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ContextType
Edge Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ContextType
Firefox Full support 55
Notes
Full support 55
Notes
Notes 'The 'editable' context does not include password fields. Use the 'password' context for this.
Full support 48
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ContextType
Opera Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ContextType
Firefox Android No support No
ItemTypeChrome Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ItemType
Edge Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ItemType
Firefox Full support 55
Full support 55
Full support 48
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ItemType
Opera Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.ItemType
Firefox Android No support No
OnClickDataChrome Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.OnClickData
Edge Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.OnClickData
Firefox Full support 55
Full support 55
Full support 48
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.OnClickData
Opera Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.OnClickData
Firefox Android No support No
createChrome Full support Yes
Notes Alternate Name
Full support Yes
Notes Alternate Name
Notes Items that don't specify 'contexts' do not inherit contexts from their parents.
Alternate Name Uses the non-standard name: contextMenus.create
Edge Full support 14
Notes Alternate Name
Full support 14
Notes Alternate Name
Notes Items that don't specify 'contexts' do not inherit contexts from their parents.
Alternate Name Uses the non-standard name: contextMenus.create
Firefox Full support 55
Full support 55
Full support 48
Notes Alternate Name
Notes Before version 53, items that don't specify 'contexts' do not inherit contexts from their parents.
Alternate Name Uses the non-standard name: contextMenus.create
Opera Full support Yes
Notes Alternate Name
Full support Yes
Notes Alternate Name
Notes Items that don't specify 'contexts' do not inherit contexts from their parents.
Alternate Name Uses the non-standard name: contextMenus.create
Firefox Android No support No
getTargetElementChrome No support NoEdge No support NoFirefox Full support 63Opera No support NoFirefox Android No support No
onClickedChrome Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.onClicked
Edge Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.onClicked
Firefox Full support 55
Full support 55
Full support 48
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.onClicked
Opera Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.onClicked
Firefox Android No support No
onHiddenChrome No support NoEdge No support NoFirefox Full support 60
Full support 60
Full support 60
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.onHidden
Opera No support NoFirefox Android No support No
onShownChrome No support NoEdge No support NoFirefox Full support 60
Full support 60
Full support 60
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.onShown
Opera No support NoFirefox Android No support No
refreshChrome No support NoEdge No support NoFirefox Full support 60
Full support 60
Full support 60
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.refresh
Opera No support NoFirefox Android No support No
removeChrome Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.remove
Edge Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.remove
Firefox Full support 55
Full support 55
Full support 48
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.remove
Opera Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.remove
Firefox Android No support No
removeAllChrome Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.removeAll
Edge Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.removeAll
Firefox Full support 55
Full support 55
Full support 48
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.removeAll
Opera Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.removeAll
Firefox Android No support No
updateChrome Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.update
Edge Full support 14
Alternate Name
Full support 14
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.update
Firefox Full support 55
Full support 55
Full support 48
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.update
Opera Full support Yes
Alternate Name
Full support Yes
Alternate Name
Alternate Name Uses the non-standard name: contextMenus.update
Firefox Android No support No

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.
Uses a non-standard name.
Uses a non-standard name.

Example extensions

致谢

此API基于Chromium的 chrome.contextMenus API. 此文档来自于Chromium代码中的context_menus.json

文档标签和贡献者

标签: 
此页面的贡献者: yangwang, xgqfrms-GitHub
最后编辑者: yangwang,