[259195] <html><head><title>Service Worker Test</title></head> <body><script src="https://code.jquery.com/...

218 Conversations | 2,860 Posts + (510 from users, 2,350 from bots) | 4 Uploaded Images +

New Post |
| Root Posts | All Posts | Latest Posts | Latest Changes | Main Posts | Team |

By stefan's bot "Safe Code Runner". Created 2020/12/21 17:18:02

Post type: Code Result

Reply | Duplicate | Rename | Raw Text

In reference to:

<html><head><title>Service Worker Test</title></head>
<body><script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><h1>Service Worker Test</h1><script type="text/javascript">
    const check = () => {
      if (!('serviceWorker' in navigator)) {
        throw new Error('No Service Worker support!')
      }
      if (!('PushManager' in window)) {
        throw new Error('No Push API Support!')
      }
    };
    
    const requestNotificationPermission = async () => {
      const permission = await window.Notification.requestPermission()
      // value of permission can be 'granted', 'default', 'denied'
      // granted: user has accepted the request
      // default: user has dismissed the notification permission popup by clicking on x
      // denied: user has denied the request.
      if (permission !== 'granted') {
        throw new Error('Permission not granted for Notification')
      }
    };
    
    const registerServiceWorker = async () => {
      const swRegistration = await navigator.serviceWorker.register('/text/258050?ct=text/javascript', {scope: '/'});
      return swRegistration;
    };
    
    const showLocalNotification = (title, body, swRegistration) => {
      swRegistration.showNotification(title, { body });
    }
    
    const main = async () => {
      check();
      const swRegistration = await registerServiceWorker();
      console.log("Service worker registered: " + swRegistration);
      const permission = await requestNotificationPermission();
      console.log("Notification permission: " + permission);
      //showLocalNotification('Demo notification', 'Hello user', swRegistration);
    };
  
    main();
  </script></body></html>