JavaScript Cookie v2.1.0 Released under the MIT license

A simple, lightweight JavaScript API for handling browser cookies

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/17418/109802/JavaScript%20Cookie%20v210%20Released%20under%20the%20MIT%20license.js

  1. /*!
  2. * JavaScript Cookie v2.1.0
  3. * https://github.com/js-cookie/js-cookie
  4. *
  5. * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
  6. * Released under the MIT license
  7. */
  8. (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. define(factory);
  11. } else if (typeof exports === 'object') {
  12. module.exports = factory();
  13. } else {
  14. var _OldCookies = window.Cookies;
  15. var api = window.Cookies = factory();
  16. api.noConflict = function () {
  17. window.Cookies = _OldCookies;
  18. return api;
  19. };
  20. }
  21. }(function () {
  22. function extend () {
  23. var i = 0;
  24. var result = {};
  25. for (; i < arguments.length; i++) {
  26. var attributes = arguments[ i ];
  27. for (var key in attributes) {
  28. result[key] = attributes[key];
  29. }
  30. }
  31. return result;
  32. }
  33.  
  34. function init (converter) {
  35. function api (key, value, attributes) {
  36. var result;
  37.  
  38. // Write
  39.  
  40. if (arguments.length > 1) {
  41. attributes = extend({
  42. path: '/'
  43. }, api.defaults, attributes);
  44.  
  45. if (typeof attributes.expires === 'number') {
  46. var expires = new Date();
  47. expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
  48. attributes.expires = expires;
  49. }
  50.  
  51. try {
  52. result = JSON.stringify(value);
  53. if (/^[\{\[]/.test(result)) {
  54. value = result;
  55. }
  56. } catch (e) {}
  57.  
  58. if (!converter.write) {
  59. value = encodeURIComponent(String(value))
  60. .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
  61. } else {
  62. value = converter.write(value, key);
  63. }
  64.  
  65. key = encodeURIComponent(String(key));
  66. key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
  67. key = key.replace(/[\(\)]/g, escape);
  68.  
  69. return (document.cookie = [
  70. key, '=', value,
  71. attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
  72. attributes.path && '; path=' + attributes.path,
  73. attributes.domain && '; domain=' + attributes.domain,
  74. attributes.secure ? '; secure' : ''
  75. ].join(''));
  76. }
  77.  
  78. // Read
  79.  
  80. if (!key) {
  81. result = {};
  82. }
  83.  
  84. // To prevent the for loop in the first place assign an empty array
  85. // in case there are no cookies at all. Also prevents odd result when
  86. // calling "get()"
  87. var cookies = document.cookie ? document.cookie.split('; ') : [];
  88. var rdecode = /(%[0-9A-Z]{2})+/g;
  89. var i = 0;
  90.  
  91. for (; i < cookies.length; i++) {
  92. var parts = cookies[i].split('=');
  93. var name = parts[0].replace(rdecode, decodeURIComponent);
  94. var cookie = parts.slice(1).join('=');
  95.  
  96. if (cookie.charAt(0) === '"') {
  97. cookie = cookie.slice(1, -1);
  98. }
  99.  
  100. try {
  101. cookie = converter.read ?
  102. converter.read(cookie, name) : converter(cookie, name) ||
  103. cookie.replace(rdecode, decodeURIComponent);
  104.  
  105. if (this.json) {
  106. try {
  107. cookie = JSON.parse(cookie);
  108. } catch (e) {}
  109. }
  110.  
  111. if (key === name) {
  112. result = cookie;
  113. break;
  114. }
  115.  
  116. if (!key) {
  117. result[name] = cookie;
  118. }
  119. } catch (e) {}
  120. }
  121.  
  122. return result;
  123. }
  124.  
  125. api.get = api.set = api;
  126. api.getJSON = function () {
  127. return api.apply({
  128. json: true
  129. }, [].slice.call(arguments));
  130. };
  131. api.defaults = {};
  132.  
  133. api.remove = function (key, attributes) {
  134. api(key, '', extend(attributes, {
  135. expires: -1
  136. }));
  137. };
  138.  
  139. api.withConverter = init;
  140.  
  141. return api;
  142. }
  143.  
  144. return init(function () {});
  145. }));