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/web/api/clients | |
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/web/api/clients')
-rw-r--r-- | files/zh-cn/web/api/clients/claim/index.html | 66 | ||||
-rw-r--r-- | files/zh-cn/web/api/clients/get/index.html | 60 | ||||
-rw-r--r-- | files/zh-cn/web/api/clients/index.html | 90 | ||||
-rw-r--r-- | files/zh-cn/web/api/clients/matchall/index.html | 68 | ||||
-rw-r--r-- | files/zh-cn/web/api/clients/openwindow/index.html | 81 |
5 files changed, 365 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/clients/claim/index.html b/files/zh-cn/web/api/clients/claim/index.html new file mode 100644 index 0000000000..4797f8c064 --- /dev/null +++ b/files/zh-cn/web/api/clients/claim/index.html @@ -0,0 +1,66 @@ +--- +title: Clients.claim() +slug: Web/API/Clients/claim +translation_of: Web/API/Clients/claim +--- +<p>{{SeeCompatTable}}{{APIRef("Service Worker Clients")}}</p> + +<p><span class="seoSummary">{{domxref("Clients")}} 接口的 <strong><code>claim()</code></strong> 方法允许一个激活的 service worker 将自己设置为其 {{domxref("ServiceWorkerRegistration.scope", "scope")}} 内所有</span>clients 的<span class="seoSummary">{{domxref("ServiceWorkerContainer.controller", "controller")}} . 这会在由此service worker 控制的任何 clients 中触发 {{domxref("ServiceWorkerContainer","navigator.serviceWorker")}} 上的 "<code>controllerchange</code>" 事件.</span></p> + +<p>当一个 service worker 被初始注册时,页面在下次加载之前不会使用它。 <code>claim()</code> 方法会立即控制这些页面。请注意,这会导致 service worker 控制通过网络定期加载的页面,或者可能通过不同的 service worker 加载.</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">await clients.claim(); +</pre> + +<h3 id="参数">参数</h3> + +<p>None.</p> + +<h3 id="返回">返回</h3> + +<p>A {{jsxref("Promise")}} for <code>void</code>.</p> + +<h2 id="示例">示例</h2> + +<p>The following example uses <code>claim()</code> inside service worker's "<code>activate</code>" event listener so that clients loaded in the same scope do not need to be reloaded before their fetches will go through this service worker.</p> + +<pre class="brush: js">self.addEventListener('activate', event => { + event.waitUntil(clients.claim()); +});</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', '#clients-claim', 'claim()')}}</td> + <td>{{Spec2('Service Workers')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.Clients.claim")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li> + <li><a href="https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle">The service worker lifecycle</a></li> + <li><a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li> + <li>{{jsxref("Promise", "Promises")}}</li> + <li>{{domxref("ServiceWorkerGlobalScope.skipWaiting()", "self.skipWaiting()")}} - skip the service worker's waiting phase </li> +</ul> diff --git a/files/zh-cn/web/api/clients/get/index.html b/files/zh-cn/web/api/clients/get/index.html new file mode 100644 index 0000000000..3a9d732f63 --- /dev/null +++ b/files/zh-cn/web/api/clients/get/index.html @@ -0,0 +1,60 @@ +--- +title: Clients.get() +slug: Web/API/Clients/get +translation_of: Web/API/Clients/get +--- +<div>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</div> + +<p>{{domxref("Clients")}} 接口的 <code><strong>get</strong></code><strong><code>()</code></strong> 方法 获取给定 <code>id</code> 匹配的Service Worker client,并在 {{jsxref("Promise")}} 中返回它.</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">self.clients.get(id).then(function(client) { + // do something with your returned client +});</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>id</code></dt> + <dd>一个 {{domxref("DOMString")}} ,表示您想要获取的 client id.</dd> +</dl> + +<h3 id="返回">返回</h3> + +<dl> + <dd>一个resolve为 {{domxref("Client")}} 对象的 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" title="The Promise object is used for deferred and asynchronous computations. A Promise is in one of the three states:">Promise</a></code> .</dd> +</dl> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">self.clients.get(options).then(function(client) { + self.clients.openWindow(client.url); +});</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', '#clients-get-method', 'get()')}}</td> + <td>{{Spec2('Service Workers')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.Clients.get")}}</p> +</div> + +<p> </p> diff --git a/files/zh-cn/web/api/clients/index.html b/files/zh-cn/web/api/clients/index.html new file mode 100644 index 0000000000..713e753621 --- /dev/null +++ b/files/zh-cn/web/api/clients/index.html @@ -0,0 +1,90 @@ +--- +title: Clients +slug: Web/API/Clients +translation_of: Web/API/Clients +--- +<p>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</p> + +<p><span class="seoSummary"><code>Clients</code> 接口提供对 {{domxref("Client")}} 对象的访问. 通过在 <a href="/en-US/docs/Web/API/ServiceWorker_API">service worker</a> 中使用 <code>{{domxref("ServiceWorkerGlobalScope", "self")}}.clients</code> 访问它.</span></p> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{domxref("Clients.get()")}}</dt> + <dd>返回一个匹配给定 {{domxref("Client.id", "id")}} 的 {{domxref("Client")}} 的 {{jsxref("Promise")}} .</dd> + <dt>{{domxref("Clients.matchAll()")}}</dt> + <dd>返回一个 {{domxref("Client")}} 对象数组的 {{jsxref("Promise")}} . options参数允许您控制返回的clients类型. </dd> + <dt>{{domxref("Clients.openWindow()")}}</dt> + <dd>打开给定URL的新浏览器窗口,并返回新 {{domxref("WindowClient")}} a 的 {{jsxref("Promise")}} .</dd> + <dt>{{domxref("Clients.claim()")}}</dt> + <dd>允许一个激活的 service worker 将自己设置为其{{domxref("ServiceWorkerRegistration.scope", "scope")}} 内所有 clients 的 {{domxref("ServiceWorkerContainer.controller", "controller")}} . </dd> +</dl> + +<h2 id="示例">示例</h2> + +<p>下面示例显示一个已有的聊天窗口,或者当用户点击通知时创建新的窗口.</p> + +<pre class="brush: js">addEventListener('notificationclick', event => { + event.waitUntil(async function() { + const allClients = await clients.matchAll({ + includeUncontrolled: true + }); + + let chatClient; + + // Let's see if we already have a chat window open: + for (const client of allClients) { + const url = new URL(client.url); + + if (url.pathname == '/chat/') { + // Excellent, let's use it! + client.focus(); + chatClient = client; + break; + } + } + + // If we didn't find an existing chat window, + // open a new one: + if (!chatClient) { + chatClient = await clients.openWindow('/chat/'); + } + + // Message the client: + chatClient.postMessage("New chat messages!"); + }()); +}); +</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', '#clients', 'Clients')}}</td> + <td>{{Spec2('Service Workers')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.Clients")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li> + <li><a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li> + <li>{{jsxref("Promise")}}</li> +</ul> diff --git a/files/zh-cn/web/api/clients/matchall/index.html b/files/zh-cn/web/api/clients/matchall/index.html new file mode 100644 index 0000000000..1a07e1456c --- /dev/null +++ b/files/zh-cn/web/api/clients/matchall/index.html @@ -0,0 +1,68 @@ +--- +title: Clients.matchAll() +slug: Web/API/Clients/matchAll +translation_of: Web/API/Clients/matchAll +--- +<div>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</div> + +<p>{{domxref("Clients")}} 接口的 <strong><code>matchAll()</code></strong> 方法返回 service worker {{domxref("Client")}} 对象列表的 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" title="The Promise object is used for deferred and asynchronous computations. A Promise is in one of the three states:">Promise</a></code> . 包含 <code style="font-style: normal;">options</code> 参数以返回域与关联的 service worker 的域相同所有 service worker 的 clients. 如果未包含 options,该方法仅返回由service worker控制的 service worker clients.</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">ServiceWorkerClients.matchAll(options).then(function(clients) { + // do something with your clients list +});</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>options {{optional_inline}}</code></dt> + <dd>一个options对象,允许您为匹配操作设置选项。 可用选项包括: + <ul> + <li><code>includeUncontrolled</code>: {{domxref("Boolean")}} — 如果设置为<code>true</code>, 匹配操作将返回与当前服务工作者共享相同源的所有服务工作者客户端。 否则,它仅返回由当前服务工作者控制的服务工作者客户端。 默认值为<code>false</code>.</li> + <li><code>type</code>: 设置想要匹配的 clients 类型. 可用值包括 <code>window</code>, <code>worker</code>, <code>sharedworker</code>, 和 <code>all</code>. 默认是 <code>all</code>.</li> + </ul> + </dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dd>resolve为一个 {{domxref("Client")}} 对象数组的 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" title="The Promise object is used for deferred and asynchronous computations. A Promise is in one of the three states:">Promise</a></code> . 在 Chrome 46/Firefox 54 以及更高版本中, 该方法以最近关注的顺序返回 clients , 根据规范更正.</dd> +</dl> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">clients.matchAll(options).then(function(clientList) { + for (var i = 0 ; i < clients.length ; i++) { + if (clientList[i].url === 'index.html') { + clients.openWindow(clientList[i]); + // or do something else involving the matching client + } + } +});</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', '#clients', 'Clients')}}</td> + <td>{{Spec2('Service Workers')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.Clients.matchAll")}}</p> +</div> diff --git a/files/zh-cn/web/api/clients/openwindow/index.html b/files/zh-cn/web/api/clients/openwindow/index.html new file mode 100644 index 0000000000..18a85d7538 --- /dev/null +++ b/files/zh-cn/web/api/clients/openwindow/index.html @@ -0,0 +1,81 @@ +--- +title: Clients.openWindow() +slug: Web/API/Clients/openWindow +translation_of: Web/API/Clients/openWindow +--- +<p>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</p> + +<p>{{domxref("Clients")}}接口的 <strong><code>openWindow()</code></strong> 方法创建一个顶级的浏览器上下文并加载给定的 URL. 如果调用脚本没有显示弹出窗口的权限, <strong><code>openWindow() </code></strong>将抛出 InvalidAccessError.</p> + +<p>在Firefox中,只有在作为通知点击事件的结果调用时,才允许该方法显示弹出窗口.</p> + +<p>在Chrome for Android中,该方法可以改为在先前添加到用户主屏幕的 <a href="https://developer.mozilla.org/en-US/Apps/Progressive/Installable">standalone web app</a> 提供的现有浏览上下文中打开URL.</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">ServiceWorkerClients.openWindow(url).then(function(WindowClient) { + // do something with your WindowClient +});</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>url</code></dt> + <dd>一个 {{domxref("USVString")}} ,表示要在窗口中打开的client的URL。 通常,此值必须是与调用脚本有相同域的URL.</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dd>如果URL来自与服务工作者相同的域,则resolve为 {{domxref("WindowClient")}} 对象的Promise,否则resolve为 {{Glossary("null", "null value")}} .</dd> +</dl> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">// When the user clicks a notification focus the window if it exists or open +// a new one otherwise. +onotificationclick = function(event) { + var found = false; + clients.matchAll().then(function(clientsArr) { + for (i = 0; i < clientsArr.length; i++) { + if (clientsArr[i].url === event.data.url) { + // We already have a window to use, focus it. + found = true; + clientsArr[i].focus(); + break; + } + } + if (!found) { + // Create a new window. + clients.openWindow(event.data.url).then(function(windowClient) { + // do something with the windowClient. + }); + } + }); +}; +</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', '#clients', 'Clients')}}</td> + <td>{{Spec2('Service Workers')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.Clients.openWindow")}}</p> +</div> |