| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| self.addEventListener('push', (event) => { |
| let data = {}; |
| try { |
| data = event.data ? event.data.json() : {}; |
| } catch (_err) { |
| |
| try { |
| data = { title: 'WorldMonitor', body: event.data ? event.data.text() : '' }; |
| } catch { |
| data = {}; |
| } |
| } |
|
|
| const title = typeof data.title === 'string' && data.title.length > 0 |
| ? data.title |
| : 'WorldMonitor'; |
| const body = typeof data.body === 'string' ? data.body : ''; |
| const url = typeof data.url === 'string' ? data.url : '/'; |
| const tag = typeof data.tag === 'string' ? data.tag : 'worldmonitor-generic'; |
| const icon = typeof data.icon === 'string' |
| ? data.icon |
| : '/favico/android-chrome-192x192.png'; |
| const badge = typeof data.badge === 'string' |
| ? data.badge |
| : '/favico/android-chrome-192x192.png'; |
|
|
| const opts = { |
| body, |
| icon, |
| badge, |
| tag, |
| |
| |
| |
| |
| requireInteraction: data.eventType === 'brief_ready', |
| data: { url, eventType: data.eventType ?? 'unknown' }, |
| }; |
|
|
| event.waitUntil(self.registration.showNotification(title, opts)); |
| }); |
|
|
| self.addEventListener('notificationclick', (event) => { |
| event.notification.close(); |
| const target = (event.notification.data && event.notification.data.url) || '/'; |
| event.waitUntil((async () => { |
| try { |
| const all = await clients.matchAll({ type: 'window', includeUncontrolled: true }); |
| |
| |
| |
| for (const c of all) { |
| try { |
| const sameOrigin = new URL(c.url).origin === self.location.origin; |
| if (sameOrigin && 'focus' in c) { |
| if ('navigate' in c && typeof c.navigate === 'function') { |
| await c.navigate(target); |
| } |
| return c.focus(); |
| } |
| } catch { |
| |
| } |
| } |
| if (clients.openWindow) return clients.openWindow(target); |
| } catch { |
| |
| } |
| })()); |
| }); |
|
|