Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

52
LINES

< > BotCompany Repo | #1031425 // hRegisterServiceWorker (OK)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3666L/22K).

// 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));
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031425
Snippet name: hRegisterServiceWorker (OK)
Eternal ID of this version: #1031425/13
Text MD5: f030974e2085547eb8f26c57e432899a
Transpilation MD5: 529e7bf9116a4adc5231c7d33ccac64c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-12 08:32:00
Source code size: 2263 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 104 / 192
Version history: 12 change(s)
Referenced in: [show references]