/* eslint-env serviceworker */ /* global workbox */ importScripts( "https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js", ); function logConsoleAndDb(message, arg1, arg2) { // in chrome://serviceworker-internals note that the arg1 and arg2 here will show as "[object Object]" in that page but will show as expandable objects in the console console.log(`${new Date().toISOString()} ${message}`, arg1, arg2); if (self.appendDailyLog) { let fullMessage = `${new Date().toISOString()} ${message}`; if (arg1) { fullMessage += `\n${JSON.stringify(arg1)}`; } if (arg2) { fullMessage += `\n${JSON.stringify(arg2)}`; } self.appendDailyLog(fullMessage); } else { // sometimes we get the error: "Uncaught TypeError: self.appendDailyLog is not a function" console.log("Not logging to DB because self.appendDailyLog doesn't exist."); } } self.addEventListener("install", async (event) => { console.log("Service worker got install event. Importing scripts...", event); await importScripts( "safari-notifications.js", "nacl.js", "noble-curves.js", "noble-hashes.js", ); // this should now be available logConsoleAndDb("Service worker imported all scripts."); }); self.addEventListener("activate", (event) => { logConsoleAndDb("Service worker is activating...", event); // see https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim // and https://web.dev/articles/service-worker-lifecycle#clientsclaim event.waitUntil(clients.claim()); logConsoleAndDb("Service worker is activated."); }); self.addEventListener("push", function (event) { let text = null; if (event.data) { text = event.data.text(); } logConsoleAndDb("Service worker received a push event.", text, event); event.waitUntil( (async () => { try { let payload; if (text) { try { payload = JSON.parse(text); } catch (e) { // don't use payload since it is not JSON } } // This is a special value that tells the service worker that it's a daily update. const DAILY_UPDATE_TITLE = "DAILY_UPDATE"; // This is a special value that tells the service worker to send a direct notification to the device, skipping filters. // This is shared with the notification-test code and should be a constant. Look for the same name in HelpNotificationsView.vue // Use something other than "Daily Update" https://gitea.anomalistdesign.com/trent_larson/py-push-server/src/commit/3c0e196c11bc98060ec5934e99e7dbd591b5da4d/app.py#L213 const DIRECT_PUSH_TITLE = "DIRECT_NOTIFICATION"; let title; let message = "Got some empty message."; if (payload && payload.title == DIRECT_PUSH_TITLE) { // skip any search logic and show the message directly title = "Direct Notification"; message = payload.message || "No details were provided."; } else { // any other title will run through regular filtering logic if (payload && payload.title === DAILY_UPDATE_TITLE) { title = "Daily Update"; } else { title = payload.title || "Update"; } message = await self.getNotificationCount(); } if (message) { const options = { body: message, icon: payload ? payload.icon : "icon.png", badge: payload ? payload.badge : "badge.png", }; await self.registration.showNotification(title, options); logConsoleAndDb("Notified user:", options); } else { logConsoleAndDb("No notification message."); } } catch (error) { logConsoleAndDb("Error with push event", event, error); } })(), ); }); self.addEventListener("message", (event) => { logConsoleAndDb("Service worker got a message...", event); if (event.data && event.data.type === "SEND_LOCAL_DATA") { self.secret = event.data.data; event.ports[0].postMessage({ success: true }); } logConsoleAndDb("Service worker posted a message."); }); self.addEventListener("fetch", (event) => { logConsoleAndDb("Service worker got fetch event.", event); }); self.addEventListener("error", (event) => { logConsoleAndDb("Service worker error", event); console.error("Full Error:", event); console.error("Message:", event.message); console.error("File:", event.filename); console.error("Line:", event.lineno); console.error("Column:", event.colno); console.error("Error Object:", event.error); }); workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);