Copy some code improvements from Kiwix JS

This commit is contained in:
Jaifroid 2023-11-15 18:17:03 +00:00
parent 36c492a7b3
commit 1d3c383413
2 changed files with 36 additions and 24 deletions

View File

@ -4,21 +4,21 @@
* corresponding content, coming from the archive * corresponding content, coming from the archive
* *
* Copyright 2022 Mossroy, Jaifroid and contributors * Copyright 2022 Mossroy, Jaifroid and contributors
* License GPL v3: * Licence GPL v3:
* *
* This file is part of Kiwix. * This file is part of Kiwix.
* *
* Kiwix is free software: you can redistribute it and/or modify * Kiwix is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public Licence as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the Licence, or
* (at your option) any later version. * (at your option) any later version.
* *
* Kiwix is distributed in the hope that it will be useful, * Kiwix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public Licence for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public Licence
* along with Kiwix (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/> * along with Kiwix (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/ */
'use strict'; 'use strict';
@ -232,6 +232,16 @@ if ('WebAssembly' in self) {
); );
} }
/**
* If we're in a Chromium extension, add a listener to launch the tab when the icon is clicked
*/
if (typeof chrome !== 'undefined' && chrome.action) {
chrome.action.onClicked.addListener(function () {
var newURL = chrome.runtime.getURL('www/index.html');
chrome.tabs.create({ url: newURL });
});
}
// Process install event // Process install event
self.addEventListener('install', function (event) { self.addEventListener('install', function (event) {
console.debug('[SW] Install Event processing'); console.debug('[SW] Install Event processing');
@ -242,8 +252,7 @@ self.addEventListener('install', function (event) {
return new Request(urlPath + '?v' + appVersion, { cache: 'no-cache' }); return new Request(urlPath + '?v' + appVersion, { cache: 'no-cache' });
}); });
if (!regexpExcludedURLSchema.test(requests[0].url)) { if (!regexpExcludedURLSchema.test(requests[0].url)) {
event.waitUntil( event.waitUntil(caches.open(APP_CACHE).then(function (cache) {
caches.open(APP_CACHE).then(function (cache) {
return Promise.all( return Promise.all(
requests.map(function (request) { requests.map(function (request) {
return fetch(request).then(function (response) { return fetch(request).then(function (response) {
@ -255,8 +264,7 @@ self.addEventListener('install', function (event) {
}); });
}) })
); );
}) }));
);
} }
}); });
@ -514,10 +522,14 @@ function fetchUrlFromZIM (urlObject, range) {
*/ */
function fromCache (cache, requestUrl) { function fromCache (cache, requestUrl) {
// Prevents use of Cache API if user has disabled it // Prevents use of Cache API if user has disabled it
if (!(useAppCache && cache === APP_CACHE || useAssetsCache && cache === ASSETS_CACHE)) return Promise.reject(new Error('Cache disabled')); if (!(useAppCache && cache === APP_CACHE || useAssetsCache && cache === ASSETS_CACHE)) {
return Promise.reject(new Error('Cache disabled'));
}
return caches.open(cache).then(function (cacheObj) { return caches.open(cache).then(function (cacheObj) {
return cacheObj.match(requestUrl).then(function (matching) { return cacheObj.match(requestUrl).then(function (matching) {
if (!matching || matching.status === 404) return Promise.reject(new Error('no-match')); if (!matching || matching.status === 404) {
return Promise.reject(new Error('no-match'));
}
console.debug('[SW] Supplying ' + requestUrl + ' from ' + cache + '...'); console.debug('[SW] Supplying ' + requestUrl + ' from ' + cache + '...');
return matching; return matching;
}); });

View File

@ -2832,7 +2832,7 @@ setContentInjectionMode(params.contentInjectionMode);
// } // }
/** /**
* Tells if the ServiceWorker API is available * Detects whether the ServiceWorker API is available
* https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker * https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker
* @returns {Boolean} * @returns {Boolean}
*/ */
@ -2841,7 +2841,7 @@ function isServiceWorkerAvailable () {
} }
/** /**
* Tells if the MessageChannel API is available * Detects whether the MessageChannel API is available
* https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel * https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel
* @returns {Boolean} * @returns {Boolean}
*/ */