Add contextmenu items to a Chrome extension's browser action button

前端 未结 3 843
执念已碎
执念已碎 2020-11-30 03:07

A G Chrome extension can have a \'browser action\'. Usually the ext developer displays the options when you click on it, meaning every action requires 2 clicks, even the def

3条回答
  •  醉话见心
    2020-11-30 03:54

    It is now possible, AdBlock chrome extensions has it. Below is working example of "context menu in browser action".

    manifest.json:

    {
        "name": "Custom context menu in browser action",
        "version": "1",
        "manifest_version": 2,
        "background": {
          "scripts": ["background.js"]
        },
        "browser_action": {
          "default_title": "Some tooltip",
          "default_popup": "popup.html"
        },
        "permissions": [
          "contextMenus"
        ],
        "icons": {
          "16": "icon16.png"
        }
    }
    

    background.js:

    chrome.contextMenus.removeAll();
    chrome.contextMenus.create({
          title: "first",
          contexts: ["browser_action"],
          onclick: function() {
            alert('first');
          }
    });
    

    Note that if you use an Event page, you cannot use the onclick attribute; you'll need to add a listener to chrome.contextMenus.onClicked instead.

提交回复
热议问题