1const FCM_DEBUG = false; 2var d = new Date(), 3 date = String(d.getFullYear()) + String(d.getMonth()) + String(d.getDay()); 4if (!FCM_DEBUG) { 5 self.importScripts('/index/notifications'); 6} else { 7 date = String(d.getFullYear()) + String(d.getMonth()) + String(d.getDay()) + String(d.getHours()) + String(d.getMinutes()) + String(d.getSeconds()); 8 self.importScripts('/index/notifications?time=' + date); 9} 10self.importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js'); 11self.importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js'); 12if (FCM_DEBUG) console.log('[Service Worker] start worker'); 13if (firebaseConfig) { 14 firebase.initializeApp(firebaseConfig); 15 if (FCM_DEBUG) console.log('[Service Worker] config exist'); 16 const messaging = firebase.messaging(); 17 self.addEventListener('install', function (event) { 18 if (FCM_DEBUG) console.log('[Service Worker] install'); 19 event.waitUntil(self.skipWaiting()); 20 }); 21 self.addEventListener('activate', function(event) { 22 if (FCM_DEBUG) console.log('[Service Worker] activate'); 23 event.waitUntil(clients.claim()); 24 }); 25 self.addEventListener('push', onPushNotification); 26 self.addEventListener('notificationclick', onPushNotificationClick); 27 28 function onPushNotification(event) { 29 if (FCM_DEBUG) console.log('[Service Worker] Push Received.', event); 30 // console.log(`[Service Worker] Push had this data: "${event.data.text()}"`); 31 const notification = event.data.json(); 32 if (FCM_DEBUG) console.log('[Service Worker] Push data: ', notification); 33 var title = ''; 34 if (typeof (notification.data.title) != 'undefined' && notification.data.title != '') { 35 title = notification.data.title; 36 } 37 var options = {}; 38 if (typeof (notification.data) != 'undefined' && notification.data != '') { 39 options.data = notification.data; 40 } 41 if (typeof (notification.data.body) != 'undefined' && notification.data.body != '') { 42 options.body = notification.data.body; 43 } 44 if (typeof (notification.data.icon) != 'undefined' && notification.data.icon != '') { 45 options.icon = notification.data.icon; 46 } 47 if (typeof (notification.data.image) != 'undefined' && notification.data.image != '') { 48 options.image = notification.data.image; 49 } 50 if (typeof (notification.data.badge) != 'undefined' && notification.data.badge != '') { 51 options.badge = notification.data.badge; 52 } 53 if (typeof (notification.data.url) != 'undefined' && notification.data.url != '') { 54 options.url = notification.data.url; 55 options.requireInteraction = true; 56 } 57 if ('actions' in Notification.prototype) { 58 if (typeof (notification.data.url) != 'undefined' && notification.data.url != '') { 59 options.actions = [ 60 {action: 'url', title: 'Open', type: 'button'}, 61 ]; 62 } 63 } 64 /* 65 { 66 body: e.content, 67 icon: e.icon, 68 image: e.image, 69 data: e, 70 actions: e.buttons, 71 tag: s.tag, 72 requireInteraction: s.persistNotification, 73 renotify: !0, 74 badge: e.badge, 75 vibrate: e.vibrate 76 }; 77 */ 78 if (event.data) { 79 if (FCM_DEBUG) { 80 console.log('[Service Worker] This push event has data: ', event.data.json()); 81 console.log('[Service Worker] This push event has data vars: ', JSON.parse(event.data.json()?.['data']?.['vars'])); 82 } 83 } else { 84 if (FCM_DEBUG) console.log('[Service Worker] This push event has no data.'); 85 } 86 event.waitUntil( 87 clients.matchAll({ 88 type: "window", 89 includeUncontrolled: true 90 }).then(function (clientList) { 91 if (FCM_DEBUG) console.log('[Service Worker] clientList', clientList); 92 var openedClient = false;
93 try { 94 if (FCM_DEBUG) console.log('[Service Worker] clientList: start checking'); 95 for (var i = 0; i < clientList.length; i++) { 96 var client = clientList[i]; 97 if ('visibilityState' in client && client.visibilityState === "visible") { 98 openedClient = true; 99 if (FCM_DEBUG) console.log('[Service Worker] Focus client window.'); 100 return client.focus(); 101 } 102 } 103 } catch (e) { 104 if (FCM_DEBUG) console.log('[Service Worker] Clients error.', e); 105 openedClient = false; 106 } 107 if (!openedClient) { 108 if (FCM_DEBUG) console.log('[Service Worker] clientList', options); 109 const notificationPromise = self.registration.showNotification(title, options); 110 return event.waitUntil(notificationPromise); 111 } else { 112 if (FCM_DEBUG) console.log('[Service Worker] clientList: exist opened client'); 113 } 114 }).catch((err) => console.log('[Service Worker Error]', err.message)) 115 ); 116 } 117 118 function onPushNotificationClick(event) { 119 if (FCM_DEBUG) console.log('[Service Worker] Notification click Received.', event); 120 if (FCM_DEBUG) console.log('[Service Worker] Notification click DATA:', event); 121 event.notification.close(); 122 var needOpen = false, url = ''; 123 if ('actions' in Notification.prototype) { 124 switch (event.action) { 125 case "url": 126 url = event.notification.data.url; 127 needOpen = true; 128 break; 129 default: 130 url = event.notification.data.url; 131 needOpen = true; 132 break; 133 } 134 } else { 135 url = event.notification.data.url; 136 needOpen = true; 137 } 138 if (typeof (url) == 'undefined' || url == '') { 139 needOpen = false; 140 if (FCM_DEBUG) console.log('[Service Worker] Notification click empty url - skipped'); 141 } 142 if (needOpen) { 143 event.waitUntil( 144 clients.matchAll({ 145 type: "window", 146 includeUncontrolled: true 147 }).then(function (clientList) { 148 if (FCM_DEBUG) console.log('[Service Worker] clientList', clientList); 149 for (var i = 0; i < clientList.length; i++) { 150 var client = clientList[i]; 151 if ('visibilityState' in client && client.visibilityState==="visible") { 152 if (FCM_DEBUG) console.log('[Service Worker] focus'); 153 return client.focus(); 154 } 155 } 156 if (clients.openWindow) { 157 if (FCM_DEBUG) console.log('[Service Worker] open'); 158 return clients.openWindow(url); 159 } 160 }).catch((err) => console.log('[Service Worker]', err.message)) 161 ); 162 } 163 } 164 if (FCM_DEBUG) console.log('[Service Worker]', self); 165} else { 166 if (FCM_DEBUG) console.log('[Service Worker]', 'no config'); 167}
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.