我正在构建一个包含上下文菜单的Chrome扩展程序。请见下方代码。它应该按照如下方式工作:当点击/悬停在“Connect-It”上下文项时,它将显示两个子项(添加链接、添加文档)的子菜单。当单击特定的子项时,会打开一个新的标签页并跳转到新的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"});
})