view cs/cookies.js @ 113:4d3f845c4ef2

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