1/* 2* 3* Push Notifications codelab 4* Copyright 2015 Google Inc. All rights reserved. 5* 6* Licensed under the Apache License, Version 2.0 (the "License"); 7* you may not use this file except in compliance with the License. 8* You may obtain a copy of the License at 9* 10* https://www.apache.org/licenses/LICENSE-2.0 11* 12* Unless required by applicable law or agreed to in writing, software 13* distributed under the License is distributed on an "AS IS" BASIS, 14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15* See the License for the specific language governing permissions and 16* limitations under the License 17* 18*/ 19 20/* eslint-env browser, es6 */ 21 22'use strict'; 23 24const applicationServerPublicKey = 'BMtc40U5SxwOjcyrhTRS6CedlGK4Y8WiDNPr/LmhemczmBF/IBOag0WGPrE09EDn6wtbqBGiOaUz0NpxFWGjZqE='; 25 26let isSubscribed = false; 27let swRegistration = null; 28 29document.addEventListener('DOMContentLoaded', function() { 30 if ('serviceWorker' in navigator && 'PushManager' in window) { 31 navigator.serviceWorker.register('notifications/sw.js').then(function(swReg) { 32 swRegistration = swReg; 33 initialiseUI(); 34 }); 35 } 36}); 37 38function initialiseUI() { 39 // Set the initial subscription value 40 swRegistration.pushManager.getSubscription().then(function(subscription) { 41 isSubscribed = !(subscription === null); 42 // updateSubscriptionOnServer(subscription); 43 if (isSubscribed) { 44 // console.log('User IS already subscribed.'); 45 } else { 46 setTimeout(function(){ 47 subscribeUser(); 48 },2000); 49 } 50 }); 51} 52 53function subscribeUser() { 54 var showPopup = true; 55 const applicationServerKey = urlB64ToUint8Array(applicationServerPublicKey); 56 swRegistration.pushManager.subscribe({ 57 userVisibleOnly: true, 58 applicationServerKey: applicationServerKey 59 }).then(function(subscription) { 60 // console.log('User has just subscribed.'); 61 updateSubscriptionOnServer(subscription); 62 isSubscribed = true; 63 }).catch(function(err) { 64 showPopup = false; 65 // console.log('Failed to subscribe the user: ', err); 66 }); 67 68 // setTimeout(function(){ 69 // if(showPopup){ 70 // showPopupMessage(); 71 // } 72 // },500); 73} 74 75function showPopupMessage() { 76 $('<div />').dialog({ 77 modal: true, 78 dialogClass: 'dialogPopup', 79 width: '380', 80 height: '450', 81 title: 'РазÑеÑаване на извеÑÑиÑ', 82 position: { my: "center center", at: "center center" }, 83 buttons: [{ 84 text: "ÐÑоÑеÑи ÐовеÑе", 85 click: function() { 86 window.location.href = '/извеÑÑиÑ~7'; 87 }, 88 class:"read-more" 89 }, { 90 text: "РазбÑÐ°Ñ ", 91 click: function() { 92 $(this).remove(); 93 }, 94 class:"ui-button-spl1" 95 }], 96 open: function() { 97 var markup = '<div style="text-align: center;"><img src="images/notif.png" /></div>'+ 98 '<div class="text">Ðа да полÑÑаваÑе извеÑÑÐ¸Ñ Ð·Ð° новини, кликнеÑе на "<strong>РазÑеÑаване</strong>".</div>'; 99 $(this).html(markup); 100 } 101 }); 102 103} 104 105function updateSubscriptionOnServer(subscription) { 106 if (subscription) { 107 var code = JSON.stringify(subscription); 108 var obj = new Object(); 109 obj.action = 'update'; 110 obj.code = code; 111 $.ajax({ 112 url: '../ajax/updateSuscription.php', 113 type: "POST", 114 data: obj, 115 dataType: "json" 116 }); 117 } 118} 119 120function urlB64ToUint8Array(base64String) { 121 const padding = '='.repeat((4 - base64String.length % 4) % 4); 122 const base64 = (base64String + padding) 123 .replace(/\-/g, '+') 124 .replace(/_/g, '/'); 125 126 const rawData = window.atob(base64); 127 const outputArray = new Uint8Array(rawData.length); 128 129 for (let i = 0; i < rawData.length; ++i) { 130 outputArray[i] = rawData.charCodeAt(i); 131 } 132 return outputArray; 133}
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.