Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add onClicked handler to browser action without popup?

I'm starting out with Chrome Extensions.

My extension has 2 content_scripts which are executed on page load. I'd like to add an onClicked handler for my browser action from inside one of these JS files.

Basically what I want to do is chrome.browserAction.onClicked.addListener but I've read in a similar question that this only works inside of a popup html file. I'm not using a popup file because I don't currently need one, and I need my JS to run in the global scope (as it does without a default_popup).

Here's my manifest.json:

{
  "manifest_version": 2,

  "name": "Foo",
  "description": "Foo description.",
  "version": "0.0.0.1",
  "permissions":[
    "tabs", "http://*/*", "https://*/*"
  ],
  "browser_action": {
    "default_icon": "icon.png"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["jquery1.9.1.min.js", "foo.js"]
    }
  ]
}

Sorry but my googling skills couldn't find how to do this. Is there a way to attach an onClicked handler to my browser action or do I need an html popup for that?

like image 617
Fabrício Matté Avatar asked Dec 22 '25 17:12

Fabrício Matté


1 Answers

Decided to just move my overly long comment to an answer.

If you have a popup defined then the onClicked event is never fired, but it works just fine without one. Just pop the handler into your background page and it should work just fine. Be aware that chrome.browserAction, as well as just about every other chrome.* API, cannot be accessed from a content script, so you will require the use of a background or event page.

like image 84
BeardFist Avatar answered Dec 24 '25 07:12

BeardFist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!