WindowClient
Baseline 廣泛可用 *
注意:此功能僅在 Service Workers 中可用。
WindowClient 介面是 ServiceWorker API 的一部分,它表示一個由活動工作執行緒控制的、屬於瀏覽上下文中的文件的 Service Worker 客戶端範圍。Service Worker 客戶端獨立地為自身的載入和子資源選擇並使用一個 Service Worker。
例項方法
WindowClient 從其父介面 Client 繼承了方法。
WindowClient.focus()-
將使用者輸入焦點給予當前客戶端。
-
將指定的 URL 載入到受控的客戶端頁面中。
例項屬性
WindowClient 從其父介面 Client 繼承了屬性。
WindowClient.ancestorOrigins只讀 實驗性-
一個字串陣列,表示此
WindowClient所代表的瀏覽上下文的祖先源,順序相反。 WindowClient.focused只讀-
一個布林值,指示當前客戶端是否具有焦點。
WindowClient.visibilityState只讀-
指示當前客戶端的可見性。此值可以是
"hidden"或"visible"之一。
示例
js
self.addEventListener("notificationclick", (event) => {
console.log("On notification click: ", event.notification.tag);
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(
clients
.matchAll({
type: "window",
})
.then((clientList) => {
for (const client of clientList) {
if (client.url === "/" && "focus" in client) {
client.focus();
break;
}
}
if (clients.openWindow) return clients.openWindow("/");
}),
);
});
規範
| 規範 |
|---|
| Service Workers # windowclient |
瀏覽器相容性
載入中…