Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extensions: get javascript to work without opening a html page

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

like image 350
Bluefire Avatar asked Feb 19 '23 13:02

Bluefire


1 Answers

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:

extensions tab

For your example, changing the icon, you could use the chrome.browserAction.

like image 137
dan-lee Avatar answered Feb 21 '23 02:02

dan-lee