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
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 forchrome.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);