background

Type Object
Mandatory No
Manifest version 2 or higher
Example

json

"background": {
  "scripts": ["background.js"]
}

Use the background key to include one or more background scripts or a background page in your extension.

Background scripts are the place to put code that needs to maintain a long-term state, or perform long-term operations, independently of the lifetime of any particular web pages or browser windows.

Background scripts are loaded as soon as the extension is loaded and stay loaded until the extension is disabled or uninstalled unless persistent is specified as false. You can use any WebExtension APIs in the script as long as you have requested the necessary permissions.

See Background scripts for some more details.

The background key is an object that must have one of these properties:

scripts

An Array of Strings, each of which is a path to a JavaScript source. The path is relative to the manifest.json file itself. These are the scripts that are executed in the extension's background page.

The scripts share the same window global context.

The scripts are loaded in the order they appear in the array.

If you specify scripts, an empty page is created where your scripts run.

Note: If you want to fetch a script from a remote location with the <script> tag (e.g., <script src = "https://code.jquery.com/jquery-3.6.0.min.js">), you have to change the content_security_policy key in the manifest.json file of your extension.

page

If you need specific content in the background page, you can define a page using the page property. This is a String representing a path, relative to the manifest.json file, to an HTML document included in your extension bundle.

If you use this property, you can not specify background scripts using scripts, but you can include scripts from the page, just like a normal web page.

The background key can also contain this optional property:

persistent

A Boolean value.

If omitted, this property default to true in Manifest V2 and false in Manifest V3. Setting to true in Manifest V3 results in an error.

  • true indicates the background page is to be kept in memory from when the extension is loaded or the browser starts until the extension is unloaded or disabled, or the browser is closed (that is, the background page is persistent).
  • false indicates the background page may be unloaded from memory when idle and recreated when needed. Such background pages are often called Event Pages, because they are loaded into memory to allow the background page to handle the events to which it has added listeners. Registration of listeners is persistent when the page is unloaded from memory, but other values are not persistent. If you want to store data persistently in an event page, then you should use the storage API.
type

A String value.

Determines whether the scripts specified in "scripts" are loaded as ES modules.

  • classic indicates the background scripts or service workers are not included as an ES Module.
  • module indicates the background scripts or service workers are included as an ES Module. This enables the background page or service worker to import code.

If omitted, this property defaults to classic.

Example

json

  "background": {
    "scripts": ["jquery.js", "my-background.js"]
  }

Load two background scripts.

json

  "background": {
    "page": "my-background.html"
  }

Load a custom background page.

Browser compatibility

BCD tables only load in the browser