1 2initSW(); 3 4function initSW() { 5 if (!"serviceWorker" in navigator) { 6 //service worker isn't supported 7 return; 8 } 9 10 //don't use it here if you use service worker 11 //for other stuff. 12 if (!"PushManager" in window) { 13 //push isn't supported 14 return; 15 } 16 17 //register the service worker 18 navigator.serviceWorker.register('/sw.js', { 19 scope: '/', 20 }).then(function(registration) { 21 console.log('serviceWorker installed!', registration.scope) 22 initPush(); 23 }) 24 .catch((err) => { 25 console.log(err) 26 }); 27} 28 29 30function initPush() { 31 if (!navigator.serviceWorker.ready) { 32 return; 33 } 34 35 new Promise(function (resolve, reject) { 36 const permissionResult = Notification.requestPermission(function (result) { 37 resolve(result); 38 }); 39 40 if (permissionResult) { 41 permissionResult.then(resolve, reject); 42 } 43 }) 44 .then((permissionResult) => { 45 if (permissionResult !== 'granted') { 46 throw new Error('Permission not granted!'); 47 } 48 subscribeUser(); 49 }); 50} 51 52function subscribeUser() { 53 54 navigator.serviceWorker.ready.then(data => console.log(data)) 55 navigator.serviceWorker.ready 56 .then((registration) => { 57 const subscribeOptions = { 58 userVisibleOnly: true, 59 applicationServerKey: urlBase64ToUint8Array( 60 //'{{env('VAPID_PUBLIC_KEY')}}' 61 'BDJC4MovqaVcazGSGFEtGiuJ5gXuSklMpwkH1mI10nDs7g2Q8-QFEPZIS9aVE6y4PjlS2UWcnK3GYZsDeLk0Njk' 62 ) 63 }; 64 65 return registration.pushManager.subscribe(subscribeOptions); 66 }) 67 .then((pushSubscription) => { 68 console.log('Received PushSubscription: ', JSON.stringify(pushSubscription)); 69 storePushSubscription(pushSubscription); 70 }); 71} 72 73 74function urlBase64ToUint8Array(base64String) { 75 var padding = '='.repeat((4 - base64String.length % 4) % 4); 76 var base64 = (base64String + padding) 77 .replace(/\-/g, '+') 78 .replace(/_/g, '/'); 79 80 var rawData = window.atob(base64); 81 var outputArray = new Uint8Array(rawData.length); 82 83 for (var i = 0; i < rawData.length; ++i) { 84 outputArray[i] = rawData.charCodeAt(i); 85 } 86 return outputArray; 87} 88 89function storePushSubscription(pushSubscription) { 90 //const token = document.querySelector('meta[name=csrf-token]').getAttribute('content'); 91 92 93 fetch('https://notifications.newsday.co.zw/api/push', { 94 method: 'POST', 95 body: JSON.stringify(pushSubscription), 96 headers: { 97 'Accept': 'application/json', 98 'Content-Type': 'application/json' 99 } 100 }) 101 .then((res) => { 102 return res.json(); 103 }) 104 .then((res) => { 105 console.log(res) 106 }) 107 .catch((err) => { 108 console.log(err) 109 }); 110}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.