I'm having a difficult time understanding how to have some JS to run when the chrome extension icon has been clicked. I'd like to for example, read some properties from the document, when the icon has been clicked.
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"clipboardWrite"
]
And inside the popup.html
, I have the following:
chrome.browserAction.onClicked.addListener(function(tab) {
alert('working?');
});
But, this doesn't appear to be working. I tried having the JS above inside a background script (inside manifest.json
) but that didn't work either.
In browser action, you should include default_popup value which points to the HTML file to be rendered as popup. In your case it is userinfo. html. You should look into setPopup method and chrome.
Open the Extensions by clicking the puzzle icon next to your profile avatar. A dropdown menu will appear, showing you all of your enabled extensions. Each extension will have a pushpin icon to the right of it. To pin an extension to Chrome, click the pushpin icon so that the icon turns blue.
A browser action is a button that your extension adds to the browser's toolbar. The button has an icon, and may optionally have a popup whose content is specified using HTML, CSS, and JavaScript. This key is replaced by action in Manifest V3 extensions.
On click — meaning the extension is able to see and alter what's in your browser only when you actively click it (and then only for the site that's currently open in that specific tab)
There are two approaches you can use:
in manifest.json
:
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"activeTab",
"clipboardWrite"
],
"background": {
"persistent": false,
"scripts": ["background.js"]
}
(You can also use "page": "background.html"
instead of "scripts"
.)
in background.js
:
chrome.browserAction.onClicked.addListener(function(tab) {
alert('working?');
});
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"clipboardWrite"
]
in popup.html
:
<html>
<head>
<script src="popup.js"></script>
</head>
</html>
in popup.js
:
alert('working?');
Your problem was that you were mixing the two. If you use a browser_action.default_popup
, then chrome.browserAction.onClicked
is never triggered. (And you wouldn’t want a background page named popup.html
, since that would cause all sorts of confusion.)
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