From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/web/api/clients/index.html | 99 +++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 files/ja/web/api/clients/index.html (limited to 'files/ja/web/api/clients/index.html') diff --git a/files/ja/web/api/clients/index.html b/files/ja/web/api/clients/index.html new file mode 100644 index 0000000000..2bb3c9a388 --- /dev/null +++ b/files/ja/web/api/clients/index.html @@ -0,0 +1,99 @@ +--- +title: Clients +slug: Web/API/Clients +tags: + - API + - Clients + - Interface + - Reference + - Service Workers + - Service worker API + - ServiceWorker + - Workers +translation_of: Web/API/Clients +--- +

{{APIRef("Service Workers API")}}

+ +

Clients インターフェイスは、{{domxref("Client")}} オブジェクトへのアクセスを提供します。 これは、サービスワーカー内で {{domxref("ServiceWorkerGlobalScope", "self")}}.clients を介してアクセスします。

+ +

メソッド

+ +
+
{{domxref("Clients.get()")}}
+
指定された {{domxref("Client.id", "id")}} に一致する {{domxref("Client")}} の {{jsxref("Promise")}} を返します。
+
{{domxref("Clients.matchAll()")}}
+
{{domxref("Client")}} オブジェクトの配列の {{jsxref("Promise")}} を返します。 options 引数を使用すると、返されるクライアントの種類を制御できます。
+
{{domxref("Clients.openWindow()")}}
+
指定された URL の新しいブラウザーウィンドウを開き、新しい {{domxref("WindowClient")}} の {{jsxref("Promise")}} を返します。
+
{{domxref("Clients.claim()")}}
+
アクティブなサービスワーカーが自身の {{domxref("ServiceWorkerRegistration.scope", "scope")}} 内のすべてのクライアントの {{domxref("ServiceWorkerContainer.controller", "controller")}} として自分自身を設定できるようにします。
+
+ +

+ +

次の例は、ユーザーが通知をクリックしたときに既存のチャットウィンドウを表示するか、新しいチャットウィンドウを作成します。

+ +
addEventListener('notificationclick', event => {
+  event.waitUntil(async function() {
+    const allClients = await clients.matchAll({
+      includeUncontrolled: true
+    });
+
+    let chatClient;
+
+    // チャットウィンドウが既に開いているかどうかを確認します。
+    for (const client of allClients) {
+      const url = new URL(client.url);
+
+      if (url.pathname == '/chat/') {
+        // よし、使ってみよう!
+        client.focus();
+        chatClient = client;
+        break;
+      }
+    }
+
+    // 既存のチャットウィンドウが見つからなかった場合、
+    // 新しいウィンドウを開きます。
+    if (!chatClient) {
+      chatClient = await clients.openWindow('/chat/');
+    }
+
+    // クライアントにメッセージを送ります。
+    chatClient.postMessage("新しいチャットメッセージ!");
+  }());
+});
+
+ +

仕様

+ + + + + + + + + + + + + + +
仕様状態コメント
{{SpecName('Service Workers', '#clients', 'Clients')}}{{Spec2('Service Workers')}}初期定義
+ +

ブラウザーの互換性

+ +
+ + +

{{Compat("api.Clients")}}

+
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf