mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-03-21 18:04:39 -05:00
31 lines
917 B
JavaScript
31 lines
917 B
JavaScript
self.addEventListener("fetch", () => {
|
|
return;
|
|
});
|
|
|
|
self.addEventListener("push", (event) => {
|
|
const { title, ...options } = JSON.parse(event.data.text());
|
|
|
|
event.waitUntil(self.registration.showNotification(title, options));
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
const targetUrl = event.notification.data.url;
|
|
event.notification.close(); // Android needs explicit close.
|
|
event.waitUntil(
|
|
clients.matchAll({ type: "window" }).then((windowClients) => {
|
|
// Check if there is already a window/tab open with the target URL
|
|
for (let i = 0; i < windowClients.length; i++) {
|
|
const client = windowClients[i];
|
|
// If so, just focus it.
|
|
if (client.url === targetUrl && "focus" in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
// If not, then open the target URL in a new window/tab.
|
|
if (clients.openWindow) {
|
|
return clients.openWindow(targetUrl);
|
|
}
|
|
}),
|
|
);
|
|
});
|