Reputation: 1402
I'm using this following script to add a context menu with two sub menu using Chrome Extension.
chrome.contextMenus.create({
id: "context1",
title: "Do it!",
contexts: ["all"]
});
chrome.contextMenus.create({
id: "context2",
title: "Do it too!",
contexts: ["all"]
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (tab) {
var code = 'doit();';
chrome.tabs.executeScript(tab.id, { code: code });
}
});
I want to know how to make each of them execute a different script using chrome.contextMenus.onClicked.addListener
?
Here's my manifest file.
{
"manifest_version": 2,
"name": "My Extension",
"version": "0.1",
"content_scripts": [ {
"js": [ "jquery-3.3.1.min.js", "content.js"],
"css": ["bootstrap.min.css"],
"matches": [ "<Call_urls>" ]
} ],
"background": {
"persistent": false,
"scripts": ["ctx.js"] //<- The file for script above.
},
"permissions": [
"contextMenus",
"activeTab"
]
}
Upvotes: 0
Views: 2516
Reputation: 1402
Thanks to wOxxOm, I figured it out.
chrome.contextMenus.create({
id: "context1",
title: "First",
contexts: ["all"]
});
chrome.contextMenus.create({
id: "context2",
title: "Second",
contexts: ["all"]
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (tab) {
if (info.menuItemId === "context1"){
var code = 'alert("First Clicked");';
chrome.tabs.executeScript(tab.id, { code: code });
}
if (info.menuItemId === "context2"){
var code = 'alert("Second Clicked");';
chrome.tabs.executeScript(tab.id, { code: code });
}
}
});
By using this above script. You will get separated alert for each item selected. Example: You will get alert Second Clicked
if you selected the Second
option.
Upvotes: 2