Search code examples
google-chrome

Adding sub contextMenus in Google Chrome extension


I can currently create a contextMenu (right-click menu) in a Google Chrome extension as follows:

chrome.contextMenus.create({
  title: "The name to click",
  contexts:["selection"],
  onclick: theFunctionToRun
});

However, I would like to be able to add a contextMenu of submenus. I am implementing 10 tools that can be invoked through the right-click menu, but would like to have 2 menus each with 5 tools in them based on their categorization.

I have not been able to find any info online or in documentation about this. I'm surprised other people do not want this feature as well so maybe I am just searching for the wrong thing.

Is creating a contextMenu of submenus possible? If so, how can I do this?


Solution

  • I figured it out. I needed to specify an id in the parent menu and then reference the parent ID in the other menus as follows:

    chrome.contextMenus.create({
      title: "The name of the parent menu",
      id: "parent",
      contexts:["selection"]
    });
    
    chrome.contextMenus.create({
      title: "The first action to click",
      parentId: "parent",
      contexts:["selection"],
      onclick: theFirstFunction
    });
    
    chrome.contextMenus.create({
      title: "The second action to click",
      parentId: "parent",
      contexts:["selection"],
      onclick: theSecondFunction
    });