movmaker-webui/sw.js

25 lines
1 KiB
JavaScript

self.addEventListener('push', event => {
let data = {};
try { data = event.data ? event.data.json() : {}; } catch (e) { data = { title: 'MVL', body: event.data ? event.data.text() : 'New article @ MVL!' }; }
const title = data.title || 'MVL';
const options = {
body: data.body || 'New article @ MVL!',
icon: 'assets/img/moto_travel.png',
badge: 'assets/img/moto_travel.png',
data: { url: data.url || './' },
tag: data.tag || 'mvlog-new-video',
renotify: true
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener('notificationclick', event => {
event.notification.close();
const url = event.notification.data && event.notification.data.url ? event.notification.data.url : './';
event.waitUntil(clients.matchAll({ type: 'window', includeUncontrolled: true }).then(clientList => {
for (const client of clientList) {
if (client.url === url && 'focus' in client) return client.focus();
}
return clients.openWindow(url);
}));
});