paulo@78: /*\ paulo@78: |*| paulo@113: |*| :: cookies.js :: paulo@78: |*| paulo@113: |*| A complete cookies reader/writer framework with full unicode support. paulo@78: |*| paulo@113: |*| Revision #8 - February 18th, 2020 paulo@78: |*| paulo@113: |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie paulo@113: |*| https://developer.mozilla.org/User:fusionchess paulo@113: |*| https://github.com/madmurphy/cookies.js paulo@78: |*| paulo@113: |*| This framework is released under the GNU Public License, version 3 or later. paulo@113: |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html paulo@78: |*| paulo@113: |*| Syntaxes: paulo@78: |*| paulo@113: |*| * docCookies.setItem(name, value[, end[, path[, domain[, secure[, same-site]]]]]) paulo@113: |*| * docCookies.getItem(name) paulo@113: |*| * docCookies.removeItem(name[, path[, domain[, secure[, same-site]]]]) paulo@113: |*| * docCookies.hasItem(name) paulo@113: |*| * docCookies.keys() paulo@113: |*| * docCookies.clear([path[, domain[, secure[, same-site]]]]) paulo@78: |*| paulo@78: \*/ paulo@78: paulo@113: (function () { paulo@113: function makeSetterString (sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite) { paulo@113: var sExpires = ""; paulo@113: if (vEnd) { paulo@113: switch (vEnd.constructor) { paulo@113: case Number: paulo@113: sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd; paulo@113: /* paulo@113: Note: Despite officially defined in RFC 6265, the use of `max-age` is not compatible with any paulo@113: version of Internet Explorer, Edge and some mobile browsers. Therefore passing a number to paulo@113: the end parameter might not work as expected. A possible solution might be to convert the the paulo@113: relative time to an absolute time. For instance you could replace the previous line with: paulo@113: */ paulo@113: /* paulo@113: sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; expires=" + (new Date(vEnd * 1e3 + Date.now())).toUTCString(); paulo@113: */ paulo@113: break; paulo@113: case String: paulo@113: sExpires = "; expires=" + vEnd; paulo@113: break; paulo@113: case Date: paulo@113: sExpires = "; expires=" + vEnd.toUTCString(); paulo@113: break; paulo@113: } paulo@113: } paulo@113: 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: } paulo@113: var reURIAllowed = /[\-\.\+\*]/g, reCNameAllowed = /^(?:expires|max\-age|path|domain|secure|samesite|httponly)$/i; paulo@113: window.docCookies = { paulo@113: "getItem": function (sKey) { paulo@113: if (!sKey) { return null; } paulo@113: return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(reURIAllowed, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; paulo@113: }, paulo@113: "setItem": function (sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite) { paulo@113: if (!sKey || reCNameAllowed.test(sKey)) { return false; } paulo@113: document.cookie = makeSetterString(sKey, sValue, vEnd, sPath, sDomain, bSecure, vSameSite); paulo@113: return true; paulo@113: }, paulo@113: "removeItem": function (sKey, sPath, sDomain, bSecure, vSameSite) { paulo@113: if (!this.hasItem(sKey)) { return false; } paulo@113: document.cookie = makeSetterString(sKey, "", "Thu, 01 Jan 1970 00:00:00 GMT", sPath, sDomain, bSecure, vSameSite); paulo@113: return true; paulo@113: }, paulo@113: "hasItem": function (sKey) { paulo@113: if (!sKey || reCNameAllowed.test(sKey)) { return false; } paulo@113: return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(reURIAllowed, "\\$&") + "\\s*\\=")).test(document.cookie); paulo@113: }, paulo@113: "keys": function () { paulo@113: var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); paulo@113: for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { paulo@113: aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); paulo@113: } paulo@113: return aKeys; paulo@113: }, paulo@113: "clear": function (sPath, sDomain, bSecure, vSameSite) { paulo@113: for (var aKeys = this.keys(), nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { paulo@113: this.removeItem(aKeys[nIdx], sPath, sDomain, bSecure, vSameSite); paulo@113: } paulo@113: } paulo@113: }; paulo@113: })(); paulo@113: paulo@113: if (typeof module !== "undefined" && typeof module.exports !== "undefined") { paulo@113: module.exports = docCookies; paulo@113: }