cookie-umd

UMD build of cookie

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greatest.deepsurf.us/scripts/501949/1418023/cookie-umd.js

  1. // ==UserScript==
  2. // @name cookie-umd
  3. // @namespace flomk.userscripts
  4. // @version 1.0
  5. // @description UMD build of cookie
  6. // @author flomk
  7. // ==/UserScript==
  8. (function (global, factory) {
  9. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  10. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  11. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.cookie = {}));
  12. })(this, (function (exports) { 'use strict';
  13. const __toString = Object.prototype.toString
  14. /**
  15. * RegExp to match field-content in RFC 7230 sec 3.2
  16. *
  17. * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  18. * field-vchar = VCHAR / obs-text
  19. * obs-text = %x80-FF
  20. */
  21. const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
  22. /**
  23. * Parse a cookie header.
  24. *
  25. * Parse the given cookie header string into an object
  26. * The object has the various cookies as keys(names) => values
  27. *
  28. * @param {string} str
  29. * @param {object} [options]
  30. * @return {object}
  31. * @public
  32. */
  33. const parse = (str, options) => {
  34. if (typeof str !== 'string') {
  35. throw new TypeError('argument str must be a string');
  36. }
  37. var obj = {}
  38. var opt = options || {};
  39. var dec = opt.decode || decode;
  40. var index = 0
  41. while (index < str.length) {
  42. var eqIdx = str.indexOf('=', index)
  43. // no more cookie pairs
  44. if (eqIdx === -1) {
  45. break
  46. }
  47. var endIdx = str.indexOf(';', index)
  48. if (endIdx === -1) {
  49. endIdx = str.length
  50. } else if (endIdx < eqIdx) {
  51. // backtrack on prior semicolon
  52. index = str.lastIndexOf(';', eqIdx - 1) + 1
  53. continue
  54. }
  55. var key = str.slice(index, eqIdx).trim()
  56. // only assign once
  57. if (undefined === obj[key]) {
  58. var val = str.slice(eqIdx + 1, endIdx).trim()
  59. // quoted values
  60. if (val.charCodeAt(0) === 0x22) {
  61. val = val.slice(1, -1)
  62. }
  63. obj[key] = tryDecode(val, dec);
  64. }
  65. index = endIdx + 1
  66. }
  67. return obj;
  68. };
  69. /**
  70. * Serialize data into a cookie header.
  71. *
  72. * Serialize the a name value pair into a cookie string suitable for
  73. * http headers. An optional options object specified cookie parameters.
  74. *
  75. * serialize('foo', 'bar', { httpOnly: true })
  76. * => "foo=bar; httpOnly"
  77. *
  78. * @param {string} name
  79. * @param {string} val
  80. * @param {object} [options]
  81. * @return {string}
  82. * @public
  83. */
  84. const serialize = (name, val, options) => {
  85. var opt = options || {};
  86. var enc = opt.encode || encode;
  87. if (typeof enc !== 'function') {
  88. throw new TypeError('option encode is invalid');
  89. }
  90. if (!fieldContentRegExp.test(name)) {
  91. throw new TypeError('argument name is invalid');
  92. }
  93. var value = enc(val);
  94. if (value && !fieldContentRegExp.test(value)) {
  95. throw new TypeError('argument val is invalid');
  96. }
  97. var str = name + '=' + value;
  98. if (null != opt.maxAge) {
  99. var maxAge = opt.maxAge - 0;
  100. if (isNaN(maxAge) || !isFinite(maxAge)) {
  101. throw new TypeError('option maxAge is invalid')
  102. }
  103. str += '; Max-Age=' + Math.floor(maxAge);
  104. }
  105. if (opt.domain) {
  106. if (!fieldContentRegExp.test(opt.domain)) {
  107. throw new TypeError('option domain is invalid');
  108. }
  109. str += '; Domain=' + opt.domain;
  110. }
  111. if (opt.path) {
  112. if (!fieldContentRegExp.test(opt.path)) {
  113. throw new TypeError('option path is invalid');
  114. }
  115. str += '; Path=' + opt.path;
  116. }
  117. if (opt.expires) {
  118. var expires = opt.expires
  119. if (!isDate(expires) || isNaN(expires.valueOf())) {
  120. throw new TypeError('option expires is invalid');
  121. }
  122. str += '; Expires=' + expires.toUTCString()
  123. }
  124. if (opt.httpOnly) {
  125. str += '; HttpOnly';
  126. }
  127. if (opt.secure) {
  128. str += '; Secure';
  129. }
  130. if (opt.partitioned) {
  131. str += '; Partitioned'
  132. }
  133. if (opt.priority) {
  134. var priority = typeof opt.priority === 'string'
  135. ? opt.priority.toLowerCase()
  136. : opt.priority
  137. switch (priority) {
  138. case 'low':
  139. str += '; Priority=Low'
  140. break
  141. case 'medium':
  142. str += '; Priority=Medium'
  143. break
  144. case 'high':
  145. str += '; Priority=High'
  146. break
  147. default:
  148. throw new TypeError('option priority is invalid')
  149. }
  150. }
  151. if (opt.sameSite) {
  152. var sameSite = typeof opt.sameSite === 'string'
  153. ? opt.sameSite.toLowerCase() : opt.sameSite;
  154. switch (sameSite) {
  155. case true:
  156. str += '; SameSite=Strict';
  157. break;
  158. case 'lax':
  159. str += '; SameSite=Lax';
  160. break;
  161. case 'strict':
  162. str += '; SameSite=Strict';
  163. break;
  164. case 'none':
  165. str += '; SameSite=None';
  166. break;
  167. default:
  168. throw new TypeError('option sameSite is invalid');
  169. }
  170. }
  171. return str;
  172. };
  173. /**
  174. * URL-decode string value. Optimized to skip native call when no %.
  175. *
  176. * @param {string} str
  177. * @returns {string}
  178. */
  179. const decode = str => str.indexOf('%') !== -1 ? decodeURIComponent(str) : str;
  180. /**
  181. * URL-encode value.
  182. *
  183. * @param {string} val
  184. * @returns {string}
  185. */
  186. const encode = val => encodeURIComponent(val);
  187. /**
  188. * Determine if value is a Date.
  189. *
  190. * @param {*} val
  191. * @private
  192. */
  193. const isDate = val => __toString.call(val) === '[object Date]' || val instanceof Date;
  194. /**
  195. * Try decoding a string using a decoding function.
  196. *
  197. * @param {string} str
  198. * @param {function} decode
  199. * @private
  200. */
  201. const tryDecode = (str, decode) => {
  202. try {
  203. return decode(str);
  204. } catch (e) {
  205. return str;
  206. }
  207. };
  208. exports.parse = parse;
  209. exports.serialize = serialize;
  210. }));