From c79f1ac3ef01a61d178220b9e2d41523bc4a11f2 Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Sun, 24 Jan 2021 12:08:15 +0000 Subject: [PATCH] Preliminary code to enable SW mode in UWP # Conflicts: # package.appxmanifest Former-commit-id: b015ab8cc95553be1a09c6ab8e83cfac2b41b94e [formerly ba2c1cf22a4ee3b4c2b2a0d73e6b1c9e6329a034 [formerly 5088eb4fd5b3b1fa3b37cf1ad702383b29cc1ccb]] Former-commit-id: 88ddfaacb2d91c68546b44bd2852817b7b56b5f8 Former-commit-id: b436bcd69068e7f34a6b3c9ed9bb45ae01153d1a --- package.appxmanifest | 5 +++-- www/js/app.js | 17 ++++++++++++++++- www/js/init.js | 9 +++++++++ www/js/lib/uiUtil.js | 9 ++++++++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/package.appxmanifest b/package.appxmanifest index bfbb68ad..82ea5a4b 100644 --- a/package.appxmanifest +++ b/package.appxmanifest @@ -1,5 +1,5 @@  - + @@ -16,7 +16,8 @@ - + + diff --git a/www/js/app.js b/www/js/app.js index cf2e63bb..06db8fd7 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -1541,6 +1541,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett }, function (err) { console.error('error while registering serviceWorker', err); refreshAPIStatus(); + var goPWA = false; var message = "The ServiceWorker could not be properly registered. Switching back to jQuery mode. Error message : " + err; var protocol = window.location.protocol; if (protocol === 'moz-extension:') { @@ -1548,8 +1549,22 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett message += "\nPlease vote for https://bugzilla.mozilla.org/show_bug.cgi?id=1344561 so that some future Firefox versions support it"; } else if (protocol === 'file:') { message += "\n\nYou seem to be opening kiwix-js with the file:// protocol. You should open it through a web server : either through a local one (http://localhost/...) or through a remote one (but you need SSL : https://webserver/...)"; + } else if (protocol === 'ms-appx-web:') { + message = 'This UWP app uses locally packaged code by default.\n' + + 'To enable the Service Worker we need to switch to PWA mode.\n\n' + + 'WARNING: This will attempt to access the server: ' + params.PWAServer; + goPWA = true; + } + if (goPWA) { + uiUtil.systemAlert(message, 'Warning!', 'Access server', function () { + settingsStore.setItem('lastContentInjectionMode', value, Infinity); + window.location.href = 'https://kiwix.github.io/kiwix-js-windows/www/index.html?contentInjectionMode=serviceworker'; + }, 'Cancel', function () { + document.getElementById('btnConfigure').click(); + }); + } else { + uiUtil.systemAlert(message, 'Information'); } - uiUtil.systemAlert(message); setContentInjectionMode("jquery"); return; }); diff --git a/www/js/init.js b/www/js/init.js index 32f11dcb..dd9ab0fc 100644 --- a/www/js/init.js +++ b/www/js/init.js @@ -56,6 +56,8 @@ params['archivePath'] = "archives"; //The directory containing the packaged arch params['fileVersion'] = "wikipedia_en_100_maxi_2021-01.zim (23-Jan-2021)"; //Use generic name for actual file, and give version here params['cachedStartPage'] = false; //If you have cached the start page for quick start, give its URI here params['kiwixDownloadLink'] = "https://download.kiwix.org/zim/"; //Include final slash +params['PWAServer'] = "https://kiwix.github.io/kiwix-js-windows/"; +params['PWAMode'] = getSetting('PWAMode'); // Set to true if the app should always operate in PWA mode params['storeType'] = getBestAvailableStorageAPI(); params['keyPrefix'] = 'kiwixjs-'; // Prefix to use for localStorage keys @@ -104,6 +106,13 @@ params['PWAInstalled'] = getSetting('PWAInstalled'); params['appType'] = getAppType(); params.pagesLoaded = 0; // Page counter used to show PWA Install Prompt only after user has played with the app for a while +// Make sure we are accessing the correct server according to the PWA setting +if (params.PWAMode && !~window.location.href.indexOf(params.PWAServer) && /UWP/.test(params.appType)) { + // User wants PWA mode so reload now + window.location.href = params.PWAServer; +} + + //Prevent app boot loop with problematic pages that cause an app crash if (getSetting('lastPageLoad') === 'failed') { params.lastPageVisit = ""; diff --git a/www/js/lib/uiUtil.js b/www/js/lib/uiUtil.js index 8aa1a1f4..55c4f0e5 100644 --- a/www/js/lib/uiUtil.js +++ b/www/js/lib/uiUtil.js @@ -529,11 +529,18 @@ define(rqDef, function() { * Provides system-specific alert function * * @param {String} message The message to display + * @param {String} title The message title + * @param {String} btn1 An optional button to display + * @param {Function} btn1Func An optional function to run when btn1 is selected + * @param {String} btn2 An optional secondary button to display + * @param {Function} btn2Func An optional function to run when btn2 is selected */ - function systemAlert(message) { + function systemAlert(message, title, btn1, btn1Func, btn2, btn2Func) { // Test for UWP if (typeof Windows !== 'undefined' && typeof Windows.UI !== 'undefined' && typeof Windows.UI.Popups !== 'undefined') { var dialog = new Windows.UI.Popups.MessageDialog(message); + if (btn1 && btn1Func) dialog.commands.append(new Windows.UI.Popups.UICommand(btn1, btn1Func)); + if (btn2 && btn2Func) dialog.commands.append(new Windows.UI.Popups.UICommand(btn2, btn2Func)); dialog.showAsync(); } else { alert(message);