aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/serviceworker
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/serviceworker
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/serviceworker')
-rw-r--r--files/zh-cn/web/api/serviceworker/index.html163
-rw-r--r--files/zh-cn/web/api/serviceworker/onstatechange/index.html123
-rw-r--r--files/zh-cn/web/api/serviceworker/scripturl/index.html92
-rw-r--r--files/zh-cn/web/api/serviceworker/state/index.html115
4 files changed, 493 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/serviceworker/index.html b/files/zh-cn/web/api/serviceworker/index.html
new file mode 100644
index 0000000000..8ea3404a4f
--- /dev/null
+++ b/files/zh-cn/web/api/serviceworker/index.html
@@ -0,0 +1,163 @@
+---
+title: ServiceWorker
+slug: Web/API/ServiceWorker
+tags:
+ - API
+ - Draft
+ - Experimental
+ - Service Worker
+ - 离线
+translation_of: Web/API/ServiceWorker
+---
+<div>{{SeeCompatTable}} {{APIRef("Service Workers API")}}</div>
+
+<div>ServiceWorker API 的<a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API"> ServiceWorker接口</a> 提供一个对一个服务工作者的引用。 多个浏览上下文(例如页面,工作者等)可以与相同的服务工作者相关联,每个都通过唯一的ServiceWorker对象。</div>
+
+<div> </div>
+
+<p>一个ServiceWorker对象在 {{domxref("ServiceWorkerRegistration.active")}} 属性和 {{domxref("ServiceWorkerContainer.controller")}} 属性中可用 — 这是一个激活并在控制页面的service worker(service worker成功注册,被控页面已经重新加载完毕.)</p>
+
+<p>ServiceWorker接口被分配了一系列生命周期事件 --- 安装和激活 --- 以及功能型的事件,包括 fetch.一个ServiceWorker对象有一个与之关联的 {{domxref("ServiceWorker.state")}},指示着它的生命周期.</p>
+
+<h2 id="属性">属性</h2>
+
+<p><em>ServiceWorker 接口继承它父类{{domxref("Worker")}}的属性.</em></p>
+
+<dl>
+ <dt>{{domxref("ServiceWorker.scriptURL")}} {{readonlyinline}}</dt>
+ <dd>返回作为 {{domxref("ServiceWorkerRegistration")}} 一部分所定义的ServiceWorker序列化脚本URL.这个URL必须和注册该ServiceWorker的文档处于同一域.</dd>
+ <dt>{{domxref("ServiceWorker.state")}} {{readonlyinline}}</dt>
+ <dd>返回service worker的状态.其值可能是如下之一:installing,installed,activating,activated或者是redundant.</dd>
+</dl>
+
+<h3 id="Event_handlers">Event handlers</h3>
+
+<dl>
+ <dt>{{domxref("ServiceWorker.onstatechange")}} {{readonlyinline}}</dt>
+ <dd>一个一旦 {{domxref("ServiceWorker.state")}} 发生改变时,即一个类型为statechange事件触发时就会被调用的 {{domxref("EventListener")}} 属性.</dd>
+</dl>
+
+<h2 id="方法">方法</h2>
+
+<dl>
+ <dd><em>ServiceWorker 接口继承它父类 {{domxref("Worker")}} 的方法 ,并带有一个 {{domxref("Worker.terminate")}} 的异常 --- 它不应该从service workers.ServiceWorker中访问.</em></dd>
+</dl>
+
+<h2 id="例子">例子</h2>
+
+<p>代码段来自<a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/registration-events/index.html">service worker registration-events sample</a> (<a href="https://googlechrome.github.io/samples/service-worker/registration-events/">live demo</a>). 这段代码监听了{{domxref("ServiceWorker.state")}} 的变化并且返回它的值.</p>
+
+<pre class="brush: js">if ('serviceWorker' in navigator) {
+ navigator.serviceWorker.register('service-worker.js', {
+ scope: './'
+ }).then(function (registration) {
+ var serviceWorker;
+ if (registration.installing) {
+ serviceWorker = registration.installing;
+ document.querySelector('#kind').textContent = 'installing';
+ } else if (registration.waiting) {
+ serviceWorker = registration.waiting;
+ document.querySelector('#kind').textContent = 'waiting';
+ } else if (registration.active) {
+ serviceWorker = registration.active;
+ document.querySelector('#kind').textContent = 'active';
+ }
+ if (serviceWorker) {
+ // logState(serviceWorker.state);
+ serviceWorker.addEventListener('statechange', function (e) {
+ // logState(e.target.state);
+ });
+ }
+ }).catch (function (error) {
+ // Something went wrong during registration. The service-worker.js file
+ // might be unavailable or contain a syntax error.
+ });
+} else {
+ // The current browser doesn't support service workers.
+}</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('Service Workers', '#service-worker-obj', 'ServiceWorker')}}</td>
+ <td>{{Spec2('Service Workers')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ <td>{{CompatGeckoDesktop("44.0")}}<sup>[1]</sup></td>
+ <td>{{CompatNo}}</td>
+ <td>24</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Android Webview</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ <th>Chrome for Android</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatGeckoMobile("44.0")}}</td>
+ <td>{{ CompatVersionUnknown }}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] Service workers (and <a href="/en-US/docs/Web/API/Push_API">Push</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 Extended Support Release</a> (ESR.)</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li><a href="https://serviceworke.rs">ServiceWorker Cookbook</a></li>
+ <li><a href="/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li>
+ <li><a href="https://github.com/mdn/sw-test">Service workers basic code example</a></li>
+ <li><a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li>
+ <li>{{jsxref("Promise", "Promises")}}</li>
+ <li><a href="/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers</a></li>
+</ul>
diff --git a/files/zh-cn/web/api/serviceworker/onstatechange/index.html b/files/zh-cn/web/api/serviceworker/onstatechange/index.html
new file mode 100644
index 0000000000..1ac245dfd4
--- /dev/null
+++ b/files/zh-cn/web/api/serviceworker/onstatechange/index.html
@@ -0,0 +1,123 @@
+---
+title: ServiceWorker.onstatechange
+slug: Web/API/ServiceWorker/onstatechange
+translation_of: Web/API/ServiceWorker/onstatechange
+---
+<div>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</div>
+
+<p>一个 {{domxref("EventListener")}} 联动的属性,其会被任何stagechange类型事件抛出时联动; 它也基本上能在任何时候{{domxref("ServiceWorker.state")}} 改变时被抛出.</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox">ServiceWorker.onstatechange = function(statechangeevent) { ... }
+ServiceWorker.addEventListener('statechange', function(statechangeevent) { ... } )</pre>
+
+<h2 id="示例">示例</h2>
+
+<p>这段代码出自 <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/registration-events/index.html">service worker registration-events sample</a> (<a href="https://googlechrome.github.io/samples/service-worker/registration-events/">live demo</a>). 它会监听 {{domxref("ServiceWorker.state")}} 的改变并返回它的值.</p>
+
+<pre class="brush: js">var serviceWorker;
+if (registration.installing) {
+ serviceWorker = registration.installing;
+ document.querySelector('#kind').textContent = 'installing';
+} else if (registration.waiting) {
+ serviceWorker = registration.waiting;
+ document.querySelector('#kind').textContent = 'waiting';
+} else if (registration.active) {
+ serviceWorker = registration.active;
+ document.querySelector('#kind').textContent = 'active';
+}
+
+if (serviceWorker) {
+ logState(serviceWorker.state);
+ serviceWorker.addEventListener('statechange', function(e) {
+ logState(e.target.state);
+ });
+}</pre>
+
+<p>注意当<code>statechange</code> 抛出时, service worker的引用可能已经改变。例如:</p>
+
+<pre class="brush: js">navigator.serviceWorker.register(..).then(function(swr) {
+ swr.installing.state == "installing"
+ swr.installing.onstatechange = function() {
+ swr.installing == null;
+ // At this point, swr.waiting OR swr.active might be true. This is because the statechange
+ // event gets queued, meanwhile the underlying worker may have gone into the waiting
+ // state and will be immediately activated if possible.
+ }
+})</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">附注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('Service Workers', '#service-worker-onstatechange-attribute', 'ServiceWorker.onstatechange')}}</td>
+ <td>{{Spec2('Service Workers')}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ <td>{{ CompatGeckoDesktop("44.0") }}<sup>[1]</sup></td>
+ <td>{{CompatNo}}</td>
+ <td>24</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Android Webview</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ <th>Chrome for Android</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{ CompatGeckoMobile("44.0") }}</td>
+ <td>{{ CompatVersionUnknown }}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] Service workers (and <a href="/en-US/docs/Web/API/Push_API">Push</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 and 52 Extended Support Releases</a> (ESR.)</p>
diff --git a/files/zh-cn/web/api/serviceworker/scripturl/index.html b/files/zh-cn/web/api/serviceworker/scripturl/index.html
new file mode 100644
index 0000000000..5b0ce2055d
--- /dev/null
+++ b/files/zh-cn/web/api/serviceworker/scripturl/index.html
@@ -0,0 +1,92 @@
+---
+title: ServiceWorker.scriptURL
+slug: Web/API/ServiceWorker/scriptURL
+translation_of: Web/API/ServiceWorker/scriptURL
+---
+<div>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</div>
+
+<p>作为<code><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration" title="ServiceWorkerRegistion is an interface object representing the service worker registration. You register a service worker to control one or more pages that share the same origin.">ServiceWorkerRegistration</a>的一部分定义,会返回该ServiceWorker的序列化脚本url</code>. 必须与document注册<code>ServiceWorker的地址同源</code>.</p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox">someURL = ServiceWorker.scriptURL
+</pre>
+
+<h3 id="值">值</h3>
+
+<p>一个 {{domxref("USVString")}} (另见 <a href="http://heycam.github.io/webidl/#idl-USVString">WebIDL definition of USVString</a>.)</p>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">附注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('Service Workers', '#service-worker-url-attribute', 'scriptURL')}}</td>
+ <td>{{Spec2('Service Workers')}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ <td>{{ CompatGeckoDesktop("44.0") }}<sup>[1]</sup></td>
+ <td>{{CompatNo}}</td>
+ <td>24</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Android Webview</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ <th>Chrome for Android</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{ CompatGeckoMobile("44.0") }}</td>
+ <td>{{ CompatVersionUnknown }}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] Service workers (and <a href="/en-US/docs/Web/API/Push_API">Push</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 and 52 Extended Support Releases</a> (ESR.)</p>
diff --git a/files/zh-cn/web/api/serviceworker/state/index.html b/files/zh-cn/web/api/serviceworker/state/index.html
new file mode 100644
index 0000000000..9c0045f8a0
--- /dev/null
+++ b/files/zh-cn/web/api/serviceworker/state/index.html
@@ -0,0 +1,115 @@
+---
+title: ServiceWorker.state
+slug: Web/API/ServiceWorker/state
+translation_of: Web/API/ServiceWorker/state
+---
+<div>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</div>
+
+<div>ServiceWorker接口的这个只读的<strong>state</strong>属性返回一个代表service worker当前状态的字符串。值可以是以下这些:installing, installed, activating, activated, 或者是redundant.</div>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox">someURL = ServiceWorker.state
+</pre>
+
+<h3 id="值">值</h3>
+
+<p>一个 {{domxref("ServiceWorkerState")}} 的定义值 (<a href="http://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-state-enum">see the spec</a>.)</p>
+
+<h2 id="示例">示例</h2>
+
+<p>这块代码出自 <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/registration-events/index.html">service worker registration-events sample</a> (<a href="https://googlechrome.github.io/samples/service-worker/registration-events/">live demo</a>). 代码监听了任何{{domxref("ServiceWorker.state")}}中的改变,并返回它的值.</p>
+
+<pre class="brush: js">var serviceWorker;
+if (registration.installing) {
+ serviceWorker = registration.installing;
+ document.querySelector('#kind').textContent = 'installing';
+} else if (registration.waiting) {
+ serviceWorker = registration.waiting;
+ document.querySelector('#kind').textContent = 'waiting';
+} else if (registration.active) {
+ serviceWorker = registration.active;
+ document.querySelector('#kind').textContent = 'active';
+}
+
+if (serviceWorker) {
+ logState(serviceWorker.state);
+ serviceWorker.addEventListener('statechange', function(e) {
+ logState(e.target.state);
+ });
+}</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">附注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('Service Workers', '#service-worker-state-attribute', 'state')}}</td>
+ <td>{{Spec2('Service Workers')}}</td>
+ <td>Initial definition</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>{{CompatibilityTable}}</div>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ <td>{{ CompatGeckoDesktop("44.0") }}<sup>[1]</sup></td>
+ <td>{{CompatNo}}</td>
+ <td>24</td>
+ <td>{{CompatNo}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Android Webview</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ <th>Chrome for Android</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{ CompatGeckoMobile("44.0") }}</td>
+ <td>{{ CompatVersionUnknown }}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatNo}}</td>
+ <td>{{CompatChrome(40.0)}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] Service workers (and <a href="/en-US/docs/Web/API/Push_API">Push</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 and 52 Extended Support Releases</a> (ESR.)</p>