-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaramelcookie.js
226 lines (185 loc) · 5.48 KB
/
caramelcookie.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Filename: juicycookie.js
// Timestamp: 2015.12.20-00:51:49 (last modified)
// Author(s): Bumblehead (www.bumblehead.com)
const cookie = {
name : '',
value : '',
expires : '',
path : '/',
secure : false,
domain : '.defaultdomain.com',
setDocCookieStr : function (cookieStr) {
document.cookie = cookieStr;
},
getDocCookieStr : function () {
return document.cookie;
},
// ip address cannot be wildcarded
//
// defines top level domain as default, allowing cookie-access at subdomains.
//
// ex: `.foxsports.com`.
getUrlAsDomainStr : function (url) {
var dmn = '', toplevel;
if (url) {
dmn = url;
if (url !== 'localhost') {
if ((toplevel = url.match(/\d*\.\d*\.\d*\.\d*$/))) {
[ dmn ] = toplevel;
} else if ((toplevel = url.match(/\w*\.\w*$/))) {
dmn = '.' + toplevel[0];
}
}
}
return dmn;
},
getAsCookieStr : function () {
var that = this,
finStr = that.name + "=" + escape(that.value);
finStr += that.expires
? ";expires=" + (new Date(that.expires)).toGMTString() : '';
finStr += that.domain ? ";domain=" + that.domain : '';
finStr += that.path ? ";path=" + that.path : '';
finStr += that.secure ? ";secure" : '';
return finStr;
},
setDomain : function (dmn) {
var that = this;
if (typeof dmn === 'string') {
that.domain = dmn;
} else if (typeof location === 'object') {
that.domain = that.getUrlAsDomainStr(location.hostname);
}
},
setName : function (name) {
if (typeof name === 'string') {
this.name = name;
}
},
getAsCrumbStr : function (v) {
var s = '', type = typeof v;
if (type === 'object') {
s = JSON.stringify(v);
} else if (type === 'string') {
s = v.replace(/,*$/, '');
} else if (type === 'number') {
s = v;
}
return s;
},
// a value (or 'crumb') may not have a trailing comma
setValue : function (v) {
return this.value = this.getAsCrumbStr(v);
},
// accepts one of two possible parameters.
// 1) a date object
// 2) an object w/ properties `y`, `m`, `d`, `hh`, `mm`, `ss`
// 3) a number. assumption made: number is a timestamp
setExpires : function (opts) {
var dateObj = '', ms = 0;
if (opts instanceof Date) {
dateObj = opts;
} else if (typeof opts === 'number') {
dateObj = opts;
} else if (typeof opts === 'object' && opts) {
// 1000 * 60 * 60 * 24 * 256
if (typeof opts.y === 'number') ms += opts.y * 22118400000;
// 1000 * 60 * 60 * 24 * 30
if (typeof opts.m === 'number') ms += opts.m * 2592000000;
// 1000 * 60 * 60 * 24
if (typeof opts.d === 'number') ms += opts.d * 86400000;
// 1000 * 60 * 60
if (typeof opts.hh === 'number') ms += opts.hh * 3600000;
// 1000 * 60
if (typeof opts.mm === 'number') ms += opts.mm * 60000;
// 1000
if (typeof opts.ss === 'number') ms += opts.ss * 1000;
if (typeof opts.ms === 'number') ms += opts.ms;
dateObj = new Date(Date.now() + ms);
}
return this.expires = dateObj;
},
set : function () {
var that = this,
cookieStr = that.getAsCookieStr();
return that.setDocCookieStr(cookieStr);
},
rm : function () {
var that = this;
that.expires = new Date(0);
return that.set();
}
};
export default {
// allow prototype methods to be redefined!
prototype : cookie,
getNew : function (name, value, params) {
var that = Object.create(cookie);
params = params || {};
that.setName(name);
that.setValue(value);
that.setExpires(params.expires);
that.setDomain(params.domain);
that.path = params.path || '/';
that.secure = params.secure || false;
return that;
},
// convenience method -constructs AND sets cookie
set : function (name, value, params) {
var cookieObj = this.getNew(name, value, params);
return cookieObj.set();
},
// return all cookie values as object litersl
// { name : value, name2 : value2, ... }
getall : function () {
var that = Object.create(cookie),
cookieArr = that.getDocCookieStr().split('; '),
cookieObj = {};
cookieArr.map(cookie => {
var crumb = cookie.split('=');
cookieObj[crumb[0]] = unescape(crumb[1]);
});
return cookieObj;
},
// get the full cookie, represented as object-literal
getExisting : function (cookieName) {
var that = Object.create(cookie), x, crumb,
cookieArr = document.cookie.split('; ');
that.name = cookieName || that.name;
for (x = cookieArr.length; x--;) {
crumb = cookieArr[x].split('=');
if (crumb[0] === that.name) {
that.value = unescape(crumb[1]);
return that;
}
}
return null;
},
// remove accross all domains
// `.blah.chris.foxsports.com`
// `.chris.foxsports.com`
// `.foxsports.com`
rm : function (name) {
var that = this, hostname, sph, x;
that.getNew(name, '', {
domain : ''
}).rm();
if (typeof location === 'object') {
// eslint-disable-next-line
hostname = location.hostname;
sph = hostname.split('.');
for (x = sph.length; x-- > 1; sph = sph.slice(1)) {
that.getNew(name, '', {
domain : '.' + sph.join('.')
}).rm();
}
}
},
get : function (name) {
var cookie = this.getExisting(name), value = null;
if (cookie) { // eslint-disable-next-line
value = cookie.value;
}
return value;
}
};