1// Se déclenche au moment ou le SW est register 2self.addEventListener('install', function (event) { 3 self.skipWaiting() 4}); 5 6// Se déclenche quand le SW est "vraiment" utilisé (permet de maj le SW par exemple) 7self.addEventListener('activate', function (event) { 8 // Vous pouvez effectuer des opérations supplémentaires ici si nécessaire 9 console.log("Activate") 10}); 11 12 13self.addEventListener('push', (event) => { 14 const data = event.data ? event.data.json() : {} 15 const decoder = new TextDecoder('utf-8'); 16 const decodedMessage = decoder.decode(new TextEncoder().encode(data.message)); 17 18 let options = { 19 body: decodedMessage, 20 icon: data.icon, 21 image: data.image, 22 data: { 23 url: data.url, 24 id: data.id, 25 } 26 } 27 if (data.url) { 28 options.actions = [ 29 { 30 action: 'go', 31 title: data.titleAction, 32 }] 33 } 34 35 if (data.triggerPost) { 36 let cacheData = { 37 options: options, 38 title: data.title, 39 id: data.id 40 } 41 if (data.trashDate) { 42 cacheData.trashDate = data.trashDate; 43 } 44 // Stockez la notification dans le cache du service worker 45 event.waitUntil( 46 caches.open('notifications').then(function (cache) { 47 for (let i = 0; i < data.triggerPost.length; i++) { 48 let tempTriggerPost = Array.from(data.triggerPost) 49 url = tempTriggerPost.splice(i, 1)[0] 50 cacheData.geminiPost = tempTriggerPost 51 cache.put(url, new Response(JSON.stringify(cacheData))) 52 } 53 }) 54 ); 55 } else { 56 event.waitUntil(self.registration.showNotification(data.title, options)) 57 // Envoyer une requête au serveur pour signaler que la notification a été affichée 58 if (data.id) { 59 fetch('/wp-json/creasit/push/addDisplayed', { 60 method: 'POST', 61 body: JSON.stringify({postID: data.id}), 62 headers: { 63 'Content-Type': 'application/json', 64 }, 65 }); 66 } 67 } 68}) 69 70self.addEventListener("notificationclick", (event) => { 71 event.notification.close(); 72 73 let id = event.notification.data.id 74 let url = event.notification.data.url 75 if (url) { 76 self.clients.openWindow(url); 77 // Envoi une requête au serveur pour signaler le clic sur la notification 78 if (id) { 79 fetch('/wp-json/creasit/push/addClicked', { 80 method: 'POST', 81 body: JSON.stringify({postID: id}), 82 headers: { 83 'Content-Type': 'application/json', 84 }, 85 }); 86 } 87 } 88}); 89 90 91 92
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.