1function invokeServiceWorkerUpdateFlow(registration) { 2 console.log('invokeServiceWorkerUpdateFlow') 3 const notification=document.querySelector('.sitepwa_btn-update') 4 if(!notification) 5 { 6 if(confirm("Une nouvelle version de l'application est disponible. Rafraîchir maintenant ?")) { 7 registration.waiting.postMessage('SKIP_WAITING') 8 } 9 return 10 } 11 notification.removeAttribute('hidden') 12 //notification.show("New version of the app is available. Refresh now?"); 13 console.log("New version of the app is available. Refresh now?"); 14 notification.addEventListener('click', () => { 15 notification.setAttribute('hidden',true) 16 if (registration.waiting) { 17 // let waiting Service Worker know it should became active 18 registration.waiting.postMessage('SKIP_WAITING') 19 } 20 },{once:true}) 21} 22 23function getInstalledStatus() { 24 return navigator.standalone ? navigator.standalone : !!matchMedia("(display-mode: standalone)").matches 25} 26 27 28function showInAppInstallPromotion(deferredPrompt) 29{ 30 console.log('showInAppInstallPromotion') 31 const btn_install=document.querySelector('.sitepwa_btn-install') 32 const dialog_install=document.querySelector('#pwa-install-dialog') 33 if(dialog_install)dialog_install.setAttribute('open',1) 34 else btn_install.removeAttribute('hidden') 35 if(!deferredPrompt) return 36 btn_install.addEventListener('click', async () => { 37 38 deferredPrompt.prompt(); 39 const { outcome } = await deferredPrompt.userChoice; 40 deferredPrompt = null; 41 42 43 if (outcome === 'accepted') { 44 if(!dialog_install) btn_install.setAttribute('hidden',true) 45 console.log('User accepted the install prompt.'); 46 } else if (outcome === 'dismissed') { 47 console.log('User dismissed the install prompt'); 48 } 49 if(dialog_install) dialog_install.removeAttribute('open') 50 }); 51} 52 53function haveAppInstallPromotion() 54{ 55 const btn = document.querySelector('.sitepwa_btn-install') 56 const already_installed=getInstalledStatus(); 57 return btn && !already_installed; 58} 59 60function init_pwa() 61{ 62 if (!('serviceWorker' in navigator)) { 63 console.warn('service worker wont work on this old browser') 64 return; 65 } 66 console.log('init_pwa') 67 68 // if( location.protocol !== 'file:' ) 69 // { 70 // console.warn('service worker wont work from a local file') 71 // return; 72 // } 73 74 navigator.serviceWorker.register(SITE_PWA.url,{ scope: '/' }) 75 .then(function(registration) { 76 console.log('service worker ready',registration); 77 78 // ensure the case when the updatefound event was missed is also handled 79 // by re-invoking the prompt when there's a waiting Service Worker 80 /*if (registration.waiting) { 81 invokeServiceWorkerUpdateFlow(registration) 82 }*/ 83 if(0) { 84 registration.addEventListener("updatefound", () => { 85 console.log("Service Worker update found!"); 86 if (!registration.installing) return 87 // wait until the new Service worker is actually installed (ready to take over) 88 registration.installing.addEventListener('statechange', () => { 89 if (!registration.waiting) return 90 // if there's an existing controller (previous Service Worker), show the prompt 91 if (navigator.serviceWorker.controller) { 92 invokeServiceWorkerUpdateFlow(registration) 93 } else { 94 // otherwise it's the first install, nothing to do 95 console.log('Service Worker initialized for the first time') 96 } 97 }) 98 }); 99 } 100 101 let refreshing = false; 102 103 // detect controller change and refresh the page 104 navigator.serviceWorker.addEventListener('controllerchange', () => { 105 if (refreshing) return; 106 refreshing = true 107 window.location.reload() 108 }) 109 }) 110 .catch(function(error) { 111 console.log('Registration failed with ' + error); 112 113 }); 114 115 if(haveAppInstallPromotion()) { 116 console.log('haveAppInstallPromotion : yes'); 117 // This variable will save the event for later use. 118 let deferredPrompt; 119 window.addEventListener('beforeinstallprompt', (e) => { 120 e.preventDefault(); 121 document.documentElement.classList.remove('pwa-no-beforeinstallprompt') 122 deferredPrompt = e; 123 showInAppInstallPromotion(deferredPrompt); 124 }); 125 if(document.documentElement.classList.contains('pwa-no-beforeinstallprompt')) 126 { 127 showInAppInstallPromotion(null); 128 } 129 } 130 else { 131 console.log('no install promotion') 132 } 133} 134init_pwa()
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.