我们的志愿者还没有将这篇文章翻译为 中文 (简体)。加入我们帮助完成翻译!
Type | Object |
---|---|
Mandatory | No |
Example |
"background": { "scripts": ["background.js"] } |
Use the background
key to include one or more background scripts, and optionally a background page in your extension.
Background scripts are the place to put code that needs to maintain 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. You can use any of the WebExtension APIs in the script, as long as you have requested the necessary permissions.
See the "Background pages" section in Anatomy of a WebExtension for some more details.
The background
key is an object that may have up to two properties, both optional:
"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 background scripts that will be included in the extension. The scripts share the same The scripts are loaded in the order they appear in the array. Note that there is a bug affecting Firefox versions earlier than 50: when the Firefox debugger is open, scripts are not always loaded in the order given in the array. To work around this bug, you can use the |
"page" |
If you specify only If you need some particular content in the page, you can define your own page using the If you use this property, you can still specify background scripts using |
Chrome incompatibilities
background
Firefox does not support the "persistent"
property. Background scripts stay loaded all the time.
Example
"background": { "scripts": ["jquery.js", "my-background.js"] }
Load two background scripts.
"background": { "page": "my-background.html" }
Load a custom background page.