Chrome extension context menu not showing up

前端 未结 1 808
星月不相逢
星月不相逢 2021-01-21 20:58

I\'m trying to add a context menu item to a chrome app and it\'s not showing up at all. Everything I\'ve read seems to indicate that I\'m doing the right thing here, but evident

相关标签:
1条回答
  • 2021-01-21 21:39

    You missed a small note in the contextMenu documentation:

    function (optional) onclick
    A function that will be called back when the menu item is clicked. Event pages cannot use this; instead, they should register a listener for chrome.contextMenus.onClicked

    You do have an Event page ("persistent": false), so it applies to you.

    Chrome unloads the page, and the reference to clickHandler can get lost. On the contrary, Event page mechanism ensures that if you registered an event with addListener the page will be loaded again, addListener applied again and then your listener executed.

    So:

    var clickHandler = function(e) {
      console.log('testing testing');
    }
    
    chrome.contextMenus.create({
      "title": "Click Me",
      "contexts": ["page", "selection", "image", "link"]
    });
    
    // Must be synchronously called on event page load,
    //   for instance in the top level code
    chrome.contextMenus.onClicked.addListener(clickHandler);
    
    0 讨论(0)
提交回复
热议问题