diff options
Diffstat (limited to 'cmd/web/js/cookies.js')
-rw-r--r-- | cmd/web/js/cookies.js | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/cmd/web/js/cookies.js b/cmd/web/js/cookies.js new file mode 100644 index 0000000..9cc7ca9 --- /dev/null +++ b/cmd/web/js/cookies.js | |||
@@ -0,0 +1,65 @@ | |||
1 | 'use strict'; | ||
2 | |||
3 | /*\ | ||
4 | |*| | ||
5 | |*| :: cookies.js :: | ||
6 | |*| | ||
7 | |*| A complete cookies reader/writer framework with full unicode support. | ||
8 | |*| | ||
9 | |*| Revision #1 - September 4, 2014 | ||
10 | |*| | ||
11 | |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie | ||
12 | |*| https://developer.mozilla.org/User:fusionchess | ||
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 | |||
27 | module.exports = { | ||
28 | getItem: function(sKey) { | ||
29 | if (!sKey) { return null; } | ||
30 | return decodeURIComponent(document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')) || null; | ||
31 | }, | ||
32 | setItem: function(sKey, sValue, vEnd, sPath, sDomain, bSecure) { | ||
33 | if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; } | ||
34 | var sExpires = ''; | ||
35 | if (vEnd) { | ||
36 | switch (vEnd.constructor) { | ||
37 | case Number: | ||
38 | sExpires = vEnd === Infinity ? '; expires=Fri, 31 Dec 9999 23:59:59 GMT' : '; max-age=' + vEnd; | ||
39 | break; | ||
40 | case String: | ||
41 | sExpires = '; expires=' + vEnd; | ||
42 | break; | ||
43 | case Date: | ||
44 | sExpires = '; expires=' + vEnd.toUTCString(); | ||
45 | break; | ||
46 | } | ||
47 | } | ||
48 | document.cookie = encodeURIComponent(sKey) + '=' + encodeURIComponent(sValue) + sExpires + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : '') + (bSecure ? '; secure' : ''); | ||
49 | return true; | ||
50 | }, | ||
51 | removeItem: function(sKey, sPath, sDomain) { | ||
52 | if (!this.hasItem(sKey)) { return false; } | ||
53 | document.cookie = encodeURIComponent(sKey) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT' + (sDomain ? '; domain=' + sDomain : '') + (sPath ? '; path=' + sPath : ''); | ||
54 | return true; | ||
55 | }, | ||
56 | hasItem: function(sKey) { | ||
57 | if (!sKey) { return false; } | ||
58 | return (new RegExp('(?:^|;\\s*)' + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=')).test(document.cookie); | ||
59 | }, | ||
60 | keys: function() { | ||
61 | var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, '').split(/\s*(?:\=[^;]*)?;\s*/); | ||
62 | for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } | ||
63 | return aKeys; | ||
64 | } | ||
65 | }; | ||