From cc8688fa2d8edcf6487b173e8919a6e19e43c71b Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Wed, 26 Feb 2020 09:55:30 +0000 Subject: [PATCH] Add improvements from nwjs branch Former-commit-id: c2a1e9fb7dc342699cd324dc40a9cbe89df1c8ae [formerly ea1d60c4aa9df2c813cd918d3eb6ec7317694b86] Former-commit-id: 851ab23f17e00c7a422645bee147769c4cef0bd0 --- KiwixWebApp.jsproj | 2 +- pwabuilder-sw.js | 6 +++--- www/js/app.js | 19 ++++++++++--------- www/js/lib/filecache.js | 17 ++++++++--------- www/js/lib/util.js | 33 ++------------------------------- 5 files changed, 24 insertions(+), 53 deletions(-) diff --git a/KiwixWebApp.jsproj b/KiwixWebApp.jsproj index 6e64c015..57cc7712 100644 --- a/KiwixWebApp.jsproj +++ b/KiwixWebApp.jsproj @@ -130,7 +130,7 @@ - + diff --git a/pwabuilder-sw.js b/pwabuilder-sw.js index c4896cd7..4cad7ea4 100644 --- a/pwabuilder-sw.js +++ b/pwabuilder-sw.js @@ -75,7 +75,7 @@ const precacheFiles = [ // DEV: add any URL schemata that should be excluded from caching with the Cache API to the regex below // As of 08-2019 the chrome-extension: schema is incompatible with the Cache API // 'example-extension' is included to show how to add another schema if necessary -var excludedURLSchema = /^(?:chrome-extension|file|example-extension):/i; +var excludedURLSchema = /^(?:file|chrome-extension|example-extension):/i; self.addEventListener("install", function (event) { console.log("[SW] Install Event processing"); @@ -240,9 +240,9 @@ function intercept(event) { }, [messageChannel.port2]); }); } else { - if (!excludedURLSchema.test(event.request.url)) return fetch(event.request).then(function (response) { + return fetch(event.request).then(function (response) { // If request was success, add or update it in the cache - event.waitUntil(updateCache(event.request, response.clone())); + if (!excludedURLSchema.test(event.request.url)) event.waitUntil(updateCache(event.request, response.clone())); return response; }).catch(function (error) { console.log("[SW] Network request failed and no cache.", error); diff --git a/www/js/app.js b/www/js/app.js index b9c0d068..6ba277d7 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -1345,14 +1345,15 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'utf8', 'images', 'cooki 'action': 'disable' }); messageChannel = null; - // If we're not in a PWA context, completely unregister the SW - if (!/^https|\/localhost/i.test(window.location.href)) { - navigator.serviceWorker.getRegistrations().then(function(registrations) { - for (var registration of registrations) { - registration.unregister(); - } - }); - } + // If we're in electron or nwjs, completely remove the SW (but we need to keep it active if we're in a PWA) + // NOTE: We can't do this, because IE11 crashes just seeing "of", even though it will never run the code! + // if (typeof window.fs !== 'undefined') { + // navigator.serviceWorker.getRegistrations().then(function(registrations) { + // for (var registration of registrations) { + // registration.unregister(); + // } + // }); + // } } refreshAPIStatus(); } else if (value === 'serviceworker') { @@ -2414,7 +2415,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'utf8', 'images', 'cooki htmlContent = -1; // DEV: You should deal with the rare possibility that the cachedStartPage is not in the same namespace as the main page dirEntry... // Ideally include the namespace in params.cachedStartPage and adjust/test code (not hard) - uiUtil.XHR(dirEntry.namespace + '/' + encodeURIComponent(params.cachedStartPage), 'text', function (responseTxt, status) { + uiUtil.XHR(dirEntry.namespace + '/' + encodeURIComponent(encodeURIComponent(params.cachedStartPage).replace(/%2F/, '/')).replace(/%2F/, '/'), 'text', function (responseTxt, status) { htmlContent = /]*>/.test(responseTxt) ? responseTxt : 0; if (htmlContent) { console.log('Article retrieved from storage cache...'); diff --git a/www/js/lib/filecache.js b/www/js/lib/filecache.js index 31b4a39b..3f75d727 100644 --- a/www/js/lib/filecache.js +++ b/www/js/lib/filecache.js @@ -160,15 +160,15 @@ define(['q'], function(Q) { // We are reading a packaged file and have to use Electron fs.read (so we don't have to pick the file) fs.open(file.path, 'r', function (err, fd) { if (err) { - console.error('Could not find file!', err); reject(err); } else { var size = end - begin; - fs.read(fd, new Uint8Array(size), 0, size, begin, function (err, bytesRead, data) { + var arr = Buffer.alloc(size) || new Uint8Array(size); + fs.read(fd, arr, 0, size, begin, function (err, bytesRead, data) { if (err) reject(err); - else resolve(data); fs.close(fd, function (err) { - if (err) console.log('Could not close file...', err); + if (err) reject(err); + else return resolve(data); }); }); } @@ -176,12 +176,11 @@ define(['q'], function(Q) { } else { var reader = new FileReader(); reader.readAsArrayBuffer(file.slice(begin, end)); - reader.onload = function(e) { + reader.addEventListener('load', function(e) { resolve(new Uint8Array(e.target.result)); - }; - reader.onerror = reader.onabort = function(e) { - reject(e); - }; + }); + reader.addEventListener('error', reject); + reader.addEventListener('abort', reject); } }); }; diff --git a/www/js/lib/util.js b/www/js/lib/util.js index 2709c6f3..87c004ff 100644 --- a/www/js/lib/util.js +++ b/www/js/lib/util.js @@ -21,7 +21,7 @@ */ 'use strict'; -define(['q', 'filecache'], function(q, FileCache) { +define(['q', 'filecache'], function(Q, FileCache) { /** * Utility function : return true if the given string ends with the suffix @@ -211,36 +211,7 @@ define(['q', 'filecache'], function(q, FileCache) { * @returns {Promise} Promise */ function readFileSlice(file, begin, size) { - // var deferred = q.defer(); - // if (file.readMode === 'electron') { - // // We are reading a packaged file and have to use Electron fs.read (so we don't have to pick the file) - // fs.open(file.path, 'r', function (err, fd) { - // if (err) { - // console.error('Could not find file!', err); - // } else { - // fs.read(fd, Buffer.alloc(size), 0, size, begin, function (err, bytesRead, data) { - // if (err) deferred.reject(err); - // else deferred.resolve(data); - // fs.close(fd, function (err) { - // if (err) console.log('Could not close file...', err); - // }); - // }); - // } - // }); - // } else { - // We are reading a picked file, so use vanilla JS methods - // var reader = new FileReader(); - // reader.onload = function (e) { - // deferred.resolve(new Uint8Array(e.target.result)); - // }; - // reader.onerror = reader.onabort = function (e) { - // deferred.reject(e); - // }; - // reader.readAsArrayBuffer(file.slice(begin, begin + size)); - // } - // return deferred.promise; - return FileCache.read(file, begin, begin + size); - // } + return FileCache.read(file, begin, begin + size); } /**