use broadcastchannel

This commit is contained in:
creeper123123321 2021-03-07 14:24:47 -03:00
parent c6a0496927
commit ab67c699b8
2 changed files with 6 additions and 3 deletions

View File

@ -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();

View File

@ -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});
});