/
history_scrape.js
568 lines (515 loc) · 18.5 KB
/
history_scrape.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
This javascript is used to run the CSS history hack in production.
Original authors: Jeremiah Grossman (license: BSD) and Daniel Bartlett
Revision author: Sai Emrys (license: CC by-sa)
http://jeremiahgrossman.blogspot.com/2006/08/i-know-where-youve-been.html
http://www.gnucitizen.org/projects/javascript-visited-link-scanner
http://code.google.com/p/attackapi/source/browse/trunk/lib/dom/scanHistory.js
*/
var CSSHistory = {};
CSSHistory.prefixes = ['http://', 'https://', 'http://www.', 'https://www.'];
CSSHistory.hex_color = '#ff0000'
CSSHistory.rgb_color = 'rgb(255, 0, 0)'
// We need to force the links to a known color value to be able to test them
CSSHistory.prep = function() {
stylesheet = "<style> \
a.csshistory {color: #00ff00; display: none;} \
a.csshistory:visited {color: #ff0000; display: inline;} \
div.csshistory a { display: none;} \
div.csshistory a:visited { display: inline; } \
</style>"
document.write(stylesheet);
};
CSSHistory.test = function(link) {
// Version 2 - modified from AttackAPI
if (link.currentStyle) // IE
return link.currentStyle['display'] == 'inline';
else
return document.defaultView.getComputedStyle(link, null).getPropertyValue('display') == 'inline';
};
CSSHistory.test_color = function(link) {
// document.defaultView is not supported by IE, nor is getComputedStyle. So for IE we use currentStyle instead.
// Cf. http://www.quirksmode.org/dom/w3c_html.html http://www.quirksmode.org/dom/w3c_css.html
// Version 1 - modified from the original hack
if(document.defaultView)
return document.defaultView.getComputedStyle(link,null).getPropertyValue("color") == CSSHistory.rgb_color;
else // IE
return link.currentStyle['color'] == CSSHistory.hex_color;
};
CSSHistory.test_width = function(link) {
return link.offsetWidth > 0 // cross-browser compatible, yay
};
// When called, this should probably be wrapped in JSON.stringify() for export back up to AJAX
// Some browsers (versions? OSes? plugins?) return bogus results even with methods that are known working for the browser as a whole.
// I have no idea why, yet.
// However, to correct for this, we make sure the method selftests as nonbogus before using it for the real scrape, falling through to other methods if it doesn't
// If all methods are bogus, then return nothing
CSSHistory.check_batch = function(urls, with_variants) {
switch(BrowserDetect.browser) {
case 'Chrome': // ~2MURL/min (!!)
if (CSSHistory.selftest('reuse_insert'))
return CSSHistory.check_batch_with(urls, 'reuse_insert', with_variants);
case 'Opera': // ~210kURL/min
case 'Firefox': // ~210kURL/min
if (CSSHistory.selftest('mass_insert'))
return CSSHistory.check_batch_with(urls, 'mass_insert', with_variants);
case 'Mozilla': // ~400kURL/min
if (CSSHistory.selftest('reuse_noinsert'))
return CSSHistory.check_batch_with(urls, 'reuse_noinsert', with_variants);
case 'Explorer': // ~200kURL/min
case 'Safari': // ~3.4MURL/min (!!!)
default:
if (CSSHistory.selftest('jquery'))
return CSSHistory.check_batch_with(urls, 'jquery', with_variants);
if (CSSHistory.selftest('mass_insert'))
return CSSHistory.check_batch_with(urls, 'mass_insert', with_variants);
if (CSSHistory.selftest('reuse_noinsert'))
return CSSHistory.check_batch_with(urls, 'reuse_noinsert', with_variants);
if (CSSHistory.selftest('reuse_insert'))
return CSSHistory.check_batch_with(urls, 'reuse_insert', with_variants);
if (CSSHistory.selftest('reuse_reinsert'))
return CSSHistory.check_batch_with(urls, 'reuse_reinsert', with_variants);
if (CSSHistory.selftest('full_insert'))
return CSSHistory.check_batch_with(urls, 'full_insert', with_variants);
// If it ever hits this, then all tests have utterly failed.
return {}
}
};
CSSHistory.selftest = function(method) {
// check this site (just visited!) and some random garbage that can't possibly be visited
fake_url = hex_md5(String(Math.random() * 50000).replace(/\D/gi,''))
result = CSSHistory.check_batch_with(['cssfingerprint.com', ], method)
return (result['cssfingerprint.com'] && !result[fake_url])
}
CSSHistory.methods = ['jquery_noinsert', 'jquery', 'reuse_noinsert', 'reuse_insert','reuse_reinsert','full_reinsert',
'mass_insert', 'mass_noinsert', 'mass_noinsert_width', 'reuse_noinsert_width', 'full_reinsert_width'];
// with_variants controls whether http/https x bare/www variants are tested; must pass explicit false to disable
CSSHistory.check_batch_with = function(urls, method, with_variants) {
if (urls.constructor == Array) {
// backwards compatibility. Convert a simple array to a return-value hash.
new_urls = new Object();
for (var i = 0; i < urls.size(); i++) {
new_urls[urls[i]] = urls[i];
}
urls = new_urls;
}
switch(method) {
case 'jquery_noinsert':
return CSSHistory.check_batch_jquery_noinsert(urls, with_variants);
case 'jquery':
return CSSHistory.check_batch_jquery(urls, with_variants);
case 'mass_insert_width':
return CSSHistory.check_batch_mass_insert_width(urls, with_variants);
case 'mass_insert':
return CSSHistory.check_batch_mass_insert(urls, with_variants);
case 'mass_noinsert':
return CSSHistory.check_batch_mass_noinsert(urls, with_variants);
case 'reuse_noinsert_width': // NOTE: reuse_noinsert appears to crash IE in at least some cases. No idea why yet.
return CSSHistory.check_batch_reuse_noinsert_width(urls, with_variants);
case 'reuse_noinsert': // NOTE: reuse_noinsert appears to crash IE in at least some cases. No idea why yet.
return CSSHistory.check_batch_reuse_noinsert(urls, with_variants);
case 'reuse_insert':
return CSSHistory.check_batch_reuse_insert(urls, with_variants);
case 'reuse_reinsert':
return CSSHistory.check_batch_reuse_reinsert(urls, with_variants);
case 'full_reinsert_width':
return CSSHistory.check_batch_full_reinsert_width(urls, with_variants);
case 'full_reinsert':
return CSSHistory.check_batch_full_reinsert(urls, with_variants);
}
};
// Query URLs in bulk thanks to jQuery's visible selector - http://api.jquery.com/visible-selector/
// requires jQuery. Assumes that var $j = jQuery.noConflict();
// requires CSS:
// <style>
// div.csshistory a { display: none;}
// div.csshistory a:visited { display: inline; }
// </style>
CSSHistory.check_batch_jquery = function(urls, with_variants) {
result = {};
string_to_insert = '';
var div = document.createElement('div');
div.className = 'csshistory';
var id = hex_md5(String(Math.random() * 50000).replace(/\D/gi,''));
div.id = id;
for(i in urls ){
result[escape(urls[i])] = false;
string_to_insert = string_to_insert + '<a href="http://' + i + '">' + urls[i] + '</a>' ;
if (with_variants !== false) { // false !== but == undefined
string_to_insert = string_to_insert + '<a href="https://' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a href="http://www.' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a href="https://www.' + i + '">' + urls[i] + '</a>';
}
}
// ~20 ms per 500 in Firefox
div.innerHTML = string_to_insert;
// ~300 ms
document.body.appendChild(div);
// ~60 ms
$j("#" + id + " :visible").each(function() { // w00t visible selector
result[escape($j(this).text())] = true;
});
// 13 ms
document.body.removeChild(div);
return result
}
CSSHistory.check_batch_jquery_noinsert = function(urls, with_variants) {
result = {};
string_to_insert = '';
var div = document.createElement('div');
div.className = 'csshistory';
var id = hex_md5(String(Math.random() * 50000).replace(/\D/gi,''));
div.id = id;
for(i in urls ){
result[escape(urls[i])] = false;
string_to_insert = string_to_insert + '<a href="http://' + i + '">' + urls[i] + '</a>' ;
if (with_variants !== false) { // false !== but == undefined
string_to_insert = string_to_insert + '<a href="https://' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a href="http://www.' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a href="https://www.' + i + '">' + urls[i] + '</a>';
}
}
div.innerHTML = string_to_insert;
$j("#" + id + " :visible").each(function() { // w00t visible selector
result[escape($j(this).text())] = true;
});
return result
}
// Insert a batch, but check each link individually
CSSHistory.check_batch_mass_insert = function(urls, with_variants){
result = {};
string_to_insert = '';
var div = document.createElement('div');
div.className = 'csshistory';
var id = hex_md5(String(Math.random() * 50000).replace(/\D/gi, ''));
div.id = id;
ids_to_check = [];
for (i in urls) {
result[escape(urls[i])] = false;
string_to_insert = string_to_insert + '<a id="' + 'ip' + urls[i] + '" href="http://' + i + '">' + urls[i] + '</a>';
ids_to_check.push('ip' + urls[i]);
if (with_variants !== false) { // false !== but == undefined
string_to_insert = string_to_insert + '<a id="' + 'sp' + urls[i] + '" href="https://' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a id="' + 'iw' + urls[i] + '" href="http://www.' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a id="' + 'sw' + urls[i] + '" href="https://www.' + i + '">' + urls[i] + '</a>';
ids_to_check.push('sp' + urls[i]);
ids_to_check.push('iw' + urls[i]);
ids_to_check.push('sw' + urls[i]);
}
}
div.innerHTML = string_to_insert;
document.body.appendChild(div);
for (i = 0; i < ids_to_check.size(); i++) {
link = $(ids_to_check[i]);
if (CSSHistory.test(link)) {
result[escape(ids_to_check[i].substring(2))] = true ;
}
}
document.body.removeChild(div);
return result
}
// Insert a batch, but check each link individually, using width to test
CSSHistory.check_batch_mass_insert_width = function(urls, with_variants){
result = {};
string_to_insert = '';
var div = document.createElement('div');
div.className = 'csshistory';
var id = hex_md5(String(Math.random() * 50000).replace(/\D/gi, ''));
div.id = id;
ids_to_check = [];
for (i in urls) {
result[escape(urls[i])] = false;
string_to_insert = string_to_insert + '<a id="' + 'ip' + urls[i] + '" href="http://' + i + '">' + urls[i] + '</a>';
ids_to_check.push('ip' + urls[i]);
if (with_variants !== false) { // false !== but == undefined
string_to_insert = string_to_insert + '<a id="' + 'sp' + urls[i] + '" href="https://' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a id="' + 'iw' + urls[i] + '" href="http://www.' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a id="' + 'sw' + urls[i] + '" href="https://www.' + i + '">' + urls[i] + '</a>';
ids_to_check.push('sp' + urls[i]);
ids_to_check.push('iw' + urls[i]);
ids_to_check.push('sw' + urls[i]);
}
}
div.innerHTML = string_to_insert;
document.body.appendChild(div);
for (i = 0; i < ids_to_check.size(); i++) {
link = $(ids_to_check[i]);
if (CSSHistory.test_width(link)) {
result[escape(ids_to_check[i].substring(2))] = true ;
}
}
document.body.removeChild(div);
return result
}
// Insert a batch, but check each link individually
CSSHistory.check_batch_mass_noinsert = function(urls, with_variants){
result = {};
string_to_insert = '';
var div = document.createElement('div');
div.className = 'csshistory';
var id = hex_md5(String(Math.random() * 50000).replace(/\D/gi, ''));
div.id = id;
ids_to_check = [];
for (i in urls) {
result[escape(urls[i])] = false;
string_to_insert = string_to_insert + '<a id="' + 'ip' + urls[i] + '" href="http://' + i + '">' + urls[i] + '</a>';
ids_to_check.push('ip' + urls[i]);
if (with_variants !== false) { // false !== but == undefined
string_to_insert = string_to_insert + '<a id="' + 'sp' + urls[i] + '" href="https://' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a id="' + 'iw' + urls[i] + '" href="http://www.' + i + '">' + urls[i] + '</a>';
string_to_insert = string_to_insert + '<a id="' + 'sw' + urls[i] + '" href="https://www.' + i + '">' + urls[i] + '</a>';
ids_to_check.push('sp' + urls[i]);
ids_to_check.push('iw' + urls[i]);
ids_to_check.push('sw' + urls[i]);
}
}
div.innerHTML = string_to_insert;
document.body.appendChild(div);
for (i = 0; i < ids_to_check.size(); i++) {
link = $(ids_to_check[i]);
try {
if (CSSHistory.test(link)) {
result[escape(ids_to_check[i].substring(2))] = true;
}
}
catch (err) {;} // if this failed, then the method is bogus
}
document.body.removeChild(div);
return result
}
// Just make & reuse the link. Don't even insert into DOM.
CSSHistory.check_batch_reuse_noinsert = function(urls, with_variants){
result = {};
var link = document.createElement("a");
link.className = 'csshistory';
for (i in urls) { // i is the URL, urls[i] is the return value (i.e. site id)
var found = false;
var j = 0
do { // if with_variants === false, only run this once
link.href = CSSHistory.prefixes[j] + i;
found = found || CSSHistory.test(link);
j++;
} while (!found && (j < CSSHistory.prefixes.length) && (with_variants !== false))
result[escape(urls[i])] = found;
};
return result;
};
CSSHistory.check_batch_reuse_noinsert_width = function(urls, with_variants){
result = {};
var link = document.createElement("a");
link.className = 'csshistory';
for (i in urls) { // i is the URL, urls[i] is the return value (i.e. site id)
var found = false;
var j = 0
do { // if with_variants === false, only run this once
link.href = CSSHistory.prefixes[j] + i;
found = found || CSSHistory.test_width(link);
j++;
} while (!found && (j < CSSHistory.prefixes.length) && (with_variants !== false))
result[escape(urls[i])] = found;
};
return result;
};
// Insert it into DOM, but reuse the same thing over again.
CSSHistory.check_batch_reuse_insert = function(urls, with_variants){
result = {};
var link = document.createElement("a");
link.className = 'csshistory';
document.body.appendChild(link);
for (i in urls) {
var found = false;
var j = 0
do {
link.href = CSSHistory.prefixes[j] + i;
found = found || CSSHistory.test(link);
j++;
} while (!found && (j < CSSHistory.prefixes.length) && (with_variants !== false))
result[escape(urls[i])] = found;
};
document.body.removeChild(link);
return result;
};
// Reuse the same link, but reinsert it into DOM each time
CSSHistory.check_batch_reuse_reinsert = function(urls, with_variants){
result = {};
var link = document.createElement("a");
link.className = 'csshistory';
for (i in urls) {
var found = false;
var j = 0
do {
document.body.appendChild(link);
link.href = CSSHistory.prefixes[j] + i;
found = found || CSSHistory.test(link);
document.body.removeChild(link);
j++;
} while (!found && (j < CSSHistory.prefixes.length) && (with_variants !== false))
result[escape(urls[i])] = found;
};
return result;
};
// Completely redo the link each time.
CSSHistory.check_batch_full_reinsert = function(urls, with_variants) {
result = {};
for (i in urls) {
var found = false;
var j = 0
do {
var link = document.createElement("a");
link.className = 'csshistory';
link.href = CSSHistory.prefixes[j] + i;
document.body.appendChild(link);
found = found || CSSHistory.test(link);
document.body.removeChild(link);
j++;
} while (!found && (j < CSSHistory.prefixes.length) && (with_variants !== false))
result[escape(urls[i])] = found;
};
return result;
};
// Completely redo the link each time.
CSSHistory.check_batch_full_reinsert_width = function(urls, with_variants) {
result = {};
for (i in urls) {
var found = false;
var j = 0
do {
var link = document.createElement("a");
link.className = 'csshistory';
link.href = CSSHistory.prefixes[j] + i;
document.body.appendChild(link);
found = found || CSSHistory.test_width(link);
document.body.removeChild(link);
j++;
} while (!found && (j < CSSHistory.prefixes.length) && (with_variants !== false))
result[escape(urls[i])] = found;
};
return result;
};
// timing utility
var timeDiff = {
setStartTime:function (){
d = new Date();
time = d.getTime();
},
getDiff:function (){
d = new Date();
return (d.getTime()-time);
}
};
// Taken from http://www.quirksmode.org/js/detect.html
var BrowserDetect = {
init: function () {
this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
this.version = this.searchVersion(navigator.userAgent)
|| this.searchVersion(navigator.appVersion)
|| "an unknown version";
this.OS = this.searchString(this.dataOS) || "an unknown OS";
},
searchString: function (data) {
for (var i=0;i<data.length;i++) {
var dataString = data[i].string;
var dataProp = data[i].prop;
this.versionSearchString = data[i].versionSearch || data[i].identity;
if (dataString) {
if (dataString.indexOf(data[i].subString) != -1)
return data[i].identity;
}
else if (dataProp)
return data[i].identity;
}
},
searchVersion: function (dataString) {
var index = dataString.indexOf(this.versionSearchString);
if (index == -1) return;
return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
},
dataBrowser: [
{
string: navigator.userAgent,
subString: "Chrome",
identity: "Chrome"
},
{ string: navigator.userAgent,
subString: "OmniWeb",
versionSearch: "OmniWeb/",
identity: "OmniWeb"
},
{
string: navigator.vendor,
subString: "Apple",
identity: "Safari",
versionSearch: "Version"
},
{
prop: window.opera,
identity: "Opera"
},
{
string: navigator.vendor,
subString: "iCab",
identity: "iCab"
},
{
string: navigator.vendor,
subString: "KDE",
identity: "Konqueror"
},
{
string: navigator.userAgent,
subString: "Firefox",
identity: "Firefox"
},
{
string: navigator.vendor,
subString: "Camino",
identity: "Camino"
},
{ // for newer Netscapes (6+)
string: navigator.userAgent,
subString: "Netscape",
identity: "Netscape"
},
{
string: navigator.userAgent,
subString: "MSIE",
identity: "Explorer",
versionSearch: "MSIE"
},
{
string: navigator.userAgent,
subString: "Gecko",
identity: "Mozilla",
versionSearch: "rv"
},
{ // for older Netscapes (4-)
string: navigator.userAgent,
subString: "Mozilla",
identity: "Netscape",
versionSearch: "Mozilla"
}
],
dataOS : [
{
string: navigator.platform,
subString: "Win",
identity: "Windows"
},
{
string: navigator.platform,
subString: "Mac",
identity: "Mac"
},
{
string: navigator.userAgent,
subString: "iPhone",
identity: "iPhone/iPod"
},
{
string: navigator.platform,
subString: "Linux",
identity: "Linux"
}
]
};
BrowserDetect.init();