1document.addEventListener('DOMContentLoaded', () => { 2 const applicationServerKey = magellanoWebPushData['vapid_key']; 3 4 5 6 if (!('serviceWorker' in navigator)) { 7 console.warn('Service workers are not supported by this browser'); 8 return; 9 } 10 11 if (!('PushManager' in window)) { 12 console.warn('Push notifications are not supported by this browser'); 13 return; 14 } 15 16 if (!('showNotification' in ServiceWorkerRegistration.prototype)) { 17 console.warn('Notifications are not supported by this browser'); 18 return; 19 } 20 if (Notification.permission === 'denied') { 21 console.warn('Notifications are denied by the user'); 22 return; 23 } 24 25 navigator.serviceWorker.register('/sw.js').then( 26 () => { 27 console.log('Worker registered'); 28 push_updateSubscription(); 29 }, 30 e => { 31 console.error('Worker Error', e); 32 } 33 ); 34 35 function urlBase64ToUint8Array(base64String) { 36 const padding = '='.repeat((4 - (base64String.length % 4)) % 4); 37 const base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/'); 38 39 const rawData = window.atob(base64); 40 const outputArray = new Uint8Array(rawData.length); 41 42 for (let i = 0; i < rawData.length; ++i) { 43 outputArray[i] = rawData.charCodeAt(i); 44 } 45 return outputArray; 46 } 47 48 function checkNotificationPermission() { 49 return new Promise((resolve, reject) => { 50 if (Notification.permission === 'denied') { 51 return reject(new Error('Push messages are blocked.')); 52 } 53 54 if (Notification.permission === 'granted') { 55 return resolve(); 56 } 57 58 if (Notification.permission === 'default') { 59 return Notification.requestPermission().then(result => { 60 if (result !== 'granted') { 61 reject(new Error('Bad permission result')); 62 } else { 63 resolve(); 64 } 65 }); 66 } 67 68 return reject(new Error('Unknown permission')); 69 }); 70 } 71 72 function push_subscribe() { 73 return checkNotificationPermission() 74 .then(() => navigator.serviceWorker.ready) 75 .then(serviceWorkerRegistration => 76 serviceWorkerRegistration.pushManager.subscribe({ 77 userVisibleOnly: true, 78 applicationServerKey: urlBase64ToUint8Array(applicationServerKey), 79 }) 80 ) 81 .then(subscription => { 82 // Subscription was successful 83 // create subscription on your server 84 return push_sendSubscriptionToServer(subscription, 'POST'); 85 }) 86 .then(subscription => subscription ) 87 .catch(e => { 88 if (Notification.permission === 'denied') { 89 console.warn('Notifications are denied by the user.'); 90 } else { 91 // A problem occurred with the subscription; common reasons 92 // include network errors or the user skipped the permission 93 console.error('Impossible to subscribe to push notifications', e); 94 } 95 }); 96 } 97 98 function push_updateSubscription() { 99 navigator.serviceWorker.ready 100 .then(serviceWorkerRegistration => serviceWorkerRegistration.pushManager.getSubscription()) 101 .then(subscription => { 102 if (!subscription) { 103 push_subscribe(); 104 return; 105 } 106 // Keep your server in sync with the latest endpoint 107 return push_sendSubscriptionToServer(subscription, 'PUT'); 108 }) 109 .then(subscription => subscription) // Set your UI to show they have subscribed for push messages 110 .catch(e => { 111 console.error('Error when updating the subscription', e); 112 }); 113 } 114 115 function push_unsubscribe() { 116 117 // To unsubscribe from push messaging, you need to get the subscription object 118 navigator.serviceWorker.ready 119 .then(serviceWorkerRegistration => serviceWorkerRegistration.pushManager.getSubscription()) 120 .then(subscription => { 121 // Check that we have a subscription to unsubscribe 122 if (!subscription) { 123 // No subscription object, so set the state 124 return; 125 } 126 127 // We have a subscription, unsubscribe 128 // Remove push subscription from server 129 return push_sendSubscriptionToServer(subscription, 'DELETE'); 130 }) 131 .then(subscription => subscription.unsubscribe()) 132 .catch(e => { 133 // We failed to unsubscribe, this can lead to 134 // an unusual state, so it may be best to remove 135 // the users data from your data store and 136 // inform the user that you have done so 137 console.error('Error when unsubscribing the user', e); 138 }); 139 } 140 141 function push_sendSubscriptionToServer(subscription, method) {
142 const key = subscription.getKey('p256dh'); 143 const token = subscription.getKey('auth'); 144 const contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0]; 145 146 return fetch(magellanoWebPushData['magellano_webpush_api'], { 147 method, 148 body: JSON.stringify({ 149 site: magellanoWebPushData['site'], 150 endpoint: subscription.endpoint, 151 publicKey: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null, 152 authToken: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null, 153 contentEncoding, 154 }), 155 }).then(() => subscription); 156 } 157 }); 158 159
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.