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