yandere_fe/src/sw.js

39 lines
1.0 KiB
JavaScript
Raw Normal View History

/* eslint-env serviceworker */
2018-12-12 09:03:50 -08:00
import localForage from 'localforage'
function isEnabled () {
return localForage.getItem('vuex-lz')
.then(data => data.config.webPushNotifications)
}
2018-12-09 04:25:43 -08:00
function getWindowClients () {
return clients.matchAll({ includeUncontrolled: true })
.then((clientList) => clientList.filter(({ type }) => type === 'window'))
}
self.addEventListener('push', (event) => {
if (event.data) {
2018-12-12 09:03:50 -08:00
event.waitUntil(isEnabled().then((isEnabled) => {
return isEnabled && getWindowClients().then((list) => {
const data = event.data.json()
2018-12-12 09:03:50 -08:00
if (list.length === 0) return self.registration.showNotification(data.title, data)
})
2018-12-09 04:25:43 -08:00
}))
}
})
2018-12-09 04:25:43 -08:00
self.addEventListener('notificationclick', (event) => {
event.notification.close()
2018-12-09 04:25:43 -08:00
event.waitUntil(getWindowClients().then((list) => {
for (var i = 0; i < list.length; i++) {
var client = list[i]
if (client.url === '/' && 'focus' in client) { return client.focus() }
}
2018-12-09 04:25:43 -08:00
if (clients.openWindow) return clients.openWindow('/')
}))
})