1var WebPushHandler = {}; 2 3try { 4 WebPushHandler.publicKey = null; 5 WebPushHandler.version = null; 6 WebPushHandler.hasPermission = null; 7 8 WebPushHandler.urlBase64ToUint8Array = function(base64String) { 9 const padding = '='.repeat((4 - base64String.length % 4) % 4); 10 const base64 = (base64String + padding) 11 .replace(/\-/g, '+') 12 .replace(/_/g, '/'); 13 14 const rawData = window.atob(base64); 15 const outputArray = new Uint8Array(rawData.length); 16 17 for (var i = 0; i < rawData.length; ++i) { 18 outputArray[i] = rawData.charCodeAt(i); 19 } 20 return outputArray; 21 } 22 23 WebPushHandler.registerServiceWorker = function () { 24 return navigator.serviceWorker.register('/ajax/js/webpush_service_worker.js?v=' + WebPushHandler.version) 25 .then(function (registration) { 26 return registration; 27 }) 28 .catch(function (err) { 29 console.log('Unable to register service worker.', err); 30 }); 31 }; 32 33 WebPushHandler.askPermission = function () { 34 return new Promise(function (resolve, reject) { 35 const permissionResult = Notification.requestPermission(function (result) { 36 resolve(result); 37 }); 38 39 if (permissionResult) { 40 permissionResult.then(resolve, reject); 41 } 42 }); 43 }; 44 45 WebPushHandler.getNotificationPermissionState = function () { 46 if (navigator.permissions) { 47 return navigator.permissions.query({name: 'notifications'}) 48 .then(function (result) { 49 return result.state; 50 }); 51 } 52 53 return new Promise(function (resolve) { 54 resolve(Notification.permission); 55 }); 56 }; 57 58 WebPushHandler.subscribeUserToPush = function () { 59 return WebPushHandler.swRegistration 60 .then(function (registration) { 61 const subscribeOptions = { 62 userVisibleOnly: true, 63 applicationServerKey: WebPushHandler.urlBase64ToUint8Array(WebPushHandler.publicKey) 64 }; 65 66 return registration.pushManager.subscribe(subscribeOptions); 67 }) 68 .then(function (pushSubscription) { 69 WebPushHandler.sendSubscriptionToBackEnd(pushSubscription); 70 71 return pushSubscription; 72 }); 73 }; 74 75 WebPushHandler.sendSubscriptionToBackEnd = function (subscription) { 76 var request = { 77 subscription: subscription, 78 vapidPublicKey: WebPushHandler.publicKey 79 }; 80 81 return fetch('/ajax/web-push.php?action=subscription', { 82 method: 'POST', 83 headers: { 84 'Content-Type': 'application/json' 85 }, 86 credentials: 'same-origin', 87 body: JSON.stringify(request) 88 }) 89 .then(function (response) { 90 if (!response.ok) { 91 throw new Error('Bad status code from server.'); 92 } 93 94 return response.json(); 95 }) 96 .then(function (responseData) { 97 }); 98 }; 99 100 WebPushHandler.checkCompatibility = function() { 101 return ('serviceWorker' in navigator) && ('PushManager' in window); 102 }; 103 104 WebPushHandler.setPermissionValue = function() { 105 return WebPushHandler.getNotificationPermissionState() 106 .then(function (permission) { 107 WebPushHandler.hasPermission = permission; 108 }); 109 }; 110 111 WebPushHandler.isSnippetAvailable = function() { 112 return WebPushHandler.checkCompatibility() && WebPushHandler.hasPermission != 'granted' && WebPushHandler.hasPermission != 'de
112nied'; 113 }; 114 115 WebPushHandler.setSnippetVisibility = function() { 116 if (WebPushHandler.isSnippetAvailable() && $("#iai_webpush_toplayer")) { 117 $("#iai_webpush_toplayer").show(); 118 } 119 }; 120 121 WebPushHandler.init = function () { 122 if (!WebPushHandler.checkCompatibility()) { 123 return; 124 } 125 126 WebPushHandler.swRegistration = WebPushHandler.registerServiceWorker(); 127 WebPushHandler.swRegistration.then(function () { 128 return WebPushHandler.getNotificationPermissionState() 129 .then(function (permission) { 130 if (permission != 'granted') { 131 return WebPushHandler.askPermission(); 132 } 133 }); 134 }) 135 .then(function (permission) { 136 if (permission == 'granted') { 137 WebPushHandler.subscribeUserToPush(); 138 } 139 }); 140 }; 141} catch(e) { 142 console.log(e); 143} 144
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.