Bluefire
Bluefire

Reputation: 14099

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

Upvotes: 4

Views: 9844

Answers (1)

dan-lee
dan-lee

Reputation: 14492

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.

Upvotes: 8

Related Questions