-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookie.js
116 lines (91 loc) · 2.94 KB
/
cookie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
FILE ARCHIVED ON 9:57:37 May 30, 2016 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 15:51:22 Aug 21, 2016.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
*/
/***
* cookie.js
* A simple cookie library supporting hash trees
* Requires Joe Tools for merge_objects() and serialize().
*
* var tree = new CookieTree();
* tree.set( "foo", "bar" );
* tree.set( "complex", { hello: "there", array: [1,2,3] } );
* tree.save();
*
* Copyright (c) 2007 Joseph Huckaby.
* Released under the LGPL v3.0: /web/20160530095737/http://www.opensource.org/licenses/lgpl-3.0.html
*/
/* if (!window.merge_objects || !window.serialize)
alert("ERROR: cookie.js requires tools.js."); */
function CookieTree(args) {
// class constructor
if (args) {
for (var key in args) this[key] = args[key];
}
if (!this.expires) {
var now = new Date();
now.setFullYear( now.getFullYear() + 10 ); // 10 years from now
this.expires = now.toGMTString();
}
this.parse();
};
CookieTree.prototype.domain = location.hostname;
CookieTree.prototype.path = location.pathname;
CookieTree.prototype.parse = function() {
// parse document.cookie into hash tree
this.tree = {};
var cookies = document.cookie.split(/\;\s*/);
for (var idx = 0, len = cookies.length; idx < len; idx++) {
var cookie_raw = cookies[idx];
if (cookie_raw.match(/^CookieTree=(.+)$/)) {
var cookie = null;
var cookie_raw = unescape( RegExp.$1 );
// Debug.trace("Cookie", "Parsing cookie: " + cookie_raw);
try {
eval( "cookie = " + cookie_raw + ";" );
}
catch (e) {
// Debug.trace("Cookie", "Failed to parse cookie.");
cookie = {};
}
this.tree = merge_objects( this.tree, cookie );
idx = len;
}
}
};
CookieTree.prototype.get = function(key) {
// get tree branch given value (top level)
return this.tree[key];
};
CookieTree.prototype.set = function(key, value) {
// set tree branch to given value (top level)
this.tree[key] = value;
};
CookieTree.prototype.save = function() {
// serialize tree and save back into document.cookie
var cookie_raw = 'CookieTree=' + escape(serialize(this.tree));
if (!this.path.match(/\/$/)) {
this.path = this.path.replace(/\/[^\/]+$/, "") + '/';
}
cookie_raw += '; expires=' + this.expires;
cookie_raw += '; domain=' + this.domain;
cookie_raw += '; path=' + this.path;
// Debug.trace("Cookie", "Saving cookie: " + cookie_raw);
document.cookie = cookie_raw;
};
CookieTree.prototype.remove = function() {
// remove cookie from document
var cookie_raw = 'CookieTree={}';
if (!this.path.match(/\/$/)) {
this.path = this.path.replace(/\/[^\/]+$/, "") + '/';
}
var now = new Date();
now.setFullYear( now.getFullYear() - 1 ); // last year
cookie_raw += '; expires=' + now.toGMTString();
cookie_raw += '; domain=' + this.domain;
cookie_raw += '; path=' + this.path;
document.cookie = cookie_raw;
};