Suppose there is a list of items that, when clicked, triggers a function.

function func_1 {alert(1)}; function func_2 {alert(2)}; function func_3 {alert(3)}; ... chrome.contextMenus.create({"title": "item 1", "onclick": func_1}); chrome.contextMenus.create({"title": "item 2", "onclick": func_2}); chrome.contextMenus.create({"title": "item 3", "onclick": func_3}); ... 

And what if there are 20 such points ??? Is it possible for each to create a separate function ???

I would like to know if it is possible to somehow represent all the items as an array ??? And is it possible to programmatically get the title of the item that was clicked on ???

The main goal: to combine all functions into one.

    1 answer 1

    Two parameters info and tab - onclick are passed to the callback function: function (info, tab). The first one contains information about the menu item (for example, the title is in info.menuItemId), the second one contains information about the tab. Accordingly, it is possible to hang one aggregator function on callback. For example, the function that displays information on the menu item and tab when clicking:

     function genericOnClick(info, tab) { console.log("item " + info.menuItemId + " was clicked"); console.log("info: " + JSON.stringify(info)); console.log("tab: " + JSON.stringify(tab)); } chrome.contextMenus.create({"title": "item 1", "onclick": genericOnClick}); 
    • ► Thanks, but I saw the example in the documentation ( developer.chrome.com/extensions/examples/api/contextMenus/basic/… ). However, I did not understand how menuItemId is formed, and why when updating the extension (CTRL + R) menuItemId changes. And more precisely, it increases constantly by the same number. ► Pro title also did not understand. info.title returns undefined - BART96
    • one
      And what does JSON.stringify (info) return? And id is different each time different returns - because you obviously did not appoint it. - iKest
    • JSON.stringify(info) returns full information. ► Now I understand. Initially I thought that the id automatically attached, but did not understand why it was changing. Thanks for the answer. ► And the title , then, can not be obtained. - BART96
    • I'm sorry too misled you, of course menuItemId has nothing to do with the tile. That's why info.title returns undefined incomprehensible ... It should be the same line in the menu to display. - iKest