From 3154de3e3cea2bc7efe7362d9f6a7d92880a4cbc Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Sat, 26 Oct 2024 14:53:53 +0100 Subject: [PATCH] Add beforeunload interceptor if archive is loaded Fixes #645 --- www/js/init.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/www/js/init.js b/www/js/init.js index 6dd13284..013993f2 100644 --- a/www/js/init.js +++ b/www/js/init.js @@ -32,6 +32,18 @@ window.onerror = function (msg, url, line, col, error) { return true; }; +// Set a beforeUnload handler to prevent app reloads without confirmation if a ZIM file is loaded +window.addEventListener('beforeunload', function (event) { + if (appstate && appstate.selectedArchive && params.appCache) { + var confirmationMessage = 'Warning: you may have to reload the ZIM archive if you leave this page!'; + event.preventDefault(); + // Included for legacy support, e.g. Chrome/Edge < 119 + event.returnValue = confirmationMessage; + // For modern browsers + return confirmationMessage; + } +}); + /** * Provides caching for assets contained in ZIM (variable needs to be available app-wide) * It significantly speeds up subsequent page display. See kiwix-js issue #335