无法获取Chrome上下文菜单以显示Chrome扩展程序

时间:2016-06-05 19:32:03

标签: javascript jquery google-chrome google-chrome-extension contextmenu

我已经浏览了其他关于此的帖子,我似乎无法弄清楚我的chrome contentMenu api的实现有什么问题。我刚刚从Chrome API教程中复制了代码,但我没有收到任何错误,但是当我右键单击时,它只是没有显示在菜单中。

我错过了什么吗?

清单

 {
      "manifest_version": 2,      

  "name": "Chrome extension practice",
  "description": "Practice",
  "version": "1.0",
  "content_scripts": 
  [
    {
      "matches": ["*://*/*"],
      "js": ["bower_components/jquery/dist/jquery.min.js", "script.js"],
      "run_at": "document_end"
    }
  ],

  "permissions": [
    "storage",
    "contextMenus",
    "background",
    "https://twitter.com/",
    "http://twitter.com/"  
  ],

  "background": {
    "scripts": ["background.js"]
  },

  "browser_action": {       
   "default_title": "Practice",
    "default_icon": "fa-moon.png",
    "default_popup": "popup.html"
  }
}

background.js

 window.onload = function() {
    //CONTEXT MENU this should go somewhere else, but we'll need to resolve onload conflict
    alert("Background script loaded");
    chrome.contextMenus.create({
        id: "custom-context-a",
        title: "Hide Tweet",
        contexts: ["launcher", "all"],
    }, function(){
        console.log(chrome.runtime.lastError);
    });

    chrome.contextMenus.onClicked.addListener(contextClicked.bind(this));
};

function contextClicked(e){
    console.log(e.menuItemId);
}

编辑---

仍然不确定我的代码有什么问题,但来自this帖子的ExpertSystem示例

1 个答案:

答案 0 :(得分:2)

chrome.contextMenus.create旁边写window.onload。并且alert在background.js

中不起作用