From c2451ba49ebb5774f83bbc5a8aa0b3de6ce8d934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=27rysiek=27=20Wo=C5=BAniak?= Date: Sun, 19 Sep 2021 11:56:57 +0000 Subject: [PATCH] service-worker status indicator works well again (ref. #16) --- libresilient.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/libresilient.js b/libresilient.js index 9f64d99..36cc007 100644 --- a/libresilient.js +++ b/libresilient.js @@ -472,23 +472,6 @@ libresilient.displayMessage = (msg) => { */ window.addEventListener('load', function() { libresilient.status = "loaded"; - /* - * status display: how did this file get fetched? - * - * yes, this code has to be directly here, - * since we want to know how *this exact file* got fetched - */ - if (typeof libresilient.info[window.location.href] === 'object') { - // service worker info - for (let libresilient_sw of document.querySelectorAll(".libresilient-status-service-worker")) { - libresilient_sw.className += " active"; - try { - libresilient_sw.querySelector('.status').innerHTML = "yes"; - } catch(e) { - // nothing to do here, move along - } - } - } // was any content unavailable so far? if (libresilient.contentUnavailable) { libresilient.displayMessage('Some content seems unavailable. Attempting to retrieve it via LibResilient.') @@ -527,6 +510,7 @@ if ('serviceWorker' in navigator) { // handling the messages from ServiceWorker navigator.serviceWorker.addEventListener('message', event => { + self.log('browser-side', 'LibResilientInfo received!') if (event.data.url) { self.log('browser-side', '+-- for:', event.data.url) @@ -595,7 +579,23 @@ if ('serviceWorker' in navigator) { } } if (event.data.clientId) { + self.log('browser-side', '+-- got our clientId:', event.data.clientId) + + // if libresilient.clientId is null, this is the first time + // we got wind that the service worker is running + // service worker info + if (libresilient.clientId === null) { + for (let libresilient_sw of document.querySelectorAll(".libresilient-status-service-worker")) { + libresilient_sw.className += " active"; + try { + libresilient_sw.querySelector('.status').innerHTML = "yes"; + } catch(e) { + // nothing to do here, move along + } + } + } + // set the clientId internally, we will need it libresilient.clientId = event.data.clientId } if (event.data.plugins) {