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).

1  
// Scope is basically the directory of the workerURL inside the domain (starting and ending with /)
2  
// If your scope is too short, Firefox will say "unsafe operation"
3  
// and Chrome will give a proper error message
4  
sS hRegisterServiceWorker(S workerURL, S scope) {
5  
  ret hjs(replaceDollarVars(javaScript_hashLocalIdentifiers("serviceWorker_", [[
6  
    const #check = () => {
7  
      if (!('serviceWorker' in navigator)) {
8  
        throw new Error('No Service Worker support!')
9  
      }
10  
      if (!('PushManager' in window)) {
11  
        throw new Error('No Push API Support!')
12  
      }
13  
    };
14  
    
15  
    const #requestNotificationPermission = async () => {
16  
      const permission = await window.Notification.requestPermission();
17  
      // value of permission can be 'granted', 'default', 'denied'
18  
      // granted: user has accepted the request
19  
      // default: user has dismissed the notification permission popup by clicking on x
20  
      // denied: user has denied the request.
21  
      console.log("out of await requestPermission");
22  
      console.log("permission: " + permission);
23  
      if (permission !== 'granted') {
24  
        throw new Error('Permission not granted for Notification')
25  
      }
26  
    };
27  
    
28  
    const #registerServiceWorker = async () => {
29  
      console.log("registerServiceWorker " + $workerURL);
30  
      const swRegistration = await navigator.serviceWorker.register($workerURL, {scope: $scope});
31  
      console.log("out of await swRegistration");
32  
      return swRegistration;
33  
    };
34  
    
35  
    const #showLocalNotification = (title, body, swRegistration) => {
36  
      swRegistration.showNotification(title, { body });
37  
    }
38  
    
39  
    const #main = async () => {
40  
      console.log("registerServiceWorker starting");
41  
      check();
42  
      const swRegistration = await registerServiceWorker();
43  
      console.log("Service worker registered: " + swRegistration);
44  
      const permission = await requestNotificationPermission();
45  
      console.log("Out of await requestNotificationPermission");
46  
      console.log("Notification permission: " + permission);
47  
      //showLocalNotification('Demo notification', 'Hello user', swRegistration);
48  
    };
49  
  
50  
    main();
51  
  ]]), workerURL := jsQuote(workerURL), scope := jsQuote(scope));
52  
}

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: 180 / 292
Version history: 12 change(s)
Referenced in: [show references]