mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-11 13:18:21 -04:00
Update cookie code
Former-commit-id: 1ab45f8dbc59de80cbb911e1927e43fcdbe9baf8 [formerly 0f99071ce36f48c10557558506a84af11df31271 [formerly e6d94553bdbed7c780f5c65fc37235c5995a2493]] Former-commit-id: f04a85dffff2c8ab6ec8b8b5afcd08261f43a550 Former-commit-id: f189cf536ebeb8250e6d96dc7069315e925aa1ec
This commit is contained in:
parent
f272768698
commit
1dd1569b10
@ -44,15 +44,20 @@ console.log('Test2: storeType: ' + storeType);
|
||||
|
||||
var docCookies = {
|
||||
getItem: function (sKey) {
|
||||
if (storeType === 'cookie') {
|
||||
return unescape(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
|
||||
if (!sKey) {
|
||||
return null;
|
||||
}
|
||||
if (params.storeType !== 'local_storage') {
|
||||
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
|
||||
} else {
|
||||
return localStorage.getItem(sKey);
|
||||
return localStorage.getItem(keyPrefix + 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; }
|
||||
if (params.storeType !== 'local_storage') {
|
||||
if (!sKey || /^(?:expires|max-age|path|domain|secure)$/i.test(sKey)) {
|
||||
return false;
|
||||
}
|
||||
var sExpires = "";
|
||||
if (vEnd) {
|
||||
switch (vEnd.constructor) {
|
||||
@ -63,31 +68,42 @@ var docCookies = {
|
||||
sExpires = "; expires=" + vEnd;
|
||||
break;
|
||||
case Date:
|
||||
sExpires = "; expires=" + vEnd.toGMTString();
|
||||
sExpires = "; expires=" + vEnd.toUTCString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
document.cookie = escape(sKey) + "=" + escape(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
|
||||
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
|
||||
} else {
|
||||
localStorage.setItem(sKey, sValue);
|
||||
localStorage.setItem(keyPrefix + 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 : "");
|
||||
removeItem: function (sKey, sPath, sDomain) {
|
||||
if (!this.hasItem(sKey)) {
|
||||
return false;
|
||||
}
|
||||
if (params.storeType !== 'local_storage') {
|
||||
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
|
||||
} else {
|
||||
localStorage.removeItem(sKey);
|
||||
localStorage.removeItem(keyPrefix + sKey);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
hasItem: function (sKey) {
|
||||
return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
|
||||
if (!sKey) {
|
||||
return false;
|
||||
}
|
||||
if (params.storeType !== 'local_storage') {
|
||||
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
|
||||
} else {
|
||||
return localStorage.getItem(keyPrefix + sKey) === null ? false : true;
|
||||
}
|
||||
},
|
||||
keys: /* optional method: you can safely remove it! */ function () {
|
||||
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
|
||||
for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = unescape(aKeys[nIdx]); }
|
||||
_cookieKeys: function () {
|
||||
var aKeys = document.cookie.replace(/((?:^|\s*;)[^=]+)(?=;|$)|^\s*|\s*(?:=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:=[^;]*)?;\s*/);
|
||||
for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) {
|
||||
aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
|
||||
}
|
||||
return aKeys;
|
||||
}
|
||||
};
|
||||
@ -96,7 +112,6 @@ return {
|
||||
getItem: docCookies.getItem,
|
||||
setItem: docCookies.setItem,
|
||||
removeItem: docCookies.removeItem,
|
||||
hasItem: docCookies.hasItem,
|
||||
keys: docCookies.keys
|
||||
hasItem: docCookies.hasItem
|
||||
};
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user