1if ('serviceWorker' in navigator && 'PushManager' in window) { 2 console.log('Service Worker and Push is supported'); 3 window.addEventListener('load', function() { 4 if (!document.cookie.includes("SubNot=valid")) { 5 registerServiceWorker(); 6 } 7 }); 8} else { 9 console.warn('Push messaging is not supported'); 10} 11 12 13async function checkSubscription(registration) { 14 15 registration.pushManager.getSubscription().then(subscription => { 16 if (subscription) { 17 fetch(window.location.origin + '/.rest/pushService/servicesEndpoints', { 18 method: 'POST', 19 body: 'consult', 20 headers: { 21 "content-type": "text/plain" 22 } 23 }).then(response1 => { 24 response1.json().then(jsonresponse => { 25 fetch(window.location.origin + jsonresponse.endpoint, { 26 method: 'POST', 27 body: JSON.stringify({ subscription: subscription, origin: window.location.origin }), 28 headers: { 29 "content-type": "application/json", 30 "Authorization": jsonresponse.token 31 } 32 }).then(response => { 33 response.json().then(subscribed => { 34 if (subscribed) { 35 createCookie(); 36 } else { 37 askForPermission(registration); 38 } 39 }); 40 }); 41 }); 42 }); 43 } else { 44 askForPermission(registration); 45 } 46 }); 47} 48 49 50// Try to register Service Worker 51 52function registerServiceWorker() { 53 var urlSW = "/.resources/modulo-notificaciones-lm/webresources/js/sw.js"; 54 return navigator.serviceWorker.register(urlSW).then(function(registration) { 55 console.log('Service worker successfully registered.'); 56 checkSubscription(registration); 57 return true; 58 }, function(error) { 59 console.error('Unable to register service worker: ', error); 60 return false; 61 }); 62} 63 64 65// Ask to User for Push notifications permission. 66 67function askForPermission(registration) { 68 69 function handlePermission(permission) { 70 // Whatever the user answers, we make sure Chrome stores the information 71 if (!('permission' in Notification)) { 72 Notification.permission = permission; 73 } 74 75 if (Notification.permission === "granted") { 76 // If it's okay let's receive notifications 77 console.info("Permisos para mostrar notificaciones dados"); 78 fetch(window.location.origin + '/.rest/pushService/servicesEndpoints', { 79 method: 'POST', 80 body: 'key', 81 headers: { 82 "content-type": "text/plain" 83 } 84 }).then(response1 => { 85 response1.json().then(jsonresponse => { 86 87 ajax_get(window.location.origin + jsonresponse.endpoint, 88 jsonresponse.token, 89 function(data) {subscribe(data, registration);} 90 ); 91 }); 92 }); 93 } else { 94 listenerPermission(); 95 } 96 } 97 98 function listenerPermission() { 99 navigator.permissions.query({ name: 'notifications' }).then(function(notificationPerm) { 100 notificationPerm.onchange = function(permission) { 101 handlePermission(permission); 102 console.log("User decided to change his settings. New permission: " + notificationPerm.state); 103 }; 104 }); 105 } 106 107 if (!("Notification" in window)) { 108 console.warn("This browser does not support desktop notification"); 109 } else { 110 if (checkNotificationPromise()) { 111 Notification.requestPermission() 112 .then((permission) => { 113 handlePermission(permission); 114 }) 115 } else { 116 Notification.requestPermission(function(permission) { 117 handlePermission(permission); 118 }); 119 } 120 } 121} 122 123// Function to check whether browser supports the promise version of requestPermission() 124// Safari only supports the old callback-based version 125function checkNotificationPromise() { 126 try { 127 Notification.requestPermission().then(); 128 } catch (e) { 129 return false; 130 } 131 132 return true; 133} 134 135function subscribeUserToPush(userServerKey) { 136 return navigator.serviceWorker.ready 137 .then(function(registration) { 138 const subscribeOptions = { 139 userVisibleOnly: true, 140 applicationServerKey: userServerKey 141 }; 142 143 return registration.pushManager.subscribe(subscribeOptions); 144 }) 145 .then(function(pushSubscription) { 146 console.log('Received PushSubscription: ', JSON.stringify(pushSubscription)); 147 return pushSubscription; 148 }); 149} 150 151function ajax_get(url, token, callback) { 152 var xmlhttp = new XMLHttpRequest(); 153 xmlhttp.onreadystatechange = function() { 154 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 155 try { 156 var data = xmlhttp.responseText; 157 158 } catch (err) { 159 console.log(err.message + " in " + xmlhttp.responseText); 160 return; 161 } 162 callback(data); 163 } 164 }; 165 xmlhttp.open("GET", url, true); 166 xmlhttp.setRequestHeader( "Authorization", token); 167 xmlhttp.send(); 168} 169 170async function subscribe(response, registration) { 171 172 registration.pushManager.subscribe({ 173 userVisibleOnly: true, 174 applicationServerKey: response 175 }).then(subscription => { 176 fetch(window.location.origin + '/.rest/pushService/servicesEndpoints', { 177 method: 'POST', 178 body: 'save', 179 headers: { 180 "content-type": "text/plain" 181 } 182 }).then(response1 => { 183 response1.json().then(jsonresponse => { 184 return fetch(window.location.origin + jsonresponse.endpoint, { 185 method: 'POST', 186 headers: { 187 'Content-Type': 'application/json', 188 "Authorization": jsonresponse.token 189 }, 190 body: JSON.stringify({ subscription: subscription, origin: window.location.origin }), 191 }).then(function(response) { 192 if (response.ok) { 193 console.log("subscription stored"); 194 createCookie(); 195 } 196 }); 197 }); 198 }); 199 }).catch((error) => { 200 unsubscribe(registration); 201 }); 202} 203 204async function unsubscribe(registration) { 205 // const registration = await navigator.serviceWorker.ready; 206 const subscription = await registration.pushManager.getSubscription(); 207 if (subscription) { 208 const successful = await subscription.unsubscribe(); 209 if (successful) { 210 console.info('Unsubscription successful'); 211 registerServiceWorker(); 212 } 213 else {
214 console.error('Unsubscription failed'); 215 } 216 } 217} 218 219function createCookie() { 220 cookieExpire = new Date(); 221 cookieExpire.setDate(cookieExpire.getDate() + 3); 222 cExpires = cookieExpire.toUTCString(); 223 document.cookie = "SubNot=valid; expires=" + cExpires + ";"; 224}
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.