Adding drop-down menu to chrome extension icon

后端 未结 2 798
伪装坚强ぢ
伪装坚强ぢ 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: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}`) };
    

提交回复
热议问题