Chrome extension > contextMenus Api > can not add onclick handler

自作多情 提交于 2019-12-18 07:19:07

问题


I am trying to create a chrome extension. Currently I am about to create context menu for my extension, which will do something when I click on it. So, following google documentation i create parent.

chrome.contextMenus.create({title: "bla", id: "parent"});

Then I create child and try to add onclick handler to it.

chrome.contextMenus.create({
  title: "bla bla", 
  parentId: "parent",
  id: "child",
  onclick : function() { alert("bla bla bla") }
});

So, its works fine without onclick, and not works all at when onclick here.


回答1:


Event based background pages (persistent=false) have to use chrome.contextMenus.onClicked listeners.

chrome.contextMenus.create({...});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
    alert("bla bla bla");
});


来源:https://stackoverflow.com/questions/24711990/chrome-extension-contextmenus-api-can-not-add-onclick-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!