Run script each time Chrome extension icon clicked
First, if you don't want to show a popup, remove "popup" : "mine.html"
from your manifest.json
(shown in your question).
Your manifest.json
will look something like this:
{
"name": "My Extension",
"version": "0.1",
"manifest_version" : 2,
"description": "Does some simple stuff",
"background" : {
"scripts" : ["background.js"]
},
"browser_action": {
"default_icon": "logo .png"
},
"permissions": ["activeTab"]
}
- Note that
manifest_version
must be there and it must be2
. - Note that the
activeTab
permission has been added. - Note that you can only do one thing when the browser action button is clicked: either you can show a popup, or you can execute a script, but you can't do both.
Second, to execute a script when the icon is clicked, place the code below in your background.js
file (the filename is specified in your manifest.json
):
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "testScript.js"});
});
Finally, testScript.js
is where you should put the code you want to execute when the icon is clicked.
Remove popup from your browser_action section of the manifest and use background pages along with browser Action in the background script.
chrome.browserAction.onClicked.addListener(function(tab) { alert('icon clicked')});
If you want to follow the manifest 3
then you should do:
chrome.action.onClicked.addListener(function (tab) {
console.log("Hello")
});
Further note that you will not see the Hello
in normal console, to see the hello go to extensions menu and click on inspect views
in front of the specific extension menu.
Instead of specifying a popup page, use the chrome.browserAction.onClicked
API, documented here.