1const CACHE_VERSION = '1.7.77'; 2 3const BASE_CACHE_FILES = [ 4 'https://www.zaaptv.com/wp-content/uploads/2020/08/1024x1024bb.png', 5'', 6'https://www.zaaptv.com/wp-content/uploads/2020/08/1024x1024bb.png', 7'', 8'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1136x640.png', 9'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_640x1136.png', 10'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_2688x1242.png', 11'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1792x828.png', 12'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1125x2436.png', 13'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_828x1792.png', 14'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_2436x1125.png', 15'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1242x2208.png', 16'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_2208x1242.png', 17'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1334x750.png', 18'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_750x1334.png', 19'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_2732x2048.png', 20'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_2048x2732.png', 21'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_2388x1668.png', 22'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1668x2388.png', 23'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_2224x1668.png', 24'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1242x2688.png', 25'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1668x2224.png', 26'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_1536x2048.png', 27'https://www.zaaptv.com/wp-content/uploads/pwa-splash-screen/splashscreens/icon_2048x1536.png', 28'https://www.zaaptv.com/wp-content/plugins/pwa-for-wp/images/logo-512x512.png', 29 30]; 31 32const OFFLINE_CACHE_FILES = [ 33 'https://www.zaaptv.com/en/renewals/', 34]; 35 36const NOT_FOUND_CACHE_FILES = [ 37 'https://www.zaaptv.com/en/contact/', 38]; 39 40const OFFLINE_PAGE = 'https://www.zaaptv.com/en/renewals/'; 41const NOT_FOUND_PAGE = 'https://www.zaaptv.com/en/contact/'; 42 43const CACHE_VERSIONS = { 44 content: 'content-v' + CACHE_VERSION, 45 notFound: '404-v' + CACHE_VERSION, 46 offline: 'offline-v' + CACHE_VERSION, 47}; 48 49// Define MAX_TTL's in SECONDS for specific file extensions 50const MAX_TTL = { 51 '/': 3600, 52 html: 3600, 53 json: 86400, 54 js: 86400, 55 css: 86400, 56 png: 86400, 57 jpg: 86400, 58}; 59 60const CACHE_STRATEGY = { 61 default: 'networkFirst', 62 css_js: 'cacheFirst', 63 images: 'cacheFirst', 64 fonts: 'cacheFirst', 65}; 66 67const CACHE_BLACKLIST = [ 68// (str) => { 69// return !str.includes('/wp-admin/') || !str.startsWith('https://www.zaaptv.com//wp-admin/'); 70// }, 71]; 72const neverCacheUrls = [/\/wp-admin/,/\/wp-login/,/preview=true/,/\/cart/,/ajax/,/login/,]; 73 74 75const SUPPORTED_METHODS = [ 76 'GET', 77]; 78// Check if current url is in the neverCacheUrls list 79function pwaForWpcheckNeverCacheList(url) { 80 if ( this.match(url) ) { 81 return false; 82 } 83 return true; 84} 85/** 86 * pwaForWpisBlackListed 87 * @param {string} url 88 * @returns {boolean} 89 */ 90function pwaForWpisBlackListed(url) { 91 return (CACHE_BLACKLIST.length > 0) ? !CACHE_BLACKLIST.filter((rule) => { 92 if(typeof rule === 'function') { 93 return !rule(url); 94 } else { 95 return false; 96 } 97 }).length : false 98} 99 100/** 101 * pwaForWpgetFileExtension 102 * @param {string} url 103 * @returns {string} 104 */ 105function pwaForWpgetFileExtension(url) { 106 107 if (typeof url === 'string') { 108 109 let split_two = url.split('?'); 110 let split_url = split_two[0]; 111 112 let extension = split_url.split('.').reverse()[0].split('?')[0]; 113 return (extension.endsWith('/')) ? '/' : extension; 114 115 }else{ 116 return null; 117 } 118} 119/** 120 * pwaForWpgetTTL 121 * @param {string} url 122 */ 123function pwaForWpgetTTL(url) { 124 if (typeof url === 'string') { 125 let extension = pwaForWpgetFileExtension(url); 126 if (typeof MAX_TTL[extension] === 'number') { 127 return MAX_TTL[extension]; 128 } else { 129 return MAX_TTL["/"]; 130 } 131 } else { 132 return MAX_TTL["/"]; 133 } 134} 135 136/** 137 * pwaForWpinstallServiceWorker
138 * @returns {Promise} 139 */ 140function pwaForWpinstallServiceWorker() { 141 return Promise.all( 142 [ 143 caches.open(CACHE_VERSIONS.content) 144 .then( 145 (cache) => { 146 147 if(BASE_CACHE_FILES.length >0){ 148 149 for (var i = 0; i < BASE_CACHE_FILES.length; i++) { 150 151 pwaForWpprecacheUrl(BASE_CACHE_FILES[i]) 152 153 } 154 155 } 156 157 //return cache.addAll(BASE_CACHE_FILES); 158 } 159 ), 160 caches.open(CACHE_VERSIONS.offline) 161 .then( 162 (cache) => { 163 return cache.addAll(OFFLINE_CACHE_FILES); 164 } 165 ), 166 caches.open(CACHE_VERSIONS.notFound) 167 .then( 168 (cache) => { 169 return cache.addAll(NOT_FOUND_CACHE_FILES); 170 } 171 ) 172 ] 173 ) 174 .then(() => { 175 return self.skipWaiting(); 176 }); 177} 178 179/** 180 * pwaForWpcleanupLegacyCache 181 * @returns {Promise} 182 */ 183function pwaForWpcleanupLegacyCache() { 184 185 let currentCaches = Object.keys(CACHE_VERSIONS) 186 .map( 187 (key) => { 188 return CACHE_VERSIONS[key]; 189 } 190 ); 191 192 return new Promise( 193 (resolve, reject) => { 194 195 caches.keys() 196 .then( 197 (keys) => { 198 return legacyKeys = keys.filter( 199 (key) => { 200 return !~currentCaches.indexOf(key); 201 } 202 ); 203 } 204 ) 205 .then( 206 (legacy) => { 207 if (legacy.length) { 208 Promise.all( 209 legacy.map( 210 (legacyKey) => { 211 return caches.delete(legacyKey) 212 } 213 ) 214 ) 215 .then( 216 () => { 217 resolve() 218 } 219 ) 220 .catch( 221 (err) => { 222 reject(err); 223 } 224 ); 225 } else { 226 resolve(); 227 } 228 } 229 ) 230 .catch( 231 () => { 232 reject(); 233 } 234 ); 235 236 } 237 ); 238} 239 240function pwaForWpprecacheUrl(url) { 241 242 if(!pwaForWpisBlackListed(url)) { 243 caches.open(CACHE_VERSIONS.content) 244 .then((cache) => { 245 cache.match(url) 246 .then((response) => { 247 if(!response) { 248 return fetch(url) 249 } else { 250 // already in cache, nothing to do. 251 return null 252 } 253 }) 254 .then((response) => { 255 if(response) { 256 fetch(url).then(dataWrappedByPromise => dataWrappedByPromise.text()) 257 .then(data => { 258 if(data){ 259 const regex = /<img[^>]+src="(https:\/\/[^">]+)"/g; 260 let m; 261 while ((m = regex.exec(data)) !== null) { 262 if (m.index === regex.lastIndex) { 263 regex.lastIndex++; 264 } 265 m.forEach((match, groupIndex) => { 266 if(groupIndex == 1){ 267 if(new URL(match).origin == location.origin){ 268 fetch(match). 269 then((imagedata) => { 270 //console.log(imagedata); 271 cache.put(match, imagedata.clone()); 272 273 }); 274 } 275 } 276 }); 277 } 278 279 280 } 281 282 283 });
284 285 return cache.put(url, response.clone()); 286 } else { 287 return null; 288 } 289 }); 290 }) 291 } 292} 293 294var fetchRengeData = function(event){ 295 var pos = Number(/^bytes\=(\d+)\-$/g.exec(event.request.headers.get('range'))[1]); 296 console.log('Range request for', event.request.url, ', starting position:', pos); 297 event.respondWith( 298 caches.open(CACHE_VERSIONS.content) 299 .then(function(cache) { 300 return cache.match(event.request.url); 301 }).then(function(res) { 302 if (!res) { 303 return fetch(event.request) 304 .then(res => { 305 return res.arrayBuffer(); 306 }); 307 } 308 return res.arrayBuffer(); 309 }).then(function(ab) { 310 return new Response( 311 ab.slice(pos), 312 { 313 status: 206, 314 statusText: 'Partial Content', 315 headers: [ 316 // ['Content-Type', 'video/webm'], 317 ['Content-Range', 'bytes ' + pos + '-' + 318 (ab.byteLength - 1) + '/' + ab.byteLength]] 319 }); 320 })); 321} 322 323let cachingStrategy = { 324 notGetMethods: function(event){ 325 // If non-GET request, try the network first, fall back to the offline page 326 if (event.request.method !== 'GET') { 327 event.respondWith( 328 fetch(event.request) 329 .catch(error => { 330 return caches.match(offlinePage); 331 }) 332 ); 333 return false; 334 } 335 }, 336 337 fetchFromCache: function(event){ 338 /* return new Promise( 339 (resolve) => {*/ 340 return caches.open(CACHE_VERSIONS.content) 341 .then( 342 (cache) => { 343 344 return cache.match(event.request) 345 .then( 346 (response) => { 347 348 if (response) { 349 350 let headers = response.headers.entries(); 351 let date = null; 352 353 for (let pair of headers) { 354 if (pair[0] === 'date') { 355 date = new Date(pair[1]); 356 } 357 } 358 359 if (date) { 360 let age = parseInt((new Date().getTime() - date.getTime())/1000); 361 let ttl = pwaForWpgetTTL(event.request.url); 362 363 if (age > ttl) { 364 365 return new Promise( 366 (resolve) => { 367 368 return fetch(event.request.clone()) 369 .then( 370 (updatedResponse) => { 371 if (updatedResponse) { 372 cache.put(event.request, updatedResponse.clone()); 373 resolve(updatedResponse); 374 } else { 375 resolve(response) 376 } 377 } 378 ) 379 .catch( 380 () => { 381 resolve(response); 382 } 383 ); 384 385 } 386 ) 387 .catch( 388 (err) => { 389 return response; 390 } 391 ); 392 } else { 393 return response; 394 } 395 396 } else { 397 return response; 398 } 399 400 } else { 401 return null; 402 } 403 } 404 ) 405 .then( 406 (response) => { 407 if (response) { 408 return response; 409 } else { 410 return fetch(event.request.clone(), {cache: "no-store"}) 411 .then( 412 (response) => { 413 414 if(response.status < 300) { 415 if (~SUPPORTED_METHODS.indexOf(event.request.method) && !pwaForWpisBlackListed(event.request.url)) { 416 cache.put(event.request, response.clone()); 417 } 418 return response; 419 } else { 420 return caches.open(CACHE_VERSIONS.notFound).then((cache) => { 421 return cache.match(NOT_FOUND_PAGE); 422 }) 423 } 424 } 425 ) 426 .then((response) => { 427 if(response) { 428 return response; 429 } 430 }) 431 .catch( 432 () => { 433 434 return caches.open(CACHE_VERSIONS.offline) 435 .then( 436 (offlineCache) => { 437 return offlineCache.match(OFFLINE_PAGE) 438 } 439 ) 440 441 } 442 ); 443 } 444 } 445 ) 446 .catch( 447 (error) => { 448 console.error(' Error in fetch handler:', error); 449 throw error; 450 } 451 ); 452 } 453 ) 454 /*})*/ 455 456 }, 457 fetchnetwork: function(event){ 458 return caches.open(CACHE_VERSIONS.content) 459 .then( 460 (cache) => { 461 return fetch(event.request.clone()).then(function (response) { 462 463 if(response.status < 300) { 464 if (~SUPPORTED_METHODS.indexOf(event.request.method) && !pwaForWpisBlackListed(event.request.url)) { 465 cache.put(event.request, response.clone()); 466 } 467 return response; 468 }else if(response.status==404){ 469 return cachingStrategy.Notfoundpage(); 470 } else if( cache.match(event.request) ){
471 return cache.match(event.request); 472 }else { 473 return cachingStrategy.Offlinepage(); 474 } 475 }).catch( 476 (err) => { 477 return cache.match(event.request) 478 } 479 ).catch( 480 (err) => { 481 return cachingStrategy.Offlinepage(); 482 } 483 ) 484 } 485 ).catch( 486 (err) => { 487 return cachingStrategy.Offlinepage(); 488 } 489 ) 490 }, 491 addCache: function(event,updatedResponse){ 492 cache.put(event.request, updatedResponse.clone()); 493 resolve(updatedResponse); 494 }, 495 Offlinepage: function(){ 496 return caches.open(CACHE_VERSIONS.offline).then((cache) => { 497 return cache.match(OFFLINE_PAGE); 498 }) 499 }, 500 Notfoundpage: function(){ 501 return caches.open(CACHE_VERSIONS.notFound).then((cache) => { 502 return cache.match(NOT_FOUND_PAGE); 503 }) 504 }, 505 /*Strategies*/ 506 networkOnlyStrategy: function(event){ 507 return caches.open(CACHE_VERSIONS.content) 508 .then( 509 (cache) => { 510 return fetch(event.request.clone()).then(function (response) { 511 if(response.status < 300) { 512 if (~SUPPORTED_METHODS.indexOf(event.request.method) && !pwaForWpisBlackListed(event.request.url)) { 513 cache.put(event.request, response.clone()); 514 } 515 return response; 516 }else if(response.status==404){ 517 return cachingStrategy.Notfoundpage(); 518 } else if(cache.match(event.request)){ 519 return cache.match(event.request) 520 } else { 521 return cachingStrategy.Offlinepage(); 522 } 523 }).catch( 524 (err) => { 525 return cachingStrategy.Offlinepage(); 526 } 527 ) 528 } 529 ).catch( 530 (err) => { 531 return cachingStrategy.Offlinepage() 532 } 533 ); 534 }, 535 cacheFirstStrategy: function(events){ 536 return cachingStrategy.fetchFromCache(events).catch( 537 (err) => { 538 return cachingStrategy.Offlinepage() 539 } 540 ); 541 }, 542 NeworkFirstStrategy: function(events){ 543 return cachingStrategy.fetchnetwork(events).catch( 544 (err) => { 545 return cachingStrategy.fetchFromCache(events) 546 } 547 ).catch( 548 (err) => { 549 return cachingStrategy.Offlinepage() 550 } 551 ); 552 } 553 554 555} 556 557 558self.addEventListener( 559 'install', event => { 560 event.waitUntil( 561 Promise.all([ 562 pwaForWpinstallServiceWorker(), 563 self.skipWaiting(), 564 ]) 565 ); 566 } 567); 568 569// The activate handler takes care of cleaning up old caches. 570self.addEventListener( 571 'activate', event => { 572 event.waitUntil( 573 Promise.all( 574 [ 575 pwaForWpcleanupLegacyCache(), 576 self.clients.claim(),
577 self.skipWaiting(), 578 ] 579 ) 580 .catch( 581 (err) => { 582 self.skipWaiting(); 583 } 584 ) 585 ); 586 } 587); 588self.addEventListener('online', event => { 589 if (navigator.onLine && navigator.standalone === true) { 590 isReachable(event.request.url).then(function(online) { 591 if (online) { 592 //handle online status 593 caches.delete(event.request.url); 594 console.log('online'); 595 } else { 596 console.log('no connectivity'); 597 } 598 }); 599 } else { 600 //handle offline status 601 console.log('offline'); 602 } 603}); 604 605function isReachable(url) { 606 /** 607 * Note: fetch() still "succeeds" for 404s on subdirectories, 608 * which is ok when only testing for domain reachability. 609 */ 610 return fetch(url, { method: 'HEAD', mode: 'no-cors' }) 611 .then(function(resp) { 612 return resp && (resp.ok || resp.type === 'opaque'); 613 }) 614 .catch(function(err) { 615 console.warn('[conn test failure]:', err); 616 }); 617} 618 619self.addEventListener( 620 'fetch', event => { 621 // Return if the current request url is in the never cache list 622 if ( ! neverCacheUrls.every(pwaForWpcheckNeverCacheList, event.request.url) ) { 623 //console.log( 'PWA ServiceWorker: URL exists in excluded list of cache.' + event.request.url); 624 return; 625 } 626 if(! neverCacheUrls.every(pwaForWpcheckNeverCacheList, event.request.referrer) ){ 627 //console.log( 'PWA ServiceWorker: Ref-URL exists in excluded list of cache.' + event.request.referrer); 628 return; 629 } 630 if(pwaForWpisBlackListed(event.request.url)){ 631 return; 632 } 633 634 const url = new URL(event.request.url); 635 636 637 path_array = url.pathname.split('/') 638 if (event.request.method === 'POST' && path_array.includes('activity')) { 639 return event.respondWith((async () => { 640 const formData = await event.request.formData(); 641 const title = formData.get('title'); 642 const text = formData.get('text'); 643 const link = formData.get('url'); 644 const image = formData.get('externalMedia'); 645 const reader = new FileReader(); 646 647 reader.onload = async (event) => { 648 const imageData = event.target.result; 649 650 const db = await openDatabase(); 651 await saveData(db, { 652 id: 'sharedData', 653 title: title, 654 text: text, 655 url: link, 656 image: imageData 657 }); 658 659 console.log('Data saved to IndexedDB'); 660 661 // return Response.redirect('/?share-target', 303); 662 return Response.redirect(url.pathname+'/?share-target', 303); 663 }; 664 665 reader.readAsDataURL(image); 666 return Response.redirect(url.pathname+'/?share-target', 303); 667 // return Response.redirect('/?share-target', 303); 668 })()); 669 } 670 671 // Return if request url protocal isn't http or https 672 if ( ! event.request.url.match(/^(http|https):\/\//i) ) 673 return; 674 if ( event.request.referrer.match(/^(wp-admin):\/\//i) ) 675 return; 676 677 if ( new URL(event.request.url).origin !== location.origin ) 678 return; 679 680 if(event.request.method !== 'GET' ){ 681 event.respondWith( 682 fetch(event.request) 683 .catch(error => { 684 return caches.open(CACHE_VERSIONS.offline).then(function(cache) { 685 return cache.match(OFFLINE_PAGE); 686 }); 687 }) 688 ); 689 return false; 690 } 691 const destination = event.request.destination; 692 switch (destination) { 693 case 'style': 694 case 'script': 695 cachingStrategyType = CACHE_STRATEGY.css_js; 696 break; 697 case 'document': 698 cachingStrategyType = CACHE_STRATEGY.default 699 break; 700 case 'image': 701 cachingStrategyType = CACHE_STRATEGY.images; 702 break; 703 case 'font': 704 cachingStrategyType = CACHE_STRATEGY.fonts; 705 break; 706 // All `XMLHttpRequest` or `fetch()` calls where 707 // `Request.destination` is the empty string default value 708 default: 709 cachingStrategyType = CACHE_STRATEGY.default 710 } 711 var cache = null; 712 switch(cachingStrategyType){ 713 case "networkFirst": 714 cache = cachingStrategy.NeworkFirstStrategy(event) 715 break; 716 case "networkOnly": 717 cache = cachingStrategy.networkOnlyStrategy(event) 718 break; 719 //break;
720 case "cacheFirst": 721 case "staleWhileRevalidate": 722 default: 723 cache = cachingStrategy.cacheFirstStrategy(event) 724 break; 725 } 726 event.respondWith(cache); 727 728 } 729); 730 731function openDatabase() { 732 return new Promise((resolve, reject) => { 733 const request = indexedDB.open('shareTargetDB', 1); 734 735 request.onupgradeneeded = (event) => { 736 const db = event.target.result; 737 db.createObjectStore('sharedData', { keyPath: 'id' }); 738 }; 739 740 request.onsuccess = (event) => { 741 resolve(event.target.result); 742 }; 743 744 request.onerror = (event) => { 745 reject(event.target.error); 746 }; 747 }); 748 } 749 750 function saveData(db, data) { 751 return new Promise((resolve, reject) => { 752 const transaction = db.transaction('sharedData', 'readwrite'); 753 const store = transaction.objectStore('sharedData'); 754 755 const request = store.put(data); 756 757 request.onsuccess = () => { 758 resolve(); 759 }; 760 761 request.onerror = (event) => { 762 reject(event.target.error); 763 }; 764 }); 765 } 766 767 768self.addEventListener('message', (event) => { 769 770 if( 771 typeof event.data === 'object' && 772 typeof event.data.action === 'string' 773 ) { 774 switch(event.data.action) { 775 case 'cache' : 776 pwaForWpprecacheUrl(event.data.url); 777 break; 778 779 default : 780 console.log('Unknown action: ' + event.data.action); 781 break; 782 } 783 } 784 785}); 786 787 788importScripts("https://www.gstatic.com/firebasejs/7.8.2/firebase-app.js"); 789importScripts("https://www.gstatic.com/firebasejs/7.8.2/firebase-analytics.js"); 790importScripts("https://www.gstatic.com/firebasejs/7.8.2/firebase-messaging.js"); 791var pnScriptSetting = {"nonce":"b9c8b13b5a","pn_config":{"apiKey":"AIzaSyDhRbFy9m-NXZVkozYJwKdDYJuwsL6W_bw","authDomain":"pushnotificationsio.firebaseapp.com","databaseURL":"https:\/\/pushnotificationsio.firebaseio.com","projectId":"pushnotificationsio","storageBucket":"pushnotificationsio.appspot.com","messagingSenderId":"788493704860","appId":"1:788493704860:web:ba71fd692e7cc9651f5759","measurementId":"G-NXS0Z75BCH"},"swsource":"https:\/\/www.zaaptv.com\/?push_notification_sw=1","scope":"https:\/\/www.zaaptv.com\/","ajax_url":"https:\/\/www.zaaptv.com\/wp-admin\/admin-ajax.php"} 792var config=pnScriptSetting.pn_config; 793if (!firebase.apps.length) {firebase.initializeApp(config);} 794const messaging = firebase.messaging(); 795var messageCount = 0; 796var unreadCount = 0; 797 798messaging.setBackgroundMessageHandler(function(payload) { 799const notificationTitle = payload.data.title; 800const notificationOptions = { 801 body: payload.data.body, 802 icon: payload.data.icon, 803 image: payload.data.image, 804 vibrate: [100, 50, 100], 805 data: { 806 dateOfArrival: Date.now(), 807 primarykey: payload.data.currentCampaign, 808 url : payload.data.url 809 }, 810 } 811 messageCount += 1; 812 setBadge(messageCount); 813 814 return self.registration.showNotification(notificationTitle, notificationOptions); 815 816 817}); 818 819 820self.addEventListener("notificationclose", function(e) { 821var notification = e.notification; 822var primarykey = notification.data.primarykey; 823 messageCount -= 1; 824 unreadCount -= 1;
825 if(messageCount>0 && unreadCount > 0){ 826 setBadge(messageCount); 827 }else{ 828 clearBadge(); 829 } 830 console.log("Closed notification: " + primarykey); 831}); 832 833self.addEventListener("notificationclick", function(e) { 834var notification = e.notification; 835messageCount -= 1; 836unreadCount -= 1; 837 838e.waitUntil( 839 clients.openWindow(notification.data.url).then(function(windowClient) { 840 fetch(pnScriptSetting.ajax_url, { 841 method: 'POST', 842 headers: { 843 'Content-Type': 'application/x-www-form-urlencoded' 844 }, 845 body: `campaign=${notification.data.currentCampaign}&nonce=${pnScriptSetting.nonce}&action=pn_noteclick_subscribers` 846 }); 847 848 if(messageCount>0 && unreadCount > 0){ 849 setBadge(messageCount); 850 }else{ 851 clearBadge(); 852 } 853 }) 854); 855 856notification.close(); // Close the notification 857 858console.log("Clicked notification: " + notification.data); 859}); 860 861 862 863 864function setBadge(...args) { 865 if (navigator.setAppBadge) { 866 navigator.setAppBadge(...args); 867 } else if (navigator.setExperimentalAppBadge) { 868 navigator.setExperimentalAppBadge(...args); 869 } else if (window.ExperimentalBadge) { 870 window.ExperimentalBadge.set(...args); 871 } 872} 873 874// Wrapper to support first and second origin trial 875// See https://web.dev/badging-api/ for details. 876function clearBadge() { 877 if (navigator.clearAppBadge) { 878 navigator.clearAppBadge(); 879 } else if (navigator.clearExperimentalAppBadge) { 880 navigator.clearExperimentalAppBadge(); 881 } else if (window.ExperimentalBadge) { 882 window.ExperimentalBadge.clear(); 883 } 884} 885 886self.addEventListener("push", (event) => { 887 unreadCount += 1; 888 // Set or clear the badge. 889 if (navigator.setAppBadge) { 890 if (unreadCount && unreadCount > 0) { 891 navigator.setAppBadge(unreadCount); 892 } else { 893 navigator.clearAppBadge(); 894 } 895 } 896});
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.