google-chrome-extension

Background pages

Declaring background page in the manifest

There are two ways to register a background page in the extension manifest.

  1. The scripts property

    In the common case, a background page doesn’t require any HTML markup. We can register these kinds of background pages using the scripts property.

    In this case, a background page will be generated by the extension system that includes each of files listed in the scripts property.

    { 
      ...
      "background": {
        "scripts": ["background1.js", "background2.js"],
        "persistent": true
      },
      ...
    }
  2. The page property

    In some cases, we may want to specify HTML in background page, we can achieve that using the page property.

    {
      ...
      "background": {
        "page": "background.html",
        "persistent": true
      },
      ...
    }

scripts VS page

It’s hard to say which one is better. we could use page property and have some elements declared in HTML page for future use. We could also dynamically create such elements in the scripts without explicitly declaring HTML page. It all depends on the actual needs.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow