blob: d4365b320eddbf706aa3f3e28f324ed71c793f6b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
---
title: Clients
slug: Web/API/Clients
tags:
- API
- Clients
- Experimental
- Interface
- NeedsTranslation
- Reference
- Service Workers
- Service worker API
- ServiceWorker
- TopicStub
- Workers
translation_of: Web/API/Clients
---
<p>{{SeeCompatTable}}{{APIRef("Service Workers API")}}</p>
<p><span class="seoSummary">The <code>Clients</code> interface provides access to {{domxref("Client")}} objects. Access it via <code>{{domxref("ServiceWorkerGlobalScope", "self")}}.clients</code> within a <a href="/en-US/docs/Web/API/ServiceWorker_API">service worker</a>.</span></p>
<h2 id="Methods">Methods</h2>
<dl>
<dt>{{domxref("Clients.get()")}}</dt>
<dd>Returns a {{jsxref("Promise")}} for a {{domxref("Client")}} matching a given {{domxref("Client.id", "id")}}.</dd>
<dt>{{domxref("Clients.matchAll()")}}</dt>
<dd>Returns a {{jsxref("Promise")}} for an array of {{domxref("Client")}} objects. An options argument allows you to control the types of clients returned. </dd>
<dt>{{domxref("Clients.openWindow()")}}</dt>
<dd>Opens a new browser window for a given url and returns a {{jsxref("Promise")}} for the new {{domxref("WindowClient")}}.</dd>
<dt>{{domxref("Clients.claim()")}}</dt>
<dd>Allows an active service worker to set itself as the {{domxref("ServiceWorkerContainer.controller", "controller")}} for all clients within its {{domxref("ServiceWorkerRegistration.scope", "scope")}}. </dd>
</dl>
<h2 id="Examples">Examples</h2>
<p>The following example shows an existing chat window or creates a new one when the user clicks a notification.</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="Specifications">Specifications</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="Browser_compatibility">Browser compatibility</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>
|