Clients
interface provides access to
Client
objects. Access it via
在
service worker
.
self
.clients
Clients.get()
Promise
对于
Client
matching a given
id
.
Clients.matchAll()
Promise
for an array of
Client
objects. An options argument allows you to control the types of clients returned.
Clients.openWindow()
Promise
for the new
WindowClient
.
Clients.claim()
controller
for all clients within its
scope
.
The following example shows an existing chat window or creates a new one when the user clicks a notification.
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!");
}());
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
服务工作者
The definition of 'Clients' in that specification. |
工作草案 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Clients
|
Chrome 40 | Edge ≤79 |
Firefox
44
注意事项
|
IE 不支持 No | Opera 27 | Safari 不支持 No | WebView Android 40 | Chrome Android 40 | Firefox Android 44 | Opera Android 27 | Safari iOS 不支持 No | Samsung Internet Android 4.0 |
claim
|
Chrome 42 | Edge ≤79 |
Firefox
44
注意事项
|
IE 不支持 No | Opera 29 | Safari 不支持 No | WebView Android 42 | Chrome Android 42 | Firefox Android 44 | Opera Android 29 | Safari iOS 不支持 No | Samsung Internet Android 4.0 |
get
|
Chrome 51 | Edge ≤79 |
Firefox
45
注意事项
|
IE 不支持 No | Opera 38 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 51 | Firefox Android 45 | Opera Android 41 | Safari iOS 不支持 No | Samsung Internet Android 5.0 |
matchAll
|
Chrome
47
注意事项
|
Edge
≤79
注意事项
|
Firefox
44
注意事项
|
IE 不支持 No | Opera 32 | Safari 不支持 No |
WebView Android
47
注意事项
|
Chrome Android
47
注意事项
|
Firefox Android
44
|
Opera Android 32 | Safari iOS 不支持 No |
Samsung Internet Android
4.0
注意事项
|
openWindow
|
Chrome
40
|
Edge
≤79
|
Firefox
45
注意事项
|
IE 不支持 No | Opera 38 | Safari 不支持 No |
WebView Android
40
|
Chrome Android
40
|
Firefox Android 45 | Opera Android 41 | Safari iOS 不支持 No |
Samsung Internet Android
4.0
|
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
Clients
缓存
CacheStorage
Client
ExtendableEvent
FetchEvent
InstallEvent
Navigator.serviceWorker
NotificationEvent
PeriodicSyncEvent
PeriodicSyncManager
PeriodicSyncRegistration
ServiceWorker
ServiceWorkerContainer
ServiceWorkerGlobalScope
ServiceWorkerRegistration
SyncEvent
SyncManager
SyncRegistration
WindowClient