最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome Extension Cannot Read Property create of undefined in contextMenus.create - Stack Overflow

programmeradmin0浏览0评论

This is my contextMenus.create function which is throwing the cannot read property of create in undefined error.

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

I also have this in the same content script:

chrome.contextMenus.onClicked.addListener(onClickHandler);

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

This is my 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"
      ]

    }

All I want to do is create a context menu in a content script. Can anyone see the problem?

This is my contextMenus.create function which is throwing the cannot read property of create in undefined error.

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

I also have this in the same content script:

chrome.contextMenus.onClicked.addListener(onClickHandler);

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

This is my 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"
      ]

    }

All I want to do is create a context menu in a content script. Can anyone see the problem?

Share Improve this question asked May 31, 2015 at 0:33 Dave GordonDave Gordon 1,8354 gold badges30 silver badges53 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

You cannot use most chrome apis in content scripts. Instead, create a background page and create the context menu there when it receives a message from the content script. When the background page receives the click event, send a message to the content script.

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

发布评论

评论列表(0)

  1. 暂无评论