timesafari
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

111 lines
3.7 KiB

/* eslint-env serviceworker */
/* global workbox */
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js",
);
self.addEventListener("install", (event) => {
console.log("Service worker got install. Importing scripts...", event);
importScripts(
"safari-notifications.js",
"nacl.js",
"noble-curves.js",
"noble-hashes.js",
);
console.log("Service worker imported scripts.");
});
function logConsoleAndDb(message, arg1, arg2) {
console.log(`${new Date().toISOString()} ${message}`, arg1, arg2);
let fullMessage = `${new Date().toISOString()} ${message}`;
if (arg1) {
fullMessage += `\n${JSON.stringify(arg1)}`;
}
if (arg2) {
fullMessage += `\n${JSON.stringify(arg2)}`;
}
self.appendDailyLog(fullMessage);
}
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 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 = "Generic Notification";
let message = "Got some empty message.";
if (payload && payload.title == DIRECT_PUSH_TITLE) {
// skip any search logic and show the message directly
title = "Direct Message";
message = payload.message || "No details were provided.";
} else {
title = payload && payload.title ? payload.title : "Note";
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("activate", (event) => {
logConsoleAndDb("Service worker 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("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);