Chrome 扩展,自定义上下文菜单项未显示

Chrome extension, custom context menu item not showing

我知道这个问题在这个网站上已经被问过很多次了,例如. However, Chrome upgrades so fast and it seems those solutions don't work for the latest version anymore. I also checked official doc,好像也不行。

这是我的文件:

manifest.json

{
  "name": "Try Context Menu",
  "version": "0.0.1",
  "permissions": [ "tabs", "contextMenus", "http://*/*", "https://*/*" ],
  "background": {
    "scripts": [ "background.js" ],
    "persistent": false
  },
  "icons": {
    "16": "icon16.png",
    "48": "icon48.png",
    "128": "icon128.png"
  },
  "manifest_version": 2
}

background.js

function clickHandler() {
  alert('great');
}

chrome.contextMenus.create({
  "id": "qwertyuiop",
  "title": "Click Me",
  "contexts": ["page", "selection", "image", "link"],
  "onclick" : clickHandler
});

chrome.contextMenus.onClicked.addListener(clickHandler);

我仔细阅读了几个示例扩展'source code

由于我将 "persistent" 设置为 false,扩展名是 运行 活动页面。因此,我的代码通过从 chrome.contextMenus.create:

的参数对象中删除 onclick 来工作
chrome.contextMenus.create({
  "id": "qwertyuiop",
  "title": "Click Me",
  "contexts": ["page", "selection", "image", "link"]
});

这是 Event Page 文档中的注释:

  1. If you're using the context menus API, pass a string id parameter to contextMenus.create, and use the contextMenus.onClicked callback instead of an onclick parameter to contextMenus.create.

真正奇怪的是,该注释是根据 最佳实践 归档的,而如果我不这样做,扩展 甚至无法工作。实际上,这是必须做的