/
sniffer.js
489 lines (423 loc) · 13.7 KB
/
sniffer.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
/*
* Sniffer - sniffs web pages to extract information such as JS libraries, CMS, analytics packages, etc.
* Author: Mark Perkins, mark@allmarkedup.com
*/
var Sniffer = (function( win, doc, undefined ){
var sniff = {},
detect = {},
pageinfo = {},
test_runner = {},
results = {},
indexed_results = {},
scripts = doc.getElementsByTagName("script"),
metas = doc.getElementsByTagName("meta"),
html = doc.documentElement.outerHTML || doc.documentElement.innerHTML,
doctype = doc.doctype,
has_run = false;
// discard meta tags that aren't useful
metas = (function(){
for ( var meta, temp = [], i = -1; meta = metas[++i]; )
{
if ( meta.name && meta.content ) temp.push(meta);
}
return temp;
})();
// discard script tags that aren't useful
scripts = (function(){
for ( var script, temp = [], i = -1; script = scripts[++i]; )
{
if ( script.src ) temp.push(scripts);
}
return temp;
})();
/* page component detection tests */
detect.pageinfo = {
description : 'Page information',
return_type : 'detail',
tests : {
'Doctype' : [
{
type : 'doctype', // source: http://www.w3.org/QA/2002/04/valid-dtd-list.html
test : {
'HTML5' : { name : 'html', publicId : '' },
'HTML 4.01 Strict' : { name : 'html', publicId : '-//W3C//DTD HTML 4.01//EN' },
'HTML 4.01 Transitional' : { name : 'html', publicId : '-//W3C//DTD HTML 4.01 Transitional//EN' },
'XHTML 1.0 Strict' : { name : 'html', publicId : '-//W3C//DTD XHTML 1.0 Strict//EN' },
'XHTML 1.0 Transitional' : { name : 'html', publicId : '-//W3C//DTD XHTML 1.0 Transitional//EN' },
'XHTML 1.0 Frameset' : { name : 'html', publicId : '-//W3C//DTD XHTML 1.0 Frameset//EN' },
'XHTML 1.1' : { name : 'html', publicId : '-//W3C//DTD XHTML 1.1//EN' },
'HTML 2.0' : { name : 'html', publicId : '-//IETF//DTD HTML 2.0//EN' },
'HTML 3.0' : { name : 'html', publicId : '-//W3C//DTD HTML 3.2 Final//EN' },
'XHTML 1.0 Basic' : { name : 'html', publicId : '-//W3C//DTD XHTML Basic 1.0//EN' }
}
}
],
'Charset' : [
{
type : 'custom',
test : function(){ return doc.characterSet || 'None detected'; }
}
]
}
};
detect.js_libs = {
description : 'JavaScript Libraries',
return_type : 'version',
// All individual tests should either return a version number, true or false.
tests : {
'jQuery' : [
{
type : 'custom',
test : function(){ return win.jQuery ? win.jQuery.fn.jquery : false; }
}
],
'jQuery UI' : [
{
type : 'custom',
test : function(){ return win.jQuery && win.jQuery.ui ? win.jQuery.ui.version : false; }
}
],
'Prototype' : [
{
type : 'custom',
test : function(){ return win.Prototype ? win.Prototype.Version : false; }
}
],
'Scriptaculous' : [
{
type : 'custom',
test : function(){ return win.Scriptaculous ? win.Scriptaculous.Version : false; }
}
],
'MooTools' : [
{
type : 'custom',
test : function(){ return win.MooTools ? win.MooTools.version : false; }
}
],
'Glow' : [
{
type : 'custom',
test : function(){ return win.glow ? win.glow.VERSION : false; }
}
],
'Dojo' : [
{
type : 'custom',
test : function(){ return win.dojo ? win.dojo.version.toString() : false; }
}
],
'ExtJS' : [
{
type : 'custom',
test : function(){ return win.Ext ? win.Ext.version : false; }
}
],
'YUI' : [
{
type : 'custom',
test : function(){ return win.YAHOO ? win.YAHOO.VERSION : false; }
}
],
'Google Closure' : [
{
type : 'custom',
test : function(){ return !! win.goog; } // need to figure out how to get YUI version
}
],
'Modernizr' : [
{
type : 'custom',
test : function(){ return win.Modernizr ? win.Modernizr._version : false; } // need to figure out how to get YUI version
}
],
'Raphael' : [
{
type : 'custom',
test : function(){ return win.Raphael ? win.Raphael.version : false; }
}
]
}
};
detect.cms = {
description : 'Content Management System',
return_type : 'version',
tests : {
'Wordpress' : [
{
type : 'meta',
test : { name : 'generator', match : /WordPress\s?([\w\d\.\-_]*)/i }
},
{
type : 'text',
test : /<link rel=["|']stylesheet["|'] [^>]+wp-content/i
}
],
'Typepad' : [
{
type : 'meta',
test : { name : 'generator', match : /typepad\.com/i }
}
],
'Joomla' : [
{
type : 'meta',
test : { name : 'generator', match : /joomla\!?\s?([\d.]*)/i }
}
],
'Blogger' : [
{
type : 'meta',
test : { name : 'generator', match : /blogger/i }
}
],
'MovableType' : [
{
type : 'meta',
test : { name : 'generator', match : /Movable Type Pro ([\d.]*)/i }
}
],
'Drupal' : [
{
type : 'custom',
test : function() { return win.Drupal ? true : false; } // no version in js obj
}
],
'Cisco Eos' : [
{
type : 'custom',
test : function() { return win.eos ? true : false; } // no version in js obj
},
{
type : 'text',
test : /<link rel=["|']stylesheet["|'] [^>]+ciscoeos.com/i
}
]
}
};
detect.analytics = {
description : 'Analytics',
return_type : 'version',
tests : {
'Google Analytics' : [
{
type : 'custom',
test : function(){ return !! (win._gat || win._gaq); }
}
],
'Reinvigorate' : [
{
type : 'custom',
test : function(){ return !! win.reinvigorate; }
}
],
'Piwik' : [
{
type : 'custom',
test : function(){ return !! win.Piwik; }
}
],
'Clicky' : [
{
type : 'custom',
test : function(){ return !! win.clicky; }
}
],
'Open Web Analytics' : [
{
type : 'custom',
test : function() { return !! win.OWA; }
}
]
}
};
detect.fonts = {
description : 'Fonts',
return_type : 'version',
tests : {
'Cufon' : [
{
type : 'custom',
test : function(){ return !! win.Cufon }
}
],
'Typekit' : [
{
type : 'custom',
test : function(){ return !! win.Typekit }
}
],
'Fontdeck' : [
{
type : 'text',
test : /<link rel=["|']stylesheet["|'] [^>]+f.fontdeck.com/i
}
],
'Google Webfonts' : [
{
type : 'custom',
test : function(){ return !! win.WebFont }
}
],
'sIFR' : [
{
type : 'custom',
test : function(){ return win.sIFR ? win.sIFR.VERSION : false }
}
]
}
};
/* test runners */
// custom tests just run a function that returns a version number, true or false.
test_runner.custom = function( test )
{
return test();
}
// one off regexp-based tests
test_runner.text = function( test )
{
return match( html, test );
}
if ( doctype )
{
test_runner.doctype = function( test )
{
for ( subtest in test )
{
if ( test.hasOwnProperty(subtest) )
{
var t = test[subtest];
if ( doctype.name.toLowerCase() == t.name && doctype.publicId == t.publicId )
{
return subtest;
}
}
}
return false;
}
}
else
{
test_runner.doctype = function(){
return 'None detected';
}
}
// check the script src... probably pretty unreliable
if ( scripts.length )
{
test_runner.script = function( test )
{
for ( var script, i = -1; script = scripts[++i]; )
{
return match( script.src, test );
}
return false;
}
}
else
{
// no scripts, tests will always return false.
test_runner.script = function(){ return false; }
}
// check the meta elements in the head
if ( metas.length )
{
test_runner.meta = function( test )
{
for ( var meta, i = -1; meta = metas[++i]; )
{
if ( meta.name == test.name )
{
var res = match( meta.content, test.match );
if ( res ) return res;
}
}
return false;
}
}
else
{
// there are no meta elements on the page so this will always return false
test_runner.meta = function(){ return false; }
}
// test arg should be a regexp, in which the only *specific* match is the version number
function match( str, test )
{
var match = str.match(test);
if ( match ) return match[1] && match[1] != '' ? match[1] : true; // return version number if poss or else true.
return false;
}
/* main function responsible for running the tests */
var run = function( tests_array )
{
for ( var check, i = -1; check = tests_array[++i]; )
{
var result = test_runner[check.type]( check.test );
if ( result !== false ) return result;
}
return false;
}
var empty = function( obj )
{
for ( var name in obj ) return false;
return true;
}
// utility function for iterating over the tests
var forEachTest = function( callback )
{
for ( group in detect )
{
if ( detect.hasOwnProperty(group) )
{
for ( test in detect[group].tests )
{
if ( detect[group].tests.hasOwnProperty(test) )
{
if ( callback( group, test ) === false ) return;
}
}
}
}
}
var addToResults = function( group, test, res )
{
// add results to group results object
results[group] = results[group] || {};
results[group].results = results[group].results || {};
results[group].description = detect[group].description;
results[group].return_type = detect[group].return_type;
results[group]['results'][test] = res;
// add the result to the name-index results object
indexed_results[test.toLowerCase()] = res;
}
/* publicly available methods */
// return results of all checks run so far
sniff.results = function(){
return results;
};
// perform an individual check
sniff.check = function( to_test )
{
to_test = to_test.toLowerCase();
if ( indexed_results[to_test] != undefined ) return indexed_results[to_test];
else {
forEachTest(function( group, test ){
if ( test.toLowerCase() === to_test )
{
addToResults( group, test, run( detect[group].tests[test] ) );
return false; // break out of forEachTest loop
}
});
}
return indexed_results[to_test];
};
// run or re-run all checks
sniff.run = function()
{
forEachTest(function( group, test ){
addToResults( group, test, run( detect[group].tests[test] ) );
});
return sniff.results();
};
return sniff;
})( window, document );