Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

javascript - Chrome extension > contextMenus Api > can not add onclick handler

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...