1/** 2 * DadaSuite 365 â service worker delle notifiche push. 3 * 4 * Questo file viene copiato in `sw.js` nella radice del sito: il service 5 * worker deve stare lì per avere come ambito tutte le pagine. 6 * 7 * A differenza della 1.x non contiene **nessun valore del sito**. Prima veniva 8 * generato da un template PHP che ci scriveva dentro la chiave VAPID e il nome 9 * host, quindi ogni volta che si cambiava la configurazione il file sulla 10 * radice restava indietro e nessuno se ne accorgeva. Qui l'host lo ricava il 11 * worker da sé e l'indirizzo del sensore arriva dentro la notifica, come già 12 * accadeva. Il file è uguale su tutti i siti e non scade mai. 13 * 14 * Sono spariti anche i tre gestori `amp-web-push-*`: il ramo AMP era disattivo 15 * in codice (`$is_amp_active = false`) da prima della 2.0. 16 */ 17 18/** Solo http/https: la destinazione arriva dal servizio push. */ 19function safeUrl( value ) { 20 if ( typeof value !== 'string' || value === '' ) { 21 return null; 22 } 23 24 try { 25 var parsed = new URL( value ); 26 27 return ( parsed.protocol === 'https:' || parsed.protocol === 'http:' ) ? parsed.href : null; 28 } catch ( e ) { 29 return null; 30 } 31} 32 33/** 34 * Notifica al sensore che è successo qualcosa. 35 * 36 * Non si legge la risposta: nella 1.x c'era un `.then(r => r.json())` che 37 * falliva ogni volta che il sensore non rispondeva JSON. 38 */ 39function track( sensor, pushSendId, eventName ) { 40 var base = safeUrl( sensor ); 41 42 if ( ! base ) { 43 return Promise.resolve(); 44 } 45 46 // Stessi parametri, stesso ordine della 1.x: il sensore riceve esattamente 47 // quello che ha sempre ricevuto. 48 var url = base.replace( /\/+$/, '' ) + '/e' + 49 '?site_name=' + encodeURIComponent( self.location.hostname ) + 50 '&push_send_id=' + encodeURIComponent( pushSendId == null ? '' : pushSendId ) + 51 '&event_name=' + encodeURIComponent( eventName ); 52 53 return fetch( url, { method: 'GET', keepalive: true } ).catch( function () {} ); 54} 55 56self.addEventListener( 'install', function () { 57 self.skipWaiting(); 58} ); 59 60self.addEventListener( 'activate', function ( event ) { 61 event.waitUntil( self.clients.claim() ); 62} ); 63 64self.addEventListener( 'push', function ( event ) { 65 // Senza permesso `showNotification` verrebbe rifiutata, e un `waitUntil` 66 // che fallisce fa comparire al browser il suo avviso generico "questo sito 67 // è stato aggiornato in background". La 1.x aveva questo controllo: va 68 // tenuto. 69 if ( ! self.Notification || self.Notification.permission !== 'granted' ) { 70 return; 71 } 72 73 if ( ! event.data ) { 74 return; 75 } 76 77 var message; 78 79 try { 80 message = event.data.json(); 81 } catch ( e ) { 82 return; 83 } 84 85 if ( ! message || ! message.title ) { 86 return; 87 } 88 89 var data = message.data || {}; 90 91 event.waitUntil( 92 Promise.all( [ 93 self.registration.showNotification( message.title, message ), 94 track( data.sensor, data.push_send_id, 'view' ) 95 ] ) 96 ); 97} ); 98 99self.addEventListener( 'notificationclick', function ( event ) { 100 event.notification.close(); 101 102 var data = event.notification.data || {}; 103 var target = safeUrl( data.url ); 104 105 event.waitUntil( 106 Promise.all( [ 107 track( data.sensor, data.push_send_id, 'click' ), 108 target ? self.clients.openWindow( target ) : Promise.resolve() 109 ] ) 110 ); 111} );
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.