Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google Chrome extension, add the tab to context menu

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?

like image 503
Artem Ekzarho Avatar asked Sep 22 '15 13:09

Artem Ekzarho


1 Answers

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.

like image 102
wOxxOm Avatar answered Oct 19 '22 09:10

wOxxOm