15

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.

Kaz
  • 1,047
  • 8
  • 18
Farhad Ghayour
  • 303
  • 1
  • 4
  • 11
  • 2
    The javascript code you have would run from a background script. But ["This event will not fire if the browser action has a popup."](https://developer.chrome.com/extensions/browserAction#event-onClicked) – Teepeemm Jul 09 '15 at 03:48
  • That was definitely it! Thank you! I tried this, but guess I wanted a HTML box to be there with the background script. – Farhad Ghayour Jul 09 '15 at 04:06
  • 1
    You can have the html box. You just need to have popup.html use popup.js, and popup.js have `alert('working?');` without the onClicked event. – Teepeemm Jul 09 '15 at 04:15
  • Thanks for the help @Teepeemm, however, when I update the manifest.json to "browser_action": { "default_title": "Mark this position!", "default_icon": "icon.png" }, "background": { "page": "popup.html" }, and I have the chrome.browserAction.onClicked.addListener, I can get alerts, etc. to work, but the html doesn't show up. I don't see the actual popup, nor can I query any of it. – Farhad Ghayour Jul 09 '15 at 04:55
  • You may call it `popup.html`, but if it's `background.page`, then it's a background page, not a popup. Let me know if my answer doesn't quite clarify how things should be arranged. – Teepeemm Jul 09 '15 at 05:12

2 Answers2

26

There are two approaches you can use:

Approach 1: Use a background script.

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?');
});

Approach 2: Use a popup. manifest.json:

"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.)

Teepeemm
  • 4,331
  • 5
  • 35
  • 58
  • 1
    You must at least have `"browser_action": {}` in your manifest, otherwise you will get `Cannot read property 'onClicked' of undefined`. – Raine Revere Aug 20 '20 at 01:17
  • 1
    Does not work if you use `"manifest_version": 3` in your manifest.json. You can not put `"browser_action"` object in manifest.json in this case. It gives error: 'browser_action' requires manifest version of 2 or lower. – sander Jan 29 '22 at 01:17
1

If you want to have a popup when you click your extension icon.

and you use manifest_version: 3 in your manifest.json file.

"action":{
    "default_popup": "/path/to/your.html"
},
sander
  • 1,426
  • 4
  • 19
  • 46