// Scope is basically the directory of the workerURL inside the domain (starting and ending with /) // If your scope is too short, Firefox will say "unsafe operation" // and Chrome will give a proper error message sS hRegisterServiceWorker(S workerURL, S scope) { ret hjs(replaceDollarVars(javaScript_hashLocalIdentifiers("serviceWorker_", [[ 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. console.log("out of await requestPermission"); console.log("permission: " + permission); if (permission !== 'granted') { throw new Error('Permission not granted for Notification') } }; const #registerServiceWorker = async () => { console.log("registerServiceWorker " + $workerURL); const swRegistration = await navigator.serviceWorker.register($workerURL, {scope: $scope}); console.log("out of await swRegistration"); return swRegistration; }; const #showLocalNotification = (title, body, swRegistration) => { swRegistration.showNotification(title, { body }); } const #main = async () => { console.log("registerServiceWorker starting"); check(); const swRegistration = await registerServiceWorker(); console.log("Service worker registered: " + swRegistration); const permission = await requestNotificationPermission(); console.log("Out of await requestNotificationPermission"); console.log("Notification permission: " + permission); //showLocalNotification('Demo notification', 'Hello user', swRegistration); }; main(); ]]), workerURL := jsQuote(workerURL), scope := jsQuote(scope)); }