cookies.js 3.2 KB

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