From 531276eda91ab4025a48290642a57d4e06d2f019 Mon Sep 17 00:00:00 2001 From: Jon Wood Date: Mon, 15 Apr 2024 19:44:41 +0000 Subject: [PATCH] Formatting --- js/notifications.js | 48 +++++++++++++++++++++++---------------------- js/serviceworker.js | 2 +- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/js/notifications.js b/js/notifications.js index e4454d656..0c11d263d 100644 --- a/js/notifications.js +++ b/js/notifications.js @@ -1,33 +1,35 @@ async function enableNotifications(event) { - let vapid_key = document.querySelector("meta[name=vapid_key]").getAttribute("value"); - let worker = await navigator.serviceWorker.ready; - let result = await worker.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey: vapid_key, - }); + let vapid_key = document.querySelector("meta[name=vapid_key]").getAttribute("value"); + let worker = await navigator.serviceWorker.ready; + let result = await worker.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: vapid_key, + }); - let response = await fetch("/account/notifications/register", { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(result.toJSON()) - }); + let response = await fetch("/account/notifications/register", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(result.toJSON()) + }); } async function checkPermissions() { - let worker = await navigator.serviceWorker.ready; + let worker = await navigator.serviceWorker.ready; - if ("pushManager" in worker) { - let permissions = await worker.pushManager.permissionState(); - document.getElementById('notification-state').querySelectorAll('.state').forEach(el => { el.classList.add('visible') }) - document.getElementById(`notification-state-${permissions}`).classList.add('visible') - } else { - console.log("No push notification support."); - } + if ("pushManager" in worker) { + let permissions = await worker.pushManager.permissionState({ + userVisibleOnly: true, + }); + document.getElementById(`notification-state-${permissions}`).classList.add('visible') + } else { + document.getElementById(`notification-state-unsupported`).classList.add('visible'); + console.log("No push notification support."); + } } if ("serviceWorker" in navigator) { - checkPermissions(); - document.getElementById("enable-notifications").addEventListener("click", enableNotifications); + checkPermissions(); + document.getElementById("enable-notifications").addEventListener("click", enableNotifications); } diff --git a/js/serviceworker.js b/js/serviceworker.js index c1aa0492b..80ea0042e 100644 --- a/js/serviceworker.js +++ b/js/serviceworker.js @@ -72,7 +72,7 @@ registerRoute( addEventListener("push", (event) => { console.log("Push event received", event); - const message = event.data.text() + const message = event.data.text(); self.registration.showNotification(message); });