I know there is a way to make JS work in chrome extension: just include a default_popup parameter in your manifest.json to specify an HTML page, then include the JS onto the HTML using <script>
. But is there a way to get JS to do some stuff without having to open a HTML page (e.g. change the icon of the extension without a HTML popup having to be opened)?
Yes, that is called background page. You can create it without a .html
file, but it will dynamically create one for you, called _generated_background_page.html
.
You can can add following to your manifest.json
to specify a background page:
{
"name": "My extension",
...
"background": {
"scripts": ["background.js"]
},
...
}
To view the background page go to chrome://chrome/extensions
activate "Developer mode" and you can see the background page with developer tools:
For your example, changing the icon, you could use the chrome.browserAction
.
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