sw.js 954 B

1234567891011121314151617181920212223242526272829303132
  1. /* eslint-env serviceworker */
  2. self.addEventListener('push', function (event) {
  3. if (event.data) {
  4. const data = event.data.json()
  5. const promiseChain = clients.matchAll({
  6. includeUncontrolled: true
  7. }).then(function (clientList) {
  8. const list = clientList.filter((item) => item.type === 'window')
  9. if (list.length) return
  10. return self.registration.showNotification(data.title, data)
  11. })
  12. event.waitUntil(promiseChain)
  13. }
  14. })
  15. self.addEventListener('notificationclick', function (event) {
  16. event.notification.close()
  17. event.waitUntil(clients.matchAll({
  18. includeUncontrolled: true
  19. }).then(function (clientList) {
  20. const list = clientList.filter((item) => item.type === 'window')
  21. for (var i = 0; i < list.length; i++) {
  22. var client = list[i]
  23. if (client.url === '/' && 'focus' in client) { return client.focus() }
  24. }
  25. if (clients.openWindow) { return clients.openWindow('/') }
  26. }))
  27. })