files/en-us/mozilla/add-ons/webextensions/manifest.json/background/index.md
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):
The background key can also contain this optional property:
Support for the scripts, page, and service_worker properties varies between browsers like this:
background.service_worker.background.scripts (and background.page) in Manifest V2 extensions only.background.scripts or background.page present. From Chrome 121, their presence in a Manifest V3 extension is ignored.background.service_worker is not supported (see Firefox bug 1573659).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.background.scripts (or background.page) and background.service_worker.background.scripts (or background.page) unless preferred_environment is set to service_worker.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:
{
"name": "Demo of service worker + event page",
"version": "1",
"manifest_version": 3,
"background": {
"scripts": ["background.js"],
"service_worker": "background.js"
}
}
And, background.js contains:
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:
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.scripts property is used, and a script starts that opens the tab because Firefox only supports scripts for background scripts.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. "background": {
"scripts": ["jquery.js", "my-background.js"]
}
Load two background scripts.
"background": {
"page": "my-background.html"
}
Load a custom background page.
{{Compat}}