Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension's Icon on click

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.

like image 959
Farhad Ghayour Avatar asked Jul 09 '15 03:07

Farhad Ghayour


People also ask

How do I show a pop up extension in Chrome?

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.

How do I pin an extension icon in 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.

What is browser action icon?

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.

What does on Click mean Chrome extension?

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)


1 Answers

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

like image 64
Teepeemm Avatar answered Oct 24 '22 01:10

Teepeemm