How to add another option to the context menu that appears by right-clicking on the icon expansion?
I try this:
function addMenu(title) {
chrome.contextMenus.create({
title: title,
contexts: ["all"],
onclick: function() { console.log(1)}
});
}
addMenu('Log out');
I receive an error:
Unchecked runtime.lastError while running contextMenus.create: Extensions using event pages must pass an id parameter to chrome.contextMenus.create
Who had such problems? Can you help?
As the error message says, you're using an event page and thus must provide an id
to chrome.contextMenus.create
to use it in the onClicked listener:
chrome.contextMenus.create({
id: "some-command",
title: "some title",
contexts: ["all"]
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "some-command") {
console.log("yay!");
}
});
This is because the event pages are unloaded after a few seconds of inactivity, so inline callbacks can't be used. Whereas onClicked
listener is tracked by Chrome so that the event page is autoloaded when needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With