A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/background below:

background - Mozilla | MDN

background Type 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.

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.

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.

If omitted, this property defaults to classic.

Browser support

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

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:

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

Load two background scripts.

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

Load a custom background page.

Browser compatibility

RetroSearch 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