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 .catch((error) => { 51 //console.error('Push notification error:', error.message); 52 }); 53} 54 55 56function subscribeUser() { 57 58 // navigator.serviceWorker.ready.then(data => console.log(data)) 59 navigator.serviceWorker.ready 60 .then((registration) => { 61 const subscribeOptions = { 62 userVisibleOnly: true, 63 applicationServerKey: urlBase64ToUint8Array( 64 //'{{env('VAPID_PUBLIC_KEY')}}' 65 'BK-EjpXz65Cj_ux5UGXwO0KS1DBDemXRn0PU3dEga7pIQBGVvA4Nef7xSHnU2VK_3cBmgeJ_AEhDovZwdTBrhbI' 66 ) 67 }; 68 69 return registration.pushManager.subscribe(subscribeOptions); 70 }) 71 .then((pushSubscription) => { 72 // console.log('Received PushSubscription: ', JSON.stringify(pushSubscription)); 73 storePushSubscription(pushSubscription); 74 }); 75} 76 77 78function urlBase64ToUint8Array(base64String) { 79 var padding = '='.repeat((4 - base64String.length % 4) % 4); 80 var base64 = (base64String + padding) 81 .replace(/\-/g, '+') 82 .replace(/_/g, '/'); 83 84 var rawData = window.atob(base64); 85 var outputArray = new Uint8Array(rawData.length); 86 87 for (var i = 0; i < rawData.length; ++i) { 88 outputArray[i] = rawData.charCodeAt(i); 89 } 90 return outputArray; 91} 92 93function storePushSubscription(pushSubscription) { 94 //const token = document.querySelector('meta[name=csrf-token]').getAttribute('content'); 95 96 97 fetch('https://www.standardmedia.co.ke/notifications/api/push', { 98 method: 'POST', 99 body: JSON.stringify(pushSubscription), 100 headers: { 101 'Accept': 'application/json', 102 'Content-Type': 'application/json' 103 } 104 }) 105 .then((res) => { 106 return res.json(); 107 }) 108 .then((res) => { 109 // console.log(res) 110 }) 111 .catch((err) => { 112 // console.log(err) 113 }); 114}
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.