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, a background page, or a Service worker 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 if 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 (for more information on how these properties are supported, see Browser support):

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.

scripts

An array of string, 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 context.

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.

service_worker

Specify a JavaScript file as the extension service worker. A service worker is a background script that acts as the extension's main event handler.

The background key can also contain this optional property:

persistent

A boolean value.

If omitted, this property defaults 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.
preferred_environment

An array of string listing the preferred environments in order of precedence.

If background specifies a service_worker and a page or scripts, this property enables the extension to tell the browser which background context to use if available. See Browser support for details of the environments supported in the major browsers.

  • document requests that the browser use the extension's background scripts as documents, if supported.
  • service_worker requests that the browser run the extension's background scripts as service workers, if supported.

Chrome only supports service workers, so it ignores this key. If omitted, Firefox and Safari run background scripts as documents. Safari uses a service worker context if the extension specifies scripts and preferred_environment is set to service_worker.

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.

Browser support

Support for the scripts, page, and service_worker properties varies between browsers like this:

  • Chrome:
    • supports background.service_worker.
    • supports background.scripts (and background.page) in Manifest V2 extensions only.
    • before Chrome 121, Chrome refuses to load a Manifest V3 extension with background.scripts or background.page present. From Chrome 121, their presence in a Manifest V3 extension is ignored.
  • Firefox:
    • background.service_worker is not supported (see Firefox bug 1573659).
    • supports background.scripts (or background.page) if service_worker is not specified or the service worker feature is disabled. Before Firefox 120, Firefox did not start the background page if service_worker was present (see Firefox bug 1860304). From Firefox 121, the background page starts as expected, regardless of the presence of service_worker.
  • Safari:
    • supports background.scripts (or background.page) and background.service_worker.
    • when both are specified, Safari uses background.scripts (or background.page) unless preferred_environment is set to service_worker.
    • when preferred_environment is set to service_worker and background.service_worker isn't specified, Safari generates a service worker from background.scripts if present.

To illustrate, this is an example of a cross-browser extension that supports scripts and service_worker. The example has this manifest.json file:

json
{
  "name": "Demo of service worker + event page",
  "version": "1",
  "manifest_version": 3,
  "background": {
    "scripts": ["background.js"],
    "service_worker": "background.js"
  }
}

And, background.js contains:

js
if (typeof browser === "undefined") {
  // Chrome does not support the browser namespace yet.
  globalThis.browser = chrome;
}
browser.runtime.onInstalled.addListener(() => {
  browser.tabs.create({ url: "http://example.com/first-run.html" });
});

When the extension is executed, this happens:

  • in Chrome, the service_worker property is used, and a service worker starts that opens the tab because, in a Manifest V3 extension, Chrome only supports service workers for background scripts.
  • in Firefox, the scripts property is used, and a script starts that opens the tab because Firefox only supports scripts for background scripts.
  • in Safari, the service_worker property is used, and a service worker starts that opens the tab because Safari gives priority to using service workers for background scripts.

