]>
Commit | Line | Data |
---|---|---|
989fb5c7 | 1 | /* eslint-disable */ |
2 | ||
7a9e5112 | 3 | 'use strict'; |
4 | ||
5 | /*\ | |
6 | |*| | |
7 | |*| :: cookies.js :: | |
8 | |*| | |
9 | |*| A complete cookies reader/writer framework with full unicode support. | |
10 | |*| | |
11 | |*| Revision #1 - September 4, 2014 | |
12 | |*| | |
13 | |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie | |
14 | |*| https://developer.mozilla.org/User:fusionchess | |
15 | |*| | |
16 | |*| This framework is released under the GNU Public License, version 3 or later. | |
17 | |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html | |
18 | |*| | |
19 | |*| Syntaxes: | |
20 | |*| | |
21 | |*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]]) | |
22 | |*| * docCookies.getItem(name) | |
23 | |*| * docCookies.removeItem(name[, path[, domain]]) | |
24 | |*| * docCookies.hasItem(name) | |
25 | |*| * docCookies.keys() | |
26 | |*| | |
27 | \*/ | |
28 | ||
29 | module.exports = { | |
30 | getItem: function(sKey) { | |
31 | if (!sKey) { return null; } | |
32 | return decodeURIComponent(document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')) || null; | |
33 | }, | |
34 | setItem: function(sKey, sValue, vEnd, sPath, sDomain, bSecure) { | |
35 | if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; } | |
36 | var sExpires = ''; | |
37 | if (vEnd) { | |
38 | switch (vEnd.constructor) { | |
39 | case Number: | |
40 | sExpires = vEnd === Infinity ? '; expires=Fri, 31 Dec 9999 23:59:59 GMT' : '; max-age=' + vEnd; | |
41 | break; | |
42 | case String: | |
43 | sExpires = '; expires=' + vEnd; | |
44 | break; | |
45 | case Date: | |
46 | sExpires = '; expires=' + vEnd.toUTCString(); | |
47 | break; | |
48 | } | |
49 | } | |
50 | document.cookie = encodeURIComponent(sKey) + '=' + encodeURIComponent(sValue) + sExpires + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '') + (bSecure ? '; secure' : ''); | |
51 | return true; | |
52 | }, | |
53 | removeItem: function(sKey, sPath, sDomain) { | |
54 | if (!this.hasItem(sKey)) { return false; } | |
55 | document.cookie = encodeURIComponent(sKey) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT' + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : ''); | |
56 | return true; | |
57 | }, | |
58 | hasItem: function(sKey) { | |
59 | if (!sKey) { return false; } | |
60 | return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=')).test(document.cookie); | |
61 | }, | |
62 | keys: function() { | |
63 | var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, '').split(/\s*(?:\=[^;]*)?;\s*/); | |
64 | for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } | |
65 | return aKeys; | |
66 | } | |
67 | }; |