background 编辑

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, unless persistent is specified as false. 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 an extension for some more details.

The background key is an object that may have one of the following 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 scripts which will be executed in the extension's background page.

The scripts share the same window global context.

The scripts are loaded in the order they appear in the array.

If you specify a value for scripts, then an empty page will be created in which your scripts are 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-1.7.1.min.js">), you will also have to change the content_security_policy key in the manifest.json file of your extension.

Note: In Firefox prior to version 50, when the 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 property and include background scripts from the page using <script> tags. This bug is fixed in Firefox 50. From that point on, scripts are always loaded in the order given in the array.

page

If you need some particular content in the page, you can define your own page using the page property, which is a String representing a path, relative your 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 your own scripts from the page, just like in a normal web page.

The background key can also contain the following optional property:

persistent

A Boolean value.

  • 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 (i.e. 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.

Example

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

Load two background scripts.

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

Load a custom background page.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:106 次

字数:5611

最后编辑:7 年前

编辑次数:0 次

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文