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/openwindow/index.html | 90 ++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 files/ja/web/api/clients/openwindow/index.html (limited to 'files/ja/web/api/clients/openwindow') diff --git a/files/ja/web/api/clients/openwindow/index.html b/files/ja/web/api/clients/openwindow/index.html new file mode 100644 index 0000000000..158e38970e --- /dev/null +++ b/files/ja/web/api/clients/openwindow/index.html @@ -0,0 +1,90 @@ +--- +title: Clients.openWindow() +slug: Web/API/Clients/openWindow +tags: + - API + - Clients + - Method + - Reference + - Service Workers + - ServiceWorker + - openWindow +translation_of: Web/API/Clients/openWindow +--- +
{{APIRef("Service Workers API")}}
+ +

{{domxref("Clients")}} インターフェイスの openWindow() メソッドは、新しい最上位の閲覧コンテキストを作成し、所与の URL をロードします。 呼び出し元のスクリプトにポップアップを表示するパーミッションがない場合、openWindow()InvalidAccessError をスローします。

+ +

Firefox では、このメソッドは、通知クリックイベントの結果として呼び出された場合にのみ、ポップアップを表示できます。

+ +

Android 版 Chrome では、メソッドは代わりに、以前にユーザーのホーム画面に追加されたスタンドアロンのウェブアプリによって提供される既存の閲覧コンテキストで URL を開く場合があります。 最近では、これは Windows 版 Chrome でも機能します。

+ +

構文

+ +
self.clients.openWindow(url).then(function(windowClient) {
+  // WindowClient で何かをします
+});
+ +

パラメーター

+ +
+
url
+
ウィンドウで開くクライアントの URL を表す {{domxref("USVString")}}。 通常、この値は呼び出し元のスクリプトと同じオリジンからの URL でなければなりません。
+
+ +

戻り値

+ +
+
URL がサービスワーカーと同じオリジンからのものである場合は {{domxref("WindowClient")}} オブジェクトに解決され、それ以外の場合は {{Glossary("null", "null 値")}}に解決される {{jsxref("Promise")}}。
+
+ +

+ +
// 適切な場合は OS に通知を送ります
+if (self.Notification.permission === 'granted') {
+  const notificationObject = {
+    body: 'ここをクリックしてメッセージを表示してください。',
+    data: { url: self.location.origin + '/some/path' },
+    // data: { url: 'http://example.com' },
+  };
+  self.registration.showNotification('メッセージがあります!', notificationObject);
+}
+
+// 通知クリックイベントリスナー
+self.addEventListener('notificationclick', e => {
+  // 通知ポップアウトを閉じます
+  e.notification.close();
+  // すべての Window クライアントを取得します
+  e.waitUntil(clients.matchAll({ type: 'window' }).then(clientsArr => {
+    // 対象 URL に一致するウィンドウタブが既に存在する場合は、それにフォーカスします。
+    const hadWindowToFocus = clientsArr.some(windowClient => windowClient.url === e.notification.data.url ? (windowClient.focus(), true) : false);
+    // それ以外の場合は、適切な URL への新しいタブを開いてフォーカスします。
+    if (!hadWindowToFocus) clients.openWindow(e.notification.data.url).then(windowClient => windowClient ? windowClient.focus() : null);
+  }));
+});
+
+ +

仕様

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

ブラウザーの互換性

+ +
+ + +

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

+
-- cgit v1.2.3-54-g00ecf