Object
Mandatory No Manifest version 2 or higher Example
"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
.
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
. If both are specified, uses background.scripts
(or background.page
), unless preferred_environment
is set to service_worker
.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.
Browser compatibilityRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4