Chrome 扩展无法读取 属性 在 contextMenus.create 中创建未定义的

Chrome Extension Cannot Read Property create of undefined in contextMenus.create

这是我的 contextMenus.create 函数,它抛出 无法读取 属性 of create in undefined 错误。

chrome.contextMenus.create({
   "title": "Buzz This",
   "contexts": ["page", "selection", "image", "link"],
   "onclick" : clickHandler
   });

我在相同的内容脚本中也有这个:

chrome.contextMenus.onClicked.addListener(onClickHandler);

// The onClicked callback function.
function onClickHandler(info, tab) {
     window.alert(info.srcUrl);    
};

这是我的manifest.json

{
  "name": "ReportIt",
  "version": "0.0.1",
  "manifest_version": 2,

  "default_locale": "en",
  "description": "Immediately Remove and Report",
  "icons": {
   "16": "images/icon-128.png",
   "128": "images/icon-128.png"
   },


   "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["scripts/contentscript.js"],
    "run_at": "document_end",
    "all_frames": false
    }],

      "permissions": [
      "http://*/*",
      "https://*/*",
      "contextMenus"
      ],

      "content_security_policy": "script-src 'self'; object-src 'self'",
      "web_accessible_resources": 
      [
      "bower_components/angular/*",
      "scripts/background.js"
      ]

    }

我只想在内容脚本中创建上下文菜单。谁能看出问题所在?

您不能在内容脚本中使用大多数 chrome api。相反,当它从内容脚本接收到消息时,创建一个背景页面并在那里创建上下文菜单。当后台页面收到点击事件后,向内容脚本发送消息

https://developer.chrome.com/extensions/messaging