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