Clients: matchAll() 方法

Baseline 已广泛支持

此功能已成熟,可跨多种设备和浏览器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有浏览器中可用。

注意:此功能仅在 Service Workers 中可用。

Clients 接口的 matchAll() 方法返回一个 Promise,该 Promise 解析为一组服务工作线程 Client 对象。包含 options 参数可返回与关联服务工作线程具有相同源的、所有服务工作线程客户端。如果未包含 options,该方法仅返回由该服务工作线程控制的服务工作线程客户端。

语法

js
matchAll()
matchAll(options)

参数

options 可选

一个 options 对象,允许您为匹配操作设置选项。可用选项包括:

includeUncontrolled

一个布尔值——如果设置为 true,则匹配操作将返回与当前服务工作线程具有相同源的所有服务工作线程客户端。否则,它将仅返回由当前服务工作线程控制的服务工作线程客户端。默认为 false

type

设置您想要匹配的客户端类型。可用值包括 "window""worker""sharedworker""all"。默认为 "window"

返回值

一个 Promise,解析为 Client 对象数组。在 Chrome 46/Firefox 54 及更高版本中,此方法按最近聚焦的顺序返回客户端,符合规范。

示例

js
clients.matchAll(options).then((clientList) => {
  for (const client of clientList) {
    if (client.url === "index.html") {
      clients.openWindow(client);
      // or do something else involving the matching client
    }
  }
});

规范

规范
Service Workers
# clients-matchall

浏览器兼容性