-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpcookie.js
295 lines (251 loc) · 7.77 KB
/
rpcookie.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* rpcookie 0.1
* https://github.com/00cpxxx/rpcookie/
* Copyright (c) 2016-2099 Bruno Jesus
* Released under the MIT license
*/
var rpcookie =
{
/*
* prefix cookie name prefix
* pages internal list of all opened pages
* interval interval to poll everything
* callbacks callback pointers to open and close functions
* timer_ix index of timer returned by setInterval
* loaded set after calling the load done callback
* runs number of times the work loop was called (resets at 6)
* my_name will store my randomly generated cookie name
*/
/* ================================= STATE ================================ */
init: function(params)
{
if (rpcookie.timer_ix)
return true;
if (typeof document.cookie != 'string' || !navigator.cookieEnabled)
return false;
rpcookie.prefix = 'rpc_';
rpcookie.pages = {};
rpcookie.interval = 300;
rpcookie.loaded = false;
rpcookie.runs = 0;
rpcookie.callbacks = { new: rpcookie.internal_new,
close: rpcookie.internal_close,
load: rpcookie.internal_load };
rpcookie.config(params);
window.addEventListener('unload', rpcookie.unload_event);
if (!rpcookie.register_me(true))
return false;
rpcookie.timer_ix = setInterval(rpcookie.timer_func, rpcookie.interval);
if (!rpcookie.timer_ix) /* insanity check */
rpcookie.del_cookie(rpcookie.my_name);
return !!rpcookie.timer_ix;
},
config: function(params)
{
if (typeof params != 'object')
return false;
if (typeof params.prefix == 'string')
rpcookie.prefix = params.prefix;
if (typeof params.interval == 'number')
rpcookie.interval = params.interval;
if (typeof params.new_page == 'function')
rpcookie.callbacks.new = params.new_page;
if (typeof params.close_page == 'function')
rpcookie.callbacks.close = params.close_page;
if (typeof params.load_done == 'function')
rpcookie.callbacks.load = params.load_done;
return true;
},
register_me: function(rename)
{
if (rename)
rpcookie.my_name = rpcookie.prefix+rpcookie.random();
if (!rpcookie.add_cookie(rpcookie.my_name, '%'))
return false;
var page = rpcookie.register_page(rpcookie.my_name);
page.is_new = false; /* do not callback for ourself */
rpcookie.update_me();
return true;
},
stop: function()
{
if (rpcookie.timer_ix)
{
clearInterval(rpcookie.timer_ix);
rpcookie.timer_ix = false;
rpcookie.del_cookie(rpcookie.my_name);
}
return true;
},
internal_load: function()
{
console.log('load ok');
},
internal_new: function(page)
{
console.log('opened:', page.url);
},
internal_close: function(page)
{
console.log('closed:', page.url);
},
/* ======================================================================== */
/* ================================ PUBLIC ================================ */
list_pages: function()
{
var i, keys, ret;
for (i = 0, keys = Object.keys(rpcookie.pages), ret = []; i < keys.length; i++)
ret.push(rpcookie.make_data(keys[i]));
return ret;
},
/* ======================================================================== */
/* ================================ WORKER ================================ */
make_data: function(name)
{
return { 'url': rpcookie.pages[name].url,
'id': name.substring(rpcookie.prefix.length) }
},
timer_func: function(me)
{
var cookies = rpcookie.parse_cookies(), keys, i, checked, page;
for (i = 0, keys = Object.keys(cookies), checked = []; i < keys.length; i++)
{
/* add and update every page reading the cookie data */
if (typeof rpcookie.pages[keys[i]] == 'undefined')
page = rpcookie.register_page(keys[i]);
else
page = rpcookie.pages[keys[i]];
page.val = cookies[keys[i]];
rpcookie.parse_page(keys[i]);
/* when the page is inactive the browser will fire the event only every 1000ms,
* so wait a least 2 cycles before killing the page */
if (page.bad_runs > 2500 / rpcookie.interval)
rpcookie.del_cookie(keys[i]);
else
checked.push(keys[i]);
}
for (i = 0, keys = Object.keys(rpcookie.pages); i < keys.length; i++)
{
page = rpcookie.pages[keys[i]];
/* callback dead pages */
if (keys[i] != rpcookie.my_name && rpcookie.in_array(keys[i], checked) < 0)
{
delete rpcookie.pages[keys[i]];
rpcookie.callbacks.close(page);
}
/* callback new pages */
else if (page.is_new)
{
page.is_new = false;
if (rpcookie.loaded) /* already existing pages are not callback'ed */
rpcookie.callbacks.new(rpcookie.make_data(keys[i]));
}
}
rpcookie.runs++;
rpcookie.runs &= 7;
rpcookie.update_me();
if (!rpcookie.loaded)
{
rpcookie.callbacks.load();
rpcookie.loaded = true;
}
},
update_me: function()
{
var page = rpcookie.pages[rpcookie.my_name], str;
page.url = location.href;
str = '@$' +
'R' + rpcookie.runs + '$' +
'U' + page.url + '$';
page.val = str;
rpcookie.add_cookie(rpcookie.my_name, page.val);
},
parse_page: function(name)
{
var page = rpcookie.pages[name], data, runs, url;
data = page.val.split('$', 3);
if (data.length != 3 || data[0] != '@')
return false;
runs = parseInt(data[1][1]);
if (runs == page.runs)
page.bad_runs++;
else
page.bad_runs = 0;
page.runs = runs;
url = data[2].substring(1);
if (page.url != url)
page.is_new = true;
page.url = url;
},
register_page: function(name)
{
rpcookie.pages[name] = { is_new: true, url: '', runs: 0, bad_runs: 0 };
return rpcookie.pages[name];
},
/* ======================================================================== */
/* ============================ DOCUMENT COOKIE =========================== */
parse_cookies: function()
{
var cookies = {}, s, i, p, plen = rpcookie.prefix.length;
if (typeof document.cookie == 'string')
{
s = document.cookie.split(';');
for (i = 0; i < s.length; i++)
{
p = s[i].split('=');
if (p.length == 2 && (p[0] = rpcookie.trim(p[0])) &&
p[0].substring(0,plen) == rpcookie.prefix)
cookies[p[0]] = p[1];
}
}
/* check if somebody has cleaned the cookies */
if (typeof cookies[rpcookie.my_name] == 'undefined')
{
if (!rpcookie.register_me(false))
{
/* browser is blocking our cookies, we can no longer recover */
cookies = {};
rpcookie.stop();
}
}
else
delete cookies[rpcookie.my_name];
return cookies;
},
add_cookie: function(name, value)
{
document.cookie = name + '=' + value;
return document.cookie.indexOf(name) >= 0;
},
del_cookie: function(name)
{
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'
},
/* ======================================================================== */
/* ================================ HELPERS =============================== */
unload_event: function()
{
rpcookie.stop();
},
page_visible: function()
{
return !(document.hidden || document.webkitHidden || document.mozHidden || document.msHidden);
},
in_array: function(what, where)
{
var i;
for (i = 0; i < where.length; i++)
if (what == where[i])
return i;
return -1;
},
trim: function(str) /* str.trim is too new on IE */
{
return str.replace(/^\s+|\s+$/gm,'');
},
random: function()
{
return Math.floor(Math.random() * (999999 - 100000 + 1) + 100000);
}
/* ======================================================================== */
};