Adding drop-down menu to chrome extension icon

后端 未结 2 747
伪装坚强ぢ
伪装坚强ぢ 2021-02-16 00:10

I know that I can associate a popup.html to clicks on the extension button. However, I want it to be a menu, like the one popping up after clicking on Chrome\'s own \"C

相关标签:
2条回答
  • 2021-02-16 00:20

    You can't.

    You can either register a click via chrome.browserAction.onClicked but show no UI, or open an HTML page in a popup. You can style it to look like a menu, but it will still not be like a native drop-down menu.

    Note that you can right-click the extension button, but the menu you get there is fixed. I submitted a feature request a long time ago regarding that, but it never took off. There is a contextMenus context "browser_action" now that achieves this.

    0 讨论(0)
  • 2021-02-16 00:23

    A practical example, since the documentation isn't actually that clarifying:

    manifest.js

    {
      "name": "Foo menu",
      "description": "Demonstrating context menus on browser actions",
      "manifest_version": 2,
      "version": "1.0",
      "background": {
        "persistent": false,
        "scripts": ["background.js"]
      },
      "browser_action": {
        "default_title": "Click me"
      },
      "icons": {
        "16": "icon.png",
      },
      "permissions": ["contextMenus"]
    }
    

    background.js

    chrome.contextMenus.removeAll();
    chrome.contextMenus.create({
      id: "FOO_ACTION",
      title: "Foo",
      contexts: ["browser_action"],
    });
    chrome.contextMenus.onClicked.addListener((info, tab) => {
      if (info.menuItemId === "FOO_ACTION") {
        // Execute foo using tab id
        foo(tab.id);
      }
    });
    
    const foo = (tabId) => { console.log(`Foo called on tab with id ${tabId}`) };
    
    0 讨论(0)
提交回复
热议问题