diff --git a/src/main/resources/web/auth.js b/src/main/resources/web/auth.js index 4f9fb23..f233ae6 100644 --- a/src/main/resources/web/auth.js +++ b/src/main/resources/web/auth.js @@ -1,4 +1,6 @@ -navigator.serviceWorker.register("sw.js"); +// SW +navigator.serviceWorker.register("sw.js") +new BroadcastChannel('viaaas-notification').addEventListener("message", e => handleSWMsg(e)); // Minecraft.id let urlParams = new URLSearchParams(); @@ -296,7 +298,6 @@ function handleSWMsg(event) { if (callback == null) return; callback(data.action); } -navigator.serviceWorker.addEventListener("message", handleSWMsg); function authNotification(msg, yes, no) { if ((!pageBlur && !document.hidden) || Notification.permission != "granted") { if (confirm(msg)) yes(); else no(); diff --git a/src/main/resources/web/sw.js b/src/main/resources/web/sw.js index 7bd7e13..dcdbc50 100644 --- a/src/main/resources/web/sw.js +++ b/src/main/resources/web/sw.js @@ -1,6 +1,8 @@ +// https://stackoverflow.com/questions/42127148/service-worker-communicate-to-clients +let viac = new BroadcastChannel('viaaas-notification'); self.addEventListener("notificationclick", event => { console.log("On notification click: " + event); event.preventDefault(); event.notification.close(); - self.clients.matchAll({type: "window"}).then(it => it.forEach(c => c.postMessage({tag: event.notification.tag, action: event.action}))); + viac.postMessage({tag: event.notification.tag, action: event.action}); });