mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-05-06 21:42:52 +02:00
60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
/* Service worker for PWA installability and Web Push notifications. */
|
|
|
|
self.addEventListener("install", () => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(self.clients.claim());
|
|
});
|
|
|
|
// No-op fetch handler — required for PWA installability criteria.
|
|
// We don't cache anything; the app always fetches from the network.
|
|
self.addEventListener("fetch", () => {});
|
|
|
|
self.addEventListener("push", (event) => {
|
|
let data = {};
|
|
try {
|
|
data = event.data ? event.data.json() : {};
|
|
} catch {
|
|
data = { title: "New message", body: event.data?.text() || "" };
|
|
}
|
|
|
|
const title = data.title || "New message";
|
|
const options = {
|
|
body: data.body || "",
|
|
icon: "./favicon-256x256.png",
|
|
badge: "./favicon-96x96.png",
|
|
tag: data.tag || "meshcore-push",
|
|
data: { url_hash: data.url_hash || "" },
|
|
};
|
|
|
|
event.waitUntil(self.registration.showNotification(title, options));
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close();
|
|
const urlHash = event.notification.data?.url_hash || "";
|
|
|
|
event.waitUntil(
|
|
clients
|
|
.matchAll({ type: "window", includeUncontrolled: true })
|
|
.then((windowClients) => {
|
|
// Focus an existing tab if one is open
|
|
for (const client of windowClients) {
|
|
if (client.url.includes(self.location.origin)) {
|
|
client.focus();
|
|
if (urlHash) {
|
|
client.navigate(self.location.origin + "/" + urlHash);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
// Otherwise open a new tab
|
|
return clients.openWindow(
|
|
self.location.origin + "/" + (urlHash || "")
|
|
);
|
|
})
|
|
);
|
|
});
|