WindowClient

Baseline 廣泛可用 *

此功能已成熟,可跨多種裝置和瀏覽器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有瀏覽器中可用。

* 此特性的某些部分可能存在不同級別的支援。

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

WindowClient 介面是 ServiceWorker API 的一部分,它表示一個由活動工作執行緒控制的、屬於瀏覽上下文中的文件的 Service Worker 客戶端範圍。Service Worker 客戶端獨立地為自身的載入和子資源選擇並使用一個 Service Worker。

Client WindowClient

例項方法

WindowClient 從其父介面 Client 繼承了方法。

WindowClient.focus()

將使用者輸入焦點給予當前客戶端。

WindowClient.navigate()

將指定的 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

瀏覽器相容性

另見