Examples

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

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
background
Chrome – Full support
Chrome 54 (Release date: 2016-10-12)
footnote Full support
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote Full support
Firefox – Full support
Firefox 48 (Release date: 2016-08-02)
footnote Full support
Opera – Full support
Opera 41 (Release date: 2016-10-25)
footnote Full support
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 48 (Release date: 2016-08-02)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
page
Chrome – Full support
Chrome 72 (Release date: 2019-01-29)
footnote
footnote Available for use in Manifest V2 only.
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote Full support
Firefox – Full support
Firefox 48 (Release date: 2016-08-02)
footnote Full support
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote
footnote Available for use in Manifest V2 only.
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote
footnote Available for use in Manifest V2 or later.
Firefox for Android – Full support
Firefox for Android 48 (Release date: 2016-08-02)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote
footnote Available for use in Manifest V2 or later.
persistent
Chrome – Full support
Chrome 58 (Release date: 2017-04-19)
footnote
footnote Available for use in Manifest V2 only.
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote
footnote Available for use in Manifest V2 only.
footnote Before Edge 79, this property was required.
Firefox – Full support
Firefox 48 (Release date: 2016-08-02)
footnote
footnote Available for use in Manifest V2 only.
footnote From Firefox 106, persistent and non-persistent pages are supported for Manifest V2.
footnote To Firefox 105, only persistent pages are supported.
footnote Before version 66, Firefox would log a warning even if the value was set to true.
Opera – Full support
Opera 45 (Release date: 2017-05-10)
footnote
footnote Available for use in Manifest V2 only.
Safari – Partial support
Safari 14 – 14 (Release date: 2020-09-16)
footnote Partial support
footnote Only persistent pages are supported.
Safari – Full support
Safari 14.1 (Release date: 2021-04-26)
footnote
footnote Available for use in Manifest V2 only.
Firefox for Android – Full support
Firefox for Android 48 (Release date: 2016-08-02)
footnote
footnote Available for use in Manifest V2 only.
footnote From Firefox for Android 106, persistent and non-persistent pages are supported for Manifest V2.
footnote To Firefox for Android 105, only persistent pages are supported.
footnote Before version 66, Firefox for Android would log a warning even if the value was set to true.
Safari on iOS – Partial support
Safari on iOS 15 – 15.3 (Release date: 2021-09-20)
footnote Partial support
footnote Only non-persistent pages are supported. Requires persistent: false.
Safari on iOS – Partial support
Safari on iOS 15.4 (Release date: 2022-03-14)
footnote Partial support
footnote Only non-persistent pages are supported. Requires persistent: false or service_worker.
preferred_environment
Chrome – No support
Chrome
footnote No support
Edge – No support
Edge
footnote No support
Firefox – Full support
Firefox 136 (Release date: 2025-03-04)
footnote Full support
Opera – No support
Opera
footnote No support
Safari – Full support
Safari 18 (Release date: 2024-09-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 136 (Release date: 2025-03-04)
footnote Full support
Safari on iOS – Full support
Safari on iOS 18 (Release date: 2024-09-16)
footnote Full support
scripts
Chrome – Full support
Chrome 72 (Release date: 2019-01-29)
footnote
footnote Available for use in Manifest V2 only.
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote Full support
Firefox – Full support
Firefox 48 (Release date: 2016-08-02)
footnote
footnote Before Firefox 50, when the debugger is open, scripts are not always loaded in the order given in the array.
Opera – Full support
Opera 60 (Release date: 2019-04-09)
footnote
footnote Available for use in Manifest V2 only.
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote
footnote Available for use in Manifest V2 or later.
Firefox for Android – Full support
Firefox for Android 48 (Release date: 2016-08-02)
footnote
footnote Before Firefox for Android 50, when the debugger is open, scripts are not always loaded in the order given in the array.
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote
footnote Available for use in Manifest V2 or later.
service_worker
Chrome – Full support
Chrome 88 (Release date: 2021-01-19)
footnote
footnote Available for use in Manifest V2 or later.
Edge – Full support
Edge 88 (Release date: 2021-01-21)
footnote
footnote Available for use in Manifest V2 or later.
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 74 (Release date: 2021-02-02)
footnote
footnote Available for use in Manifest V2 or later.
Safari – Full support
Safari 15.4 (Release date: 2022-03-14)
footnote
footnote Available for use in Manifest V2 or later.
Firefox for Android – No support
Firefox for Android
footnote No support
Safari on iOS – Full support
Safari on iOS 15.4 (Release date: 2022-03-14)
footnote
footnote Available for use in Manifest V2 or later.
type
Chrome – Full support
Chrome 92 (Release date: 2021-07-20)
footnote Full support
Edge – Full support
Edge 92 (Release date: 2021-07-22)
footnote Full support
Firefox – Full support
Firefox 112 (Release date: 2023-04-11)
footnote Full support
Opera – Full support
Opera 78 (Release date: 2021-08-03)
footnote Full support
Safari – Full support
Safari 16.4 (Release date: 2023-03-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 112 (Release date: 2023-04-11)
footnote Full support
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.
Has more compatibility info.