mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-08 19:57:46 -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
@ -1,102 +1,117 @@
|
||||
'use strict';
|
||||
define([], function() {
|
||||
/*\
|
||||
|*|
|
||||
|*| :: cookies.js ::
|
||||
|*|
|
||||
|*| A complete cookies reader/writer framework with full unicode support.
|
||||
|*|
|
||||
|*| https://developer.mozilla.org/en-US/docs/DOM/document.cookie
|
||||
|*|
|
||||
|*| This framework is released under the GNU Public License, version 3 or later.
|
||||
|*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
|
||||
|*|
|
||||
|*| Syntaxes:
|
||||
|*|
|
||||
|*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|
||||
|*| * docCookies.getItem(name)
|
||||
|*| * docCookies.removeItem(name[, path])
|
||||
|*| * docCookies.hasItem(name)
|
||||
|*| * docCookies.keys()
|
||||
|*|
|
||||
\*/
|
||||
|
||||
// Test for cookie support
|
||||
var storeType = 'cookie';
|
||||
document.cookie = 'kiwixCookie=working;expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
||||
var kiwixCookie = /kiwixCookie=working/i.test(document.cookie);
|
||||
if (kiwixCookie) {
|
||||
document.cookie = 'kiwixCookie=broken;expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
||||
kiwixCookie = !/kiwixCookie=working/i.test(document.cookie);
|
||||
}
|
||||
document.cookie = 'kiwixCookie=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
||||
if (!kiwixCookie) {
|
||||
// Cookies appear to be blocked, so test for localStorage support
|
||||
var result = false;
|
||||
try {
|
||||
result = 'localStorage' in window && window['localStorage'] !== null;
|
||||
} catch (e) {
|
||||
console.log('LocalStorage is not supported!');
|
||||
}
|
||||
if (result) storeType = 'local_storage';
|
||||
}
|
||||
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;
|
||||
} 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) {
|
||||
switch (vEnd.constructor) {
|
||||
case Number:
|
||||
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
|
||||
break;
|
||||
case String:
|
||||
sExpires = "; expires=" + vEnd;
|
||||
break;
|
||||
case Date:
|
||||
sExpires = "; expires=" + vEnd.toGMTString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
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) {
|
||||
return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
|
||||
},
|
||||
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]); }
|
||||
return aKeys;
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
getItem: docCookies.getItem,
|
||||
setItem: docCookies.setItem,
|
||||
removeItem: docCookies.removeItem,
|
||||
hasItem: docCookies.hasItem,
|
||||
keys: docCookies.keys
|
||||
};
|
||||
'use strict';
|
||||
define([], function() {
|
||||
/*\
|
||||
|*|
|
||||
|*| :: cookies.js ::
|
||||
|*|
|
||||
|*| A complete cookies reader/writer framework with full unicode support.
|
||||
|*|
|
||||
|*| https://developer.mozilla.org/en-US/docs/DOM/document.cookie
|
||||
|*|
|
||||
|*| This framework is released under the GNU Public License, version 3 or later.
|
||||
|*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
|
||||
|*|
|
||||
|*| Syntaxes:
|
||||
|*|
|
||||
|*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|
||||
|*| * docCookies.getItem(name)
|
||||
|*| * docCookies.removeItem(name[, path])
|
||||
|*| * docCookies.hasItem(name)
|
||||
|*| * docCookies.keys()
|
||||
|*|
|
||||
\*/
|
||||
|
||||
// Test for cookie support
|
||||
var storeType = 'cookie';
|
||||
document.cookie = 'kiwixCookie=working;expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
||||
var kiwixCookie = /kiwixCookie=working/i.test(document.cookie);
|
||||
if (kiwixCookie) {
|
||||
document.cookie = 'kiwixCookie=broken;expires=Fri, 31 Dec 9999 23:59:59 GMT';
|
||||
kiwixCookie = !/kiwixCookie=working/i.test(document.cookie);
|
||||
}
|
||||
document.cookie = 'kiwixCookie=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
||||
if (!kiwixCookie) {
|
||||
// Cookies appear to be blocked, so test for localStorage support
|
||||
var result = false;
|
||||
try {
|
||||
result = 'localStorage' in window && window['localStorage'] !== null;
|
||||
} catch (e) {
|
||||
console.log('LocalStorage is not supported!');
|
||||
}
|
||||
if (result) storeType = 'local_storage';
|
||||
}
|
||||
console.log('Test2: storeType: ' + storeType);
|
||||
|
||||
var docCookies = {
|
||||
getItem: function (sKey) {
|
||||
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(keyPrefix + sKey);
|
||||
}
|
||||
},
|
||||
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
|
||||
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) {
|
||||
case Number:
|
||||
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
|
||||
break;
|
||||
case String:
|
||||
sExpires = "; expires=" + vEnd;
|
||||
break;
|
||||
case Date:
|
||||
sExpires = "; expires=" + vEnd.toUTCString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
|
||||
} else {
|
||||
localStorage.setItem(keyPrefix + sKey, sValue);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
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(keyPrefix + sKey);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
hasItem: function (sKey) {
|
||||
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;
|
||||
}
|
||||
},
|
||||
_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;
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
getItem: docCookies.getItem,
|
||||
setItem: docCookies.setItem,
|
||||
removeItem: docCookies.removeItem,
|
||||
hasItem: docCookies.hasItem
|
||||
};
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user