diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/mozilla/add-ons/webextensions/manifest.json/background/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/mozilla/add-ons/webextensions/manifest.json/background/index.html')
-rw-r--r-- | files/zh-cn/mozilla/add-ons/webextensions/manifest.json/background/index.html | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/files/zh-cn/mozilla/add-ons/webextensions/manifest.json/background/index.html b/files/zh-cn/mozilla/add-ons/webextensions/manifest.json/background/index.html new file mode 100644 index 0000000000..df4e1fc9ae --- /dev/null +++ b/files/zh-cn/mozilla/add-ons/webextensions/manifest.json/background/index.html @@ -0,0 +1,88 @@ +--- +title: 后台 - background +slug: Mozilla/Add-ons/WebExtensions/manifest.json/background +tags: + - 后台 + - 附加组件 +translation_of: Mozilla/Add-ons/WebExtensions/manifest.json/background +--- +<div>{{AddonSidebar}}</div> + +<table class="fullwidth-table standard-table"> + <tbody> + <tr> + <th scope="row" style="width: 30%;">Type</th> + <td><code>Object</code></td> + </tr> + <tr> + <th scope="row">强制性</th> + <td>不强制</td> + </tr> + <tr> + <th scope="row">示例</th> + <td> + <pre class="brush: json"> +"background": { + "scripts": ["background.js"] +}</pre> + </td> + </tr> + </tbody> +</table> + +<p>通过background 键来给您的扩展程序引入一个或者多个后台脚本文件,以及一个可选的后台页面文件。</p> + +<p>后台脚本放置的是需要长期保持状态,或者需要执行长期的操作,并且与任意特定网页或者浏览器窗口的生命周期无关的代码。</p> + +<p>后台脚本会在扩展程序被加载后立即被加载,并且一直保持被加载状态,只有在扩展程序被禁止或者卸载的时候才停止运行。只要您获得了必要的许可<a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions">permissions</a>,您可以在该脚本中使用任意的扩展程序API。</p> + +<p>浏览<a href="/en-US/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#Background_pages">Anatomy of a WebExtension</a>中的Background pages部分以获取更多的细节。</p> + +<p>background 键最多只能拥有以下的两个属性,两个属性都是可选属性:</p> + +<table class="standard-table"> + <tbody> + <tr> + <td><code>"scripts"</code></td> + <td> + <p>一个字符串数组,数组中的每个字符串都是JavaScript源文件相对于manifest.json的相对路径,JavaScript源文件便是些被包含在扩展程序中的后台脚本文件。</p> + + <p>脚本共享相同的全局窗口</p> + + <p>脚本按照在数组中的顺序进行加载</p> + + <p><strong>注意在低于Firefox 50版本的浏览器下会出现一个问题:</strong>当Firefox debugger已经打开时,脚本并不能总是按照数组中定义的顺序进行加载。若要解决这个问题,您可以使用page属性,用<script>标签将后台脚本文件从页面中加载进来。这个问题已经在Firefox 50中被修复。</p> + </td> + </tr> + <tr> + <td><code>"page"</code></td> + <td> + <p>如果您仅仅指定了“scripts”属性,将生成一个空白的页面来运行指定的脚本。</p> + + <p>如果您需要在页面中显示某些特定的内容,您可以使用“page”属性来定义自己的页面。</p> + + <p dir="ltr" id="tw-target-text">如果您使用此属性,您仍然可以使用“script”属性来指定后台脚本,但您也可以就像在普通网页中一样在页面中包含自己的脚本。</p> + </td> + </tr> + </tbody> +</table> + +<h2 id="示例">示例</h2> + +<p>加载2个后台脚本文件.</p> + +<pre class="brush: json"> "background": { + "scripts": ["jquery.js", "my-background.js"] + }</pre> + +<p>加载1个自定义的后台页面文件.</p> + +<pre class="brush: json"> "background": { + "page": "my-background.html" + }</pre> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("webextensions.manifest.background", 10)}}</p> |