WindowClient
        
        
          
                Baseline
                
                  Widely available
                
                 *
              
        
        
        
          
                
              
                
              
                
              
        
        
      
      This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2018年4月.
* Some parts of this feature may have varying levels of support.
WindowClient はサービスワーカー API のインターフェイスで、アクティブなワーカーによって制御される閲覧コンテキスト内のドキュメントである、サービスワーカークライアントのスコープを表します。 サービスワーカークライアントは、それ自身の読み込みとサブリソースに対してサービスワーカーを個別に選択して使用します。
インスタンスメソッド
WindowClient には、親インターフェイスである Client から継承したメソッドがあります。
- WindowClient.focus()
- 
現在のクライアントにユーザー入力フォーカスを与えます。 
- 
指定された URL を制御されたクライアントのページに読み込みます。 
インスタンスプロパティ
WindowClient には、親インターフェイスである Client から継承したプロパティがあります。
- WindowClient.focused読取専用
- 
論理値で、現在のクライアントにフォーカスがあるかどうかを示します。 
- WindowClient.visibilityState読取専用
- 
現在のクライアントの可視性を示します。 この値は、 "hidden"、"visible"、"prerender"のいずれかです。
例
js
self.addEventListener("notificationclick", (event) => {
  console.log("On notification click: ", event.notification.tag);
  event.notification.close();
  // これは、クライアントが既に開いているかどうかを確認し、
  // 開いている場合にフォーカスを合わせます
  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("/");
      }),
  );
});
仕様書
| Specification | 
|---|
| Service Workers> # windowclient> | 
ブラウザーの互換性
Loading…