Chrome 上下文菜单子项功能同时启动

Chrome Context Menu Child Item functions launching all at once

我正在构建一个带有上下文菜单的 chrome 扩展。请参阅下面的代码。它应该按如下方式工作:"Connect-It" 上下文项是父项,并且在 clicked/hovered 时在子菜单中显示两个子项(添加一个 Link、添加一个文档)。单击特定子项时,新选项卡会打开一个新 URL。相反,如果单击任一子项,两个新的 URl 都将打开。她是我的代号。我该如何解决这个问题?

// background.js

// Parent Level context menu item

chrome.runtime.onInstalled.addListener(function() {
var id = chrome.contextMenus.create({
    id: "Connect-IT",  // Required for event pages
    title: "Connect-IT",
    contexts: ["all"],
  });

});

//  child level contextmenu items 

chrome.runtime.onInstalled.addListener(function() {
var id = chrome.contextMenus.create({
    id: "Add a Link", 
    parentId: "Connect-IT",
    title: "Add a Link",
    contexts: ["all"],  
 });

});


chrome.runtime.onInstalled.addListener(function() {
var id = chrome.contextMenus.create({
    id: "Add a Doc",  
    parentId: "Connect-IT",
    title: "Add a Doc",
    contexts: ["all"],  
 });

});

// click handler below.  This is what's broken.  If either Child Menu Item is clicked both of the function below execute
//     launching the two web pages.  If one menu item is clicked only the website with taht item shoudl launch.


chrome.contextMenus.onClicked.addListener(function addLink(info){     menuItemId="Add a Link",
    chrome.tabs.create({url: "https://www.wufoo.com"});
})

chrome.contextMenus.onClicked.addListener(function addDoc(info){  menuItemId="Add a Doc",
    chrome.tabs.create( {url: "https://www.google.com"});
})

只需替换这两个语句

chrome.contextMenus.onClicked.addListener(function addLink(info){     menuItemId="Add a Link",
  chrome.tabs.create({url: "https://www.wufoo.com"});
})

chrome.contextMenus.onClicked.addListener(function addDoc(info){  menuItemId="Add a Doc",
  chrome.tabs.create( {url: "https://www.google.com"});
})

用这条语句

chrome.contextMenus.onClicked.addListener(function(info, tabs){  
  if ( info.menuItemId === 'Add a Link' )
    chrome.tabs.create( {url: "https://www.google.com"});
  else 
    chrome.tabs.create({url: "https://www.wufoo.com"});
});