Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support localStorage for storing settings #613

Merged
merged 35 commits into from Apr 14, 2020
Merged
Changes from 6 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ab47958
Support localStorage for storing settings
Jaifroid Apr 4, 2020
8046503
Put test into a function
Jaifroid Apr 4, 2020
98b5a00
Fix for IE11
Jaifroid Apr 5, 2020
1b3c505
Add clearer documentation
Jaifroid Apr 5, 2020
2dc6fac
More documentation
Jaifroid Apr 5, 2020
a078f23
Simplify code
Jaifroid Apr 5, 2020
abf0bd4
Better format of console log message
Jaifroid Apr 5, 2020
2c10e74
Fix some review requests
Jaifroid Apr 5, 2020
45244e6
Report API in status panel
Jaifroid Apr 6, 2020
3acae3e
Complete API and status panel
Jaifroid Apr 6, 2020
6394eae
Update latest Mozilla cookies framework
Jaifroid Apr 6, 2020
612d90f
Rename cookies.js to settingsStore.js
Jaifroid Apr 6, 2020
7a2e7b5
Ensure cookies implementation runs even if storeType is 'none'
Jaifroid Apr 8, 2020
f683aad
Move API panel-setting code to app.js
Jaifroid Apr 8, 2020
03727d5
Codefactor changes (unnecessary escape characters)
Jaifroid Apr 10, 2020
86dbbb9
Use standard format for header
Jaifroid Apr 10, 2020
b9f1323
Return Boolean values from cookie
Jaifroid Apr 10, 2020
459db34
Accept Boolean values too
Jaifroid Apr 10, 2020
270594c
Add migration code
Jaifroid Apr 10, 2020
99dba53
Revert accepting Boolean values
Jaifroid Apr 10, 2020
dc4a5ba
Revert returning Boolean values
Jaifroid Apr 10, 2020
d4b5d87
Correct documentation
Jaifroid Apr 10, 2020
3e2aef3
Add console message for migration
Jaifroid Apr 10, 2020
88641e7
Fix more codeFactor issues
Jaifroid Apr 10, 2020
a53f843
Add diagnostic messages
Jaifroid Apr 10, 2020
14e6e43
Make cookieKeys() a private function
Jaifroid Apr 10, 2020
7dee737
Remove cookieKeys from documentation
Jaifroid Apr 10, 2020
f2ea273
Change some references to "cookie" in app.js
Jaifroid Apr 10, 2020
836e35d
Add list of keys to migrate
Jaifroid Apr 12, 2020
21a9dd5
Correct JSDoc type statement
Jaifroid Apr 12, 2020
21f0e14
Do not initiate migration if keys to migrate are not in cookie
Jaifroid Apr 12, 2020
f17dae1
Correct documentation
Jaifroid Apr 12, 2020
4e154c3
Change function names
Jaifroid Apr 13, 2020
c3e1bf1
Add 'kiwixjs-' prefix to keys in localStorage
Jaifroid Apr 13, 2020
34ce7d9
Add a constant for the keyPrefix
Jaifroid Apr 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 46 additions & 7 deletions www/js/lib/cookies.js
Expand Up @@ -21,11 +21,43 @@ define([], function() {
|*|
\*/

var storeType = testStorageSupport();

// Tests for localStorage support
function testStorageSupport() {
// Prefer localStorage if supported due to some platforms blocking cookies in local contexts
// DEV: In FF extensions, cookies are blocked since at least FF 68.6 but possibly since FF 55 [kiwix-js #612]
var type = 'local_storage';
// First test for localStorage support
var localStorageTest = false;
try {
localStorageTest = 'localStorage' in window && window['localStorage'] !== null;
// DEV: Above test returns true in IE11 running from file:// protocol, but attempting to write a key to
// localStorage causes an exception; so to test fully, we must now attempt to write and remove a test key
if (localStorageTest) {
localStorage.setItem('kiwixStorage', '');
Jaifroid marked this conversation as resolved.
Show resolved Hide resolved
localStorage.removeItem('kiwixStorage');
}
} catch (e) {
localStorageTest = false;
console.log('LocalStorage is not supported!');
Jaifroid marked this conversation as resolved.
Show resolved Hide resolved
}
// Fall back to cookie if localStorage fails
if (!localStorageTest) type = 'cookie';
console.log('Storage test: type: ' + type);
Jaifroid marked this conversation as resolved.
Show resolved Hide resolved
return type;
}

var docCookies = {
Jaifroid marked this conversation as resolved.
Show resolved Hide resolved
getItem: function (sKey) {
if (storeType === 'cookie') {
return unescape(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
} else {
return localStorage.getItem(sKey);
}
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (storeType === 'cookie') {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
Expand All @@ -42,11 +74,18 @@ var docCookies = {
}
}
document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
} else {
localStorage.setItem(sKey, sValue);
}
return true;
},
removeItem: function (sKey, sPath) {
if (storeType === 'cookie') {
if (!sKey || !this.hasItem(sKey)) { return false; }
document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sPath ? "; path=" + sPath : "");
} else {
localStorage.removeItem(sKey);
}
return true;
},
hasItem: function (sKey) {
Expand All @@ -59,11 +98,11 @@ var docCookies = {
}
};

return {
getItem: docCookies.getItem,
setItem: docCookies.setItem,
removeItem: docCookies.removeItem,
hasItem: docCookies.hasItem,
keys: docCookies.keys
};
return {
getItem: docCookies.getItem,
setItem: docCookies.setItem,
removeItem: docCookies.removeItem,
hasItem: docCookies.hasItem,
keys: docCookies.keys
};
});