I want to trigger a click that listens for the following code:
chrome.browserAction.onClicked.addListener(function(tab) {});
The reason is because I have a working extension that listens in the background script (addListener above) and runs some scripts when clicked:
chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.executeScript(tab.id, {file: 'abc.js'}); chrome.tabs.executeScript(tab.id, {file: 'xxx.js'}); });
And now I want to call this "onClicked" from the context menu:
var myContextPage = chrome.contextMenus.create({"title": myTitle, "contexts":["page"], "onclick": fctContext});
So, I thought the easiest way is to have a "click" in fctContext. Maybe there is a better way, but I can not solve my problem. I also tried to run "executeScript", but this does not work either.
Thanks in advance!
// Update
Solution of answers: This solution works:
//Shared code. When the argument length is two, it is coming from the context // menu, while a single argument is coming from the browser action. function fctContext() { var tab = arguments.length == 2 ? arguments[1] : arguments[0]; // Do whatever you want with the tab. } // Browser Action chrome.browserAction.onClicked.addListener(fctContext); // Context Menu chrome.contextMenus.create({ title: myTitle, contexts: ['page'], onclick: fctContext });
Solution after testing some other things:
function fctStart() { chrome.tabs.getSelected(null, function(tab){ chrome.tabs.executeScript(tab.id, {file: 'abc.js'}); chrome.tabs.executeScript(tab.id, {file: 'xxx.js'}); }); }
In this case, fctStart() works from anywhere without having to skip the tab.
javascript google-chrome google-chrome-extension
Daniel
source share