/**
* reset.js : Provide utilities for resetting the app to a fresh state
* Copyright 2024 Jaifroid and contributors
* License GPL v3:
*
* This file is part of Kiwix.
*
* Kiwix is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Kiwix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kiwix (file LICENSE-GPLv3.txt). If not, see
WARNING: This will reset the app to a freshly installed state, deleting all app caches,' + // ' Archives stored in the Private File System,' + ' and settings! (Archives stored in the OPFS will be preserved.)
Make sure you have an Internet connection' + ' if this is an offline PWA, because it will be erased and reloaded.
', 'Warning!', true).then(function (confirm) { if (confirm) performReset(); else console.debug('User cancelled'); }); } } // Gets cache names from Service Worker, as we cannot rely on having them in params.cacheNames function getCacheNames (callback) { if (navigator.serviceWorker && navigator.serviceWorker.controller) { var channel = new MessageChannel(); channel.port1.onmessage = function (event) { var names = event.data; callback(names); }; navigator.serviceWorker.controller.postMessage({ action: 'getCacheNames' }, [channel.port2]); } else { callback(null); } } // Deregisters all Service Workers and reboots the app function reloadApp () { var reboot = function () { // Temporarily disable the beforeunload event listener params.interceptBeforeUnload = false; console.debug('Performing app reload...'); setTimeout(function () { window.location.href = location.origin + location.pathname + uriParams }, 600); }; // Blank the querystring, so that parameters are not set on reload var uriParams = ''; if (~window.location.href.indexOf(params.PWAServer) && params.referrerExtensionURL) { // However, if we're in a PWA that was called from local code, then by definition we must remain in SW mode and we need to // ensure the user still has access to the referrerExtensionURL (so they can get back to local code from the UI) uriParams = '?allowInternetAccess=truee&contentInjectionMode=serviceworker'; uriParams += '&referrerExtensionURL=' + encodeURIComponent(params.referrerExtensionURL); } if (navigator && navigator.serviceWorker) { console.debug('Deregistering Service Workers...'); var cnt = 0; navigator.serviceWorker.getRegistrations().then(function (registrations) { if (!registrations.length) { reboot(); return; } cnt++; registrations.forEach(function (registration) { registration.unregister().then(function () { cnt--; if (!cnt) { console.debug('All Service Workers unregistered...'); reboot(); } }); }); }).catch(function (err) { console.error(err); reboot(); }); } else { console.debug('Performing app reload...'); reboot(); } } export default { reset: reset, reloadApp: reloadApp, getCacheNames: getCacheNames }