1'use strict'; 2var appServerKey = 'BHK1O82NvTWYE9Hod4dPpZW55oT8b1FxVA8KG9EGEPvpUl-o_7x6ubqCAp-0QcwzeHqC28PU7c5c1R5vTV-VdLo'; 3var hasSubscription = false; 4var serviceWorkerRegistration = null; 5var subscriptionData = false; 6 7 8navigator.serviceWorker.register('/sw2.js').then(function (sw) { 9 serviceWorkerRegistration = sw; 10 11 serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription) { 12 hasSubscription = !(subscription === null); 13 requestPermission(); 14 }); 15}).catch(function (error) { 16 console.error('Service Worker Error', error); 17}); 18 19function urlB64ToUint8Array(base64String) { 20 var padding = '='.repeat((4 - base64String.length % 4) % 4); 21 var base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/'); 22 var rawData = window.atob(base64); 23 var outputArray = new Uint8Array(rawData.length); 24 for (var i = 0; i < rawData.length; ++i) { 25 outputArray[i] = rawData.charCodeAt(i); 26 } 27 return outputArray; 28} 29 30function subscribeUser() { 31 serviceWorkerRegistration.pushManager.subscribe({ 32 userVisibleOnly: true, 33 applicationServerKey: urlB64ToUint8Array(appServerKey) 34 }).then(function (subscription) { 35 //fetch('https://editor.fo.ru/api/v1/webpush/publiccreate', { 36 fetch('/api/v1/webpush/publiccreate', { 37 method: "POST", 38 headers: { 39 'Content-Type': 'application/json' 40 }, 41 body: JSON.stringify(subscription) 42 }).then(function (response) { 43 return response; 44 }).then(function (text) { 45 console.log('User is subscribed.'); 46 hasSubscription = true; 47 //updatePushButton(); 48 }).catch(function (error) { 49 hasSubscription = false; 50 console.error('error fetching subscribe', error); 51 }); 52 }).catch(function (err) { 53 console.log('Failed to subscribe the user!!: ', err); 54 }); 55} 56 57function unsubscribeUser() { 58 serviceWorkerRegistration.pushManager.getSubscription().then(function (subscription) { 59 if (subscription) { 60 subscriptionData = { 61 endpoint: subscription.endpoint 62 }; 63 fetch('https://editor.fo.ru/api/v1/webpush', { 64 method: "DELETE", 65 headers: { 66 'Content-Type': 'application/json' 67 }, 68 body: JSON.stringify(subscriptionData) 69 }).then(function (response) { 70 return response; 71 }).then(function (text) { 72 hasSubscription = false; 73 }).catch(function (error) { 74 hasSubscription = true; 75 console.error('error fetching subscribe', error); 76 }); 77 hasSubscription = false; 78 return subscription.unsubscribe(); 79 } 80 }); 81} 82 83 84function requestPermission() { 85 return new Promise(function (resolve, reject) { 86 var permissionResult = Notification.requestPermission(function (result) { 87 // ÐоддеÑжка ÑÑÑаÑевÑей веÑÑии Ñ ÑÑнкÑией обÑаÑного вÑзова. 88 resolve(result); 89 }); 90 if (permissionResult) { 91 permissionResult.then(resolve, reject); 92 } 93 }).then(function (permissionResult) { 94 if (permissionResult !== 'granted') { 95 throw new Error('Permission not granted.'); 96 } else { 97 if (!hasSubscription) { 98 subscribeUser(); 99 } 100 } 101 }); 102} 103
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.