comparison cs/cookies.js @ 140:ae6b4032a912

myrss2: FEEDS: Add jabberwocking.com, remove danielmiessler.com
author paulo
date Mon, 03 Jul 2023 04:40:23 +0000
parents f833a888c548
children
comparison
equal deleted inserted replaced
0:2e5a5028dafa 1:b5f9bb401929
1 /*\ 1 /*\
2 |*| 2 |*|
3 |*| :: cookies.js :: 3 |*| :: cookies.js ::
4 |*| 4 |*|
5 |*| A complete cookies reader/writer framework with full unicode support. 5 |*| A complete cookies reader/writer framework with full unicode support.
6 |*| 6 |*|
7 |*| Revision #1 - September 4, 2014 7 |*| Revision #8 - February 18th, 2020
8 |*| 8 |*|
9 |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie 9 |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
10 |*| https://developer.mozilla.org/User:fusionchess 10 |*| https://developer.mozilla.org/User:fusionchess
11 |*| https://github.com/madmurphy/cookies.js
11 |*| 12 |*|
12 |*| This framework is released under the GNU Public License, version 3 or later. 13 |*| This framework is released under the GNU Public License, version 3 or later.
13 |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html 14 |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
14 |*| 15 |*|
15 |*| Syntaxes: 16 |*| Syntaxes:
16 |*| 17 |*|
17 |*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]]) 18 |*| * docCookies.setItem(name, value[, end[, path[, domain[, secure[, same-site]]]]])
18 |*| * docCookies.getItem(name) 19 |*| * docCookies.getItem(name)
19 |*| * docCookies.removeItem(name[, path[, domain]]) 20 |*| * docCookies.removeItem(name[, path[, domain[, secure[, same-site]]]])
20 |*| * docCookies.hasItem(name) 21 |*| * docCookies.hasItem(name)
21 |*| * docCookies.keys() 22 |*| * docCookies.keys()
23 |*| * docCookies.clear([path[, domain[, secure[, same-site]]]])
22 |*| 24 |*|
23 \*/ 25 \*/
24 26
25 var docCookies = { 27 (function () {
26 getItem: function (sKey) { 28 function makeSetterString (sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite) {
27 if (!sKey) { return null; } 29 var sExpires = "";
28 return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; 30 if (vEnd) {
29 }, 31 switch (vEnd.constructor) {
30 setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) { 32 case Number:
31 if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; } 33 sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
32 var sExpires = ""; 34 /*
33 if (vEnd) { 35 Note: Despite officially defined in RFC 6265, the use of `max-age` is not compatible with any
34 switch (vEnd.constructor) { 36 version of Internet Explorer, Edge and some mobile browsers. Therefore passing a number to
35 case Number: 37 the end parameter might not work as expected. A possible solution might be to convert the the
36 sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd; 38 relative time to an absolute time. For instance you could replace the previous line with:
37 break; 39 */
38 case String: 40 /*
39 sExpires = "; expires=" + vEnd; 41 sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; expires=" + (new Date(vEnd * 1e3 + Date.now())).toUTCString();
40 break; 42 */
41 case Date: 43 break;
42 sExpires = "; expires=" + vEnd.toUTCString(); 44 case String:
43 break; 45 sExpires = "; expires=" + vEnd;
44 } 46 break;
45 } 47 case Date:
46 document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : ""); 48 sExpires = "; expires=" + vEnd.toUTCString();
47 return true; 49 break;
48 }, 50 }
49 removeItem: function (sKey, sPath, sDomain) { 51 }
50 if (!this.hasItem(sKey)) { return false; } 52 return encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "") + (!vSameSite || vSameSite.toString().toLowerCase() === "no_restriction" ? "" : vSameSite.toString().toLowerCase() === "lax" || Math.ceil(vSameSite) === 1 || vSameSite === true ? "; samesite=lax" : vSameSite.toString().toLowerCase() === "none" || vSameSite < 0 ? "; samesite=none" : "; samesite=strict");
51 document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : ""); 53 }
52 return true; 54 var reURIAllowed = /[\-\.\+\*]/g, reCNameAllowed = /^(?:expires|max\-age|path|domain|secure|samesite|httponly)$/i;
53 }, 55 window.docCookies = {
54 hasItem: function (sKey) { 56 "getItem": function (sKey) {
55 if (!sKey) { return false; } 57 if (!sKey) { return null; }
56 return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); 58 return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(reURIAllowed, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
57 }, 59 },
58 keys: function () { 60 "setItem": function (sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite) {
59 var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); 61 if (!sKey || reCNameAllowed.test(sKey)) { return false; }
60 for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } 62 document.cookie = makeSetterString(sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite);
61 return aKeys; 63 return true;
62 } 64 },
63 }; 65 "removeItem": function (sKey, sPath, sDomain, bSecure, vSameSite) {
66 if (!this.hasItem(sKey)) { return false; }
67 document.cookie = makeSetterString(sKey, "", "Thu, 01 Jan 1970 00:00:00 GMT", sPath, sDomain, bSecure, vSameSite);
68 return true;
69 },
70 "hasItem": function (sKey) {
71 if (!sKey || reCNameAllowed.test(sKey)) { return false; }
72 return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(reURIAllowed, "\\$&") + "\\s*\\=")).test(document.cookie);
73 },
74 "keys": function () {
75 var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
76 for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) {
77 aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
78 }
79 return aKeys;
80 },
81 "clear": function (sPath, sDomain, bSecure, vSameSite) {
82 for (var aKeys = this.keys(), nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) {
83 this.removeItem(aKeys[nIdx], sPath, sDomain, bSecure, vSameSite);
84 }
85 }
86 };
87 })();
88
89 if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
90 module.exports = docCookies;
91 }