mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-09 12:19:46 -04:00
Integrate new settings store
Former-commit-id: ece2e71a660c9d902b5d0763b4b34a9b10328b0c [formerly 21f505ca969ed4c72b060515c76d5abeee4ad37e] [formerly 66d45d166359063a1ac1172acf31ea3fad90fd70] [formerly bc28dbd485684548ca4a3a36e400439bd36b7475 [formerly a60d78e1f861eea0f0bb2b30ddeb227881c6f1e5 [formerly 3a93dce7af8838d2e1918db1d12f27f6bf84101a]]] Former-commit-id: 0d70283476f3114ef55f3f148fb1df19c666671e [formerly 2e322b00a031eb8deee6c7f0b5c9fc59383d78f4 [formerly a14205ee7904ced1807711628c5c613567cf5b40]] Former-commit-id: 719d216aa1f7f37dcda23c7123b16c76e9ae5960 [formerly 45b586c1194e035eab7dbd871682b5f901dff0ca] Former-commit-id: 909a0a167ec82cd8ab2f7402fff2301e43eca28d
This commit is contained in:
parent
aff3827e0f
commit
40d1fe3c44
@ -26,8 +26,8 @@
|
|||||||
// This uses require.js to structure javascript:
|
// This uses require.js to structure javascript:
|
||||||
// http://requirejs.org/docs/api.html#define
|
// http://requirejs.org/docs/api.html#define
|
||||||
|
|
||||||
define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'cookies', 'q', 'transformStyles', 'kiwixServe'],
|
define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'settingsStore', 'q', 'transformStyles', 'kiwixServe'],
|
||||||
function ($, zimArchiveLoader, uiUtil, util, cache, images, cookies, Q, transformStyles, kiwixServe) {
|
function ($, zimArchiveLoader, uiUtil, util, cache, images, settingsStore, Q, transformStyles, kiwixServe) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The delay (in milliseconds) between two "keepalive" messages
|
* The delay (in milliseconds) between two "keepalive" messages
|
||||||
@ -52,7 +52,9 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'cook
|
|||||||
'type': '' // The type of the search: 'basic'|'full' (set automatically in search algorithm)
|
'type': '' // The type of the search: 'basic'|'full' (set automatically in search algorithm)
|
||||||
};
|
};
|
||||||
|
|
||||||
// A parameter to determine the Settings Store API in use
|
// A parameter to determine the Settings Store API in use (we need to nullify before testing)
|
||||||
|
// because params.storeType is also set in a preliminary way in init.js
|
||||||
|
params['storeType'] = null;
|
||||||
params['storeType'] = settingsStore.getBestAvailableStorageAPI();
|
params['storeType'] = settingsStore.getBestAvailableStorageAPI();
|
||||||
|
|
||||||
// Unique identifier of the article expected to be displayed
|
// Unique identifier of the article expected to be displayed
|
||||||
@ -347,7 +349,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'cook
|
|||||||
params.lastPageHTML = "";
|
params.lastPageHTML = "";
|
||||||
if (typeof Storage !== "undefined") {
|
if (typeof Storage !== "undefined") {
|
||||||
try {
|
try {
|
||||||
localStorage.setItem('lastPageHTML', "");
|
localStorage.setItem(params.keyPrefix + 'lastPageHTML', "");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("localStorage not supported: " + err);
|
console.log("localStorage not supported: " + err);
|
||||||
}
|
}
|
||||||
@ -1238,7 +1240,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'cook
|
|||||||
//Clear localStorage
|
//Clear localStorage
|
||||||
if (typeof Storage !== "undefined") {
|
if (typeof Storage !== "undefined") {
|
||||||
try {
|
try {
|
||||||
localStorage.setItem('lastPageHTML', "");
|
localStorage.setItem(params.keyPrefix + 'lastPageHTML', "");
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("localStorage not supported: " + err);
|
console.log("localStorage not supported: " + err);
|
||||||
@ -2728,7 +2730,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'cook
|
|||||||
if (params.rememberLastPage && (typeof Storage !== "undefined") && dirEntry.namespace + '/' + dirEntry.url == lastPage) {
|
if (params.rememberLastPage && (typeof Storage !== "undefined") && dirEntry.namespace + '/' + dirEntry.url == lastPage) {
|
||||||
if (!params.lastPageHTML) {
|
if (!params.lastPageHTML) {
|
||||||
try {
|
try {
|
||||||
params.lastPageHTML = localStorage.getItem('lastPageHTML');
|
params.lastPageHTML = localStorage.getItem(params.keyPrefix + 'lastPageHTML');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("localStorage not supported: " + err);
|
console.log("localStorage not supported: " + err);
|
||||||
}
|
}
|
||||||
@ -2994,8 +2996,8 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'cook
|
|||||||
//Store current document's raw HTML in localStorage for fast restart
|
//Store current document's raw HTML in localStorage for fast restart
|
||||||
try {
|
try {
|
||||||
// Ensure we don't go over quota
|
// Ensure we don't go over quota
|
||||||
localStorage.removeItem('lastPageHTML');
|
localStorage.removeItem(params.keyPrefix + 'lastPageHTML');
|
||||||
localStorage.setItem('lastPageHTML', htmlArticle);
|
localStorage.setItem(params.keyPrefix + 'lastPageHTML', htmlArticle);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (/quota\s*exceeded/i.test(err.message)) {
|
if (/quota\s*exceeded/i.test(err.message)) {
|
||||||
// Note that Edge gives a quotaExceeded message when running from localhost even if the quota isn't exceeded
|
// Note that Edge gives a quotaExceeded message when running from localhost even if the quota isn't exceeded
|
||||||
|
@ -57,6 +57,7 @@ params['cachedStartPage'] = false; //If you have cached the start page for quick
|
|||||||
params['kiwixDownloadLink'] = "https://download.kiwix.org/zim/"; //Include final slash
|
params['kiwixDownloadLink'] = "https://download.kiwix.org/zim/"; //Include final slash
|
||||||
|
|
||||||
params['storeType'] = checkCookies();
|
params['storeType'] = checkCookies();
|
||||||
|
params['keyPrefix'] = 'kiwixjs-'; // Prefix to use for localStorage keys
|
||||||
params['maxResults'] = ~~(getCookie('maxResults') || 25); //Number of search results to display
|
params['maxResults'] = ~~(getCookie('maxResults') || 25); //Number of search results to display
|
||||||
params['relativeFontSize'] = ~~(getCookie('relativeFontSize') || 100); //Sets the initial font size for articles (as a percentage) - user can adjust using zoom buttons
|
params['relativeFontSize'] = ~~(getCookie('relativeFontSize') || 100); //Sets the initial font size for articles (as a percentage) - user can adjust using zoom buttons
|
||||||
params['relativeUIFontSize'] = ~~(getCookie('relativeUIFontSize') || 100); //Sets the initial font size for UI (as a percentage) - user can adjust using slider in Config
|
params['relativeUIFontSize'] = ~~(getCookie('relativeUIFontSize') || 100); //Sets the initial font size for UI (as a percentage) - user can adjust using slider in Config
|
||||||
@ -102,16 +103,14 @@ params['PWAInstalled'] = decodeURIComponent(getCookie('PWAInstalled'));
|
|||||||
params.pagesLoaded = 0; // Page counter used to show PWA Install Prompt only after user has played with the app for a while
|
params.pagesLoaded = 0; // Page counter used to show PWA Install Prompt only after user has played with the app for a while
|
||||||
|
|
||||||
//Prevent app boot loop with problematic pages that cause an app crash
|
//Prevent app boot loop with problematic pages that cause an app crash
|
||||||
if (getCookie('lastPageLoad') == 'failed') {
|
if (getCookie('lastPageLoad') === 'failed') {
|
||||||
params.lastPageVisit = "";
|
params.lastPageVisit = "";
|
||||||
} else {
|
} else {
|
||||||
//Cookie will signal failure until article is fully loaded
|
//Cookie will signal failure until article is fully loaded
|
||||||
if (typeof window.fs === 'undefined') {
|
if (params.storeType === 'cookie') {
|
||||||
document.cookie = 'lastPageLoad=failed;expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
document.cookie = 'lastPageLoad=failed;expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
||||||
} else {
|
} else if (params.storeType === 'local_storage') {
|
||||||
if (typeof localStorage !== 'undefined') {
|
localStorage.setItem(params.keyPrefix + 'lastPageLoad', 'failed');
|
||||||
localStorage.setItem('lastPageLoad', 'failed');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,26 +235,24 @@ function installApp(e) {
|
|||||||
|
|
||||||
window.addEventListener('appinstalled', function(e) {
|
window.addEventListener('appinstalled', function(e) {
|
||||||
params.PWAInstalled = params.version;
|
params.PWAInstalled = params.version;
|
||||||
document.cookie = 'PWAInstalled=' + encodeURIComponent(params.PWAInstalled) + ';expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
if (params.storeType === 'cookie') {
|
||||||
|
document.cookie = 'PWAInstalled=' + encodeURIComponent(params.PWAInstalled) + ';expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
||||||
|
} else if (params.storeType === 'local_storage') {
|
||||||
|
localStorage.setItem(params.keyPrefix + 'PWAInstalled', params.PWAInstalled);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function getCookie(name) {
|
function getCookie(name) {
|
||||||
var result;
|
var result;
|
||||||
if (params.storeType == 'cookie') {
|
if (params.storeType === 'cookie') {
|
||||||
var regexp = new RegExp('(?:^|;)\\s*' + name + '=([^;]+)(?:;|$)');
|
var regexp = new RegExp('(?:^|;)\\s*' + name + '=([^;]+)(?:;|$)');
|
||||||
result = document.cookie.match(regexp);
|
result = document.cookie.match(regexp);
|
||||||
result = result && result.length > 1 ? result[1] : null;
|
result = result && result.length > 1 ? result[1] : null;
|
||||||
} else {
|
} else if (params.storeType === 'local_storage') {
|
||||||
// We're in an electron app that may not be able to access cookies, so use localStorage instead
|
// Use localStorage instead
|
||||||
if (typeof Storage !== 'undefined') {
|
result = localStorage.getItem(params.keyPrefix + name);
|
||||||
try {
|
|
||||||
result = localStorage.getItem(name);
|
|
||||||
} catch (err) {
|
|
||||||
console.log("localStorage not supported: " + err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result === null || result == "undefined" ? null : result == "true" ? true : result == "false" ? false : result;
|
return result === null || result === "undefined" ? null : result === "true" ? true : result === "false" ? false : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkCookies() {
|
function checkCookies() {
|
||||||
@ -268,16 +265,14 @@ function checkCookies() {
|
|||||||
kiwixCookie = !/kiwixCookie=working/i.test(document.cookie);
|
kiwixCookie = !/kiwixCookie=working/i.test(document.cookie);
|
||||||
}
|
}
|
||||||
document.cookie = 'kiwixCookie=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
document.cookie = 'kiwixCookie=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
||||||
if (!kiwixCookie) {
|
// Test for localStorage support
|
||||||
// Cookies appear to be blocked, so test for localStorage support
|
var result = false;
|
||||||
var result = false;
|
try {
|
||||||
try {
|
result = 'localStorage' in window && window['localStorage'] !== null;
|
||||||
result = 'localStorage' in window && window['localStorage'] !== null;
|
} catch (e) {
|
||||||
} catch (e) {
|
console.log('LocalStorage is not supported!');
|
||||||
console.log('LocalStorage is not supported!');
|
|
||||||
}
|
|
||||||
if (result) storeType = 'local_storage';
|
|
||||||
}
|
}
|
||||||
|
if (result) storeType = 'local_storage';
|
||||||
console.log('Test1: storeType: ' + storeType);
|
console.log('Test1: storeType: ' + storeType);
|
||||||
return storeType;
|
return storeType;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
define(['q', 'uiUtil'], function(Q, uiUtil) {
|
define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) {
|
||||||
|
|
||||||
var CACHE_NAME = 'kiwixjs-assetCache'; // Set the database or cache name here
|
var CACHE_NAME = 'kiwixjs-assetCache'; // Set the database or cache name here
|
||||||
var objStore = 'Assets'; // Name of the object store
|
var objStore = 'Assets'; // Name of the object store
|
||||||
@ -268,27 +268,26 @@ define(['q', 'uiUtil'], function(Q, uiUtil) {
|
|||||||
*/
|
*/
|
||||||
function setArticle(zimFile, article, content, callback) {
|
function setArticle(zimFile, article, content, callback) {
|
||||||
// Prevent storage if user has deselected the option in Configuration
|
// Prevent storage if user has deselected the option in Configuration
|
||||||
if (/rememberLastPage=false\b/i.test(document.cookie)) {
|
if (!params.rememberLastPage) {
|
||||||
callback(-1);
|
callback(-1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
document.cookie = zimFile + '=' + encodeURIComponent(article) + ';expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
settingsStore.setItem(zimFile, article, Infinity);
|
||||||
setItem(zimFile, content, function(response) {
|
setItem(zimFile, content, function(response) {
|
||||||
callback(response);
|
callback(response);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves article contents from cache only if the article's key has been stored in cookie
|
* Retrieves article contents from cache only if the article's key has been stored in settings store
|
||||||
* (since checking the cookie is synchronous, it prevents unnecessary async cache lookups)
|
* (since checking the store is synchronous, it prevents unnecessary async cache lookups)
|
||||||
*
|
*
|
||||||
* @param {String} zimFile The filename (or name of first file in set) of the ZIM archive
|
* @param {String} zimFile The filename (or name of first file in set) of the ZIM archive
|
||||||
* @param {String} article The URL of the article to be retrieved (including namespace)
|
* @param {String} article The URL of the article to be retrieved (including namespace)
|
||||||
* @param {Function} callback The function to call with the result
|
* @param {Function} callback The function to call with the result
|
||||||
*/
|
*/
|
||||||
function getArticle(zimFile, article, callback) {
|
function getArticle(zimFile, article, callback) {
|
||||||
var encodedTitle = encodeURIComponent(article);
|
if (settingsStore.getItem(zimFile) === article) {
|
||||||
if (~document.cookie.indexOf(zimFile + '=' + encodedTitle)) {
|
|
||||||
getItem(zimFile, callback);
|
getItem(zimFile, callback);
|
||||||
} else {
|
} else {
|
||||||
callback(false);
|
callback(false);
|
||||||
@ -305,7 +304,7 @@ define(['q', 'uiUtil'], function(Q, uiUtil) {
|
|||||||
function setItem(key, contents, callback) {
|
function setItem(key, contents, callback) {
|
||||||
// Prevent use of storage if user has deselected the option in Configuration
|
// Prevent use of storage if user has deselected the option in Configuration
|
||||||
// or if the asset is of the wrong type
|
// or if the asset is of the wrong type
|
||||||
if (/useCache=false\b/i.test(document.cookie) || !regexpKeyTypes.test(key)) {
|
if (params.useCache === false || !regexpKeyTypes.test(key)) {
|
||||||
callback(-1);
|
callback(-1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -412,7 +411,7 @@ define(['q', 'uiUtil'], function(Q, uiUtil) {
|
|||||||
if (!/\.css$|\.js$/.test(key)) {
|
if (!/\.css$|\.js$/.test(key)) {
|
||||||
document.getElementById('cachingAssets').style.display = 'none';
|
document.getElementById('cachingAssets').style.display = 'none';
|
||||||
document.getElementById('searchingArticles').style.display = 'block';
|
document.getElementById('searchingArticles').style.display = 'block';
|
||||||
} else if (/useCache=true\b/i.test(document.cookie)) {
|
} else if (params.useCache !== false) {
|
||||||
var shortTitle = key.replace(/[^/]+\//g, '').substring(0, 18);
|
var shortTitle = key.replace(/[^/]+\//g, '').substring(0, 18);
|
||||||
document.getElementById('cachingAssets').innerHTML = 'Getting ' + shortTitle + '...';
|
document.getElementById('cachingAssets').innerHTML = 'Getting ' + shortTitle + '...';
|
||||||
document.getElementById('cachingAssets').style.display = 'block';
|
document.getElementById('cachingAssets').style.display = 'block';
|
||||||
|
@ -35,7 +35,7 @@ define([], function () {
|
|||||||
var regexpCookieKeysToMigrate = new RegExp([
|
var regexpCookieKeysToMigrate = new RegExp([
|
||||||
'lastContentInjectionMode', 'lastSelectedArchivePath', 'imageDisplay', 'useMathJax', 'version', 'lastSelectedArchive',
|
'lastContentInjectionMode', 'lastSelectedArchivePath', 'imageDisplay', 'useMathJax', 'version', 'lastSelectedArchive',
|
||||||
'listOfArchives', 'lastPageVisit', 'cssUITheme', 'cssTheme', 'cssSource', 'removePageMaxWidth', 'hideActiveContentWarning',
|
'listOfArchives', 'lastPageVisit', 'cssUITheme', 'cssTheme', 'cssSource', 'removePageMaxWidth', 'hideActiveContentWarning',
|
||||||
'allowHTMLExtraction'
|
'allowHTMLExtraction', 'PWAInstalled', 'lastPageLoad', 'hideToolbars', 'showFileSelectors', 'cssCache'
|
||||||
].join('|'));
|
].join('|'));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -43,7 +43,7 @@ define([], function () {
|
|||||||
* potential collision of key names with localStorage keys used by code inside ZIM archives
|
* potential collision of key names with localStorage keys used by code inside ZIM archives
|
||||||
* @type {String}
|
* @type {String}
|
||||||
*/
|
*/
|
||||||
const keyPrefix = 'kiwixjs-';
|
const keyPrefix = params.keyPrefix || 'kiwixjs-';
|
||||||
|
|
||||||
// Tests for available Storage APIs (document.cookie or localStorage) and returns the best available of these
|
// Tests for available Storage APIs (document.cookie or localStorage) and returns the best available of these
|
||||||
function getBestAvailableStorageAPI() {
|
function getBestAvailableStorageAPI() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user