I am building a chrome extension with context menus. See code below. It should work as follows: "Connect-It" context item is parent item and shows the two Child items (Add a Link, Add a Doc) in submenu when clicked/hovered. When a specific child item is clicked, a new tab opens a new URL. Instead, if either child item is clicked, both new URl's will open. Her is my code. How do I fix this?
// 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"});
})
just replace these two statements
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"});
})
with this single statement
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"});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With