forked from minj/foxtrick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookies.js
279 lines (238 loc) · 6.23 KB
/
cookies.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* cookies.js
* cookie management
*
* @author convincedd, LA-MJ
*/
'use strict';
/* eslint-disable */
if (!this.Foxtrick)
// @ts-ignore
var Foxtrick = {};
/* eslint-enable */
/**
* Cookie specification object
*/
Foxtrick.COOKIE_SPEC = {
// eslint-disable-next-line camelcase
for_hty: {
url: 'https://www.hattrick-youthclub.org/*',
name: 'fromFoxtrick',
addId: true,
domain: '.hattrick-youthclub.org',
isJSON: true,
isBase64: true,
},
// eslint-disable-next-line camelcase
from_hty: {
url: 'https://hattrick-youthclub.org/*',
name: 'forFoxtrick',
addId: true,
domain: '.hattrick-youthclub.org',
isJSON: true,
isBase64: true,
},
};
for (let k of Object.keys(Foxtrick.COOKIE_SPEC))
Object.freeze(Foxtrick.COOKIE_SPEC[k]);
Object.freeze(Foxtrick.COOKIE_SPEC);
/** @typedef {keyof Foxtrick.COOKIE_SPEC} CookieKey */
Foxtrick.cookies = (function() {
const COOKIE_SPEC = Foxtrick.COOKIE_SPEC;
/**
* Parse a value from a cookie string according to spec:
* {isJSON, isBase64: Boolean}.
*
* base64 may only be used when isJSON=true.
*
* @param {string} str
* @param {object} spec
* @return {object}
*/
var parseVal = function(str, spec) {
if (!str)
return {};
if (!spec.isJSON)
return str;
if (!spec.isBase64)
return JSON.parse(str);
return JSON.parse(Foxtrick.decodeBase64(str));
};
/**
* Prepare a value for storing in a cookie according to spec:
* {isJSON, isBase64: Boolean}.
*
* base64 may only be used when isJSON=true.
*
* @param {object} val
* @param {object} spec
* @return {string}
*/
var stringifyVal = function(val, spec) {
if (!val)
return '';
if (!spec.isJSON)
return val.toString();
if (!spec.isBase64)
return JSON.stringify(val);
return Foxtrick.encodeBase64(JSON.stringify(val));
};
/**
* Create a chrome API Cookie object.
*
* Returns {url, domain, name, value: string}
*
* @param {string} key
* @param {string} name
* @param {object} oldVal
* @param {object} val
* @return {chrome.cookies.SetDetails}
*/
var makeCookie = function(key, name, oldVal, val) {
const spec = COOKIE_SPEC[key];
const { url, domain } = spec;
/** @type {chrome.cookies.SetDetails} */
const cookie = { url, domain, name };
if (spec.isJSON) {
let old = Object.assign(oldVal || {}, val);
cookie.value = stringifyVal(old, spec);
}
else {
cookie.value = stringifyVal(val, spec);
}
return cookie;
};
/**
* A global Promise to limit concurrency while cookie.set is in progress
* @type {Promise}
*/
var gCookiesReady = Promise.resolve();
/**
* Get a promise when cookie value is set.
*
* Promise will never reject.
*
* Cookie storage key must be preset in COOKIE_SPEC.
*
* cookieName is optional cookie name override in content
* which is **REQUIRED** in BG
*
* value may be any stringify-able object.
*
* @param {CookieKey} key
* @param {object} value
* @param {string} [cookieName] optional in content, **REQUIRED** in BG
* @return {Promise}
*/
var set = function(key, value, cookieName) {
const spec = COOKIE_SPEC[key];
let name = cookieName ||
(spec.addId ? spec.name + '_' + Foxtrick.util.id.getOwnTeamId() : spec.name);
if (Foxtrick.context == 'content') {
return new Promise(function(fulfill) {
Foxtrick.SB.ext.sendRequest({
req: 'cookiesSet',
key,
value,
name,
}, fulfill);
});
}
return Foxtrick.cookies.get(key, name).then(function(oldVal) {
let cookie = makeCookie(key, name, oldVal, value);
if (Foxtrick.arch === 'Gecko') {
try {
// expire just to make the function happy, no effect
// when the param before is true (session only)
const MSECS_IN_WEEK = Foxtrick.util.time.DAYS_IN_WEEK *
Foxtrick.util.time.MSECS_IN_DAY;
const expire = Date.now() + MSECS_IN_WEEK;
Services.cookies.add(cookie.domain, '/', cookie.name, cookie.value,
false, true, true, expire);
}
catch (e) {
Foxtrick.log('Error setting cookie', key, value, cookie, e);
}
}
else if (Foxtrick.platform === 'Chrome') {
gCookiesReady = new Promise(function(resolve) {
try {
chrome.cookies.set(cookie, function(_) {
resolve();
});
}
catch (e) {
Foxtrick.log('Error setting cookie', key, value, cookie, e);
resolve();
}
});
}
return parseVal(cookie.value, spec);
});
};
/**
* Get a promise for a cookie value.
*
* Promise will never reject, returns null instead.
*
* Cookie storage key must be preset in COOKIE_SPEC.
*
* cookieName is optional cookie name override in content
* which is **REQUIRED** in BG
*
* value may be any stringify-able object or null if N/A.
*
* @param {CookieKey} key
* @param {string} [cookieName] optional in content, **REQUIRED** in BG
* @return {Promise} {Promise.<?value>}
*/
var get = function(key, cookieName) {
const spec = COOKIE_SPEC[key];
if (!spec)
Foxtrick.log(new Error(`spec is ${spec}`));
let name = cookieName ||
(spec.addId ? spec.name + '_' + Foxtrick.util.id.getOwnTeamId() : spec.name);
if (Foxtrick.context == 'content') {
return new Promise(function(fulfill) {
Foxtrick.SB.ext.sendRequest({ req: 'cookiesGet', key, name }, fulfill);
});
}
/** @type {chrome.cookies.Details} */
const cookie = { url: spec.url, name };
return gCookiesReady.then(function() {
if (Foxtrick.arch == 'Gecko') {
try {
let iter = Services.cookies.getCookiesFromHost(spec.domain);
while (iter.hasMoreElements()) {
let cookie = iter.getNext();
if (!(cookie instanceof Ci.nsICookie))
continue;
if (cookie.name == name && cookie.host == spec.domain)
return parseVal(cookie.value, spec);
}
}
catch (e) {
Foxtrick.log('Error getting cookie', key, e);
}
}
else if (Foxtrick.platform == 'Chrome') {
return new Promise(function(resolve) {
try {
chrome.cookies.get(cookie, (cookie) => {
if (cookie)
resolve(parseVal(cookie.value, spec));
else
resolve(null);
});
}
catch (e) {
Foxtrick.log('Error getting cookie', key, e);
resolve(null);
}
});
}
return null;
});
};
return { get, set };
})();