forked from brunosimon/burno.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
detector.class.js
324 lines (287 loc) · 10.6 KB
/
detector.class.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
/**
* @class Detector
* @author Bruno SIMON / http://bruno-simon.com
*/
B.Tools.Detector = B.Core.Event_Emitter.extend(
{
static : 'detector',
options :
{
targets : [ 'html' ]
},
/**
* Initialise and merge options
* @constructor
* @param {object} options Properties to merge with defaults
*/
construct : function( options )
{
this._super( options );
// Init
this.init_detection();
this.init_classes();
},
/**
* Detect engine, browser, system and feature in a specified list and store in 'detect' property
* @return {object} Context
*/
init_detection : function()
{
// Prepare
var engine = {
ie : 0,
gecko : 0,
webkit : 0,
khtml : 0,
opera : 0,
version : 0,
};
var browser = {
ie : 0,
firefox : 0,
safari : 0,
konq : 0,
opera : 0,
chrome : 0,
version : 0,
};
var system = {
windows : false,
mac : false,
osx : false,
iphone : false,
ipod : false,
ipad : false,
ios : false,
blackberry : false,
android : false,
opera_mini : false,
windows_mobile : false,
wii : false,
ps : false,
};
var features = {
touch : false,
media_query : false
};
// Detect
var user_agent = navigator.userAgent;
if( window.opera )
{
engine.version = browser.version = window.opera.version();
engine.opera = browser.opera = parseInt( engine.version );
}
else if( /AppleWebKit\/(\S+)/.test( user_agent ) || /AppleWebkit\/(\S+)/.test( user_agent ) )
{
engine.version = RegExp.$1;
engine.webkit = parseInt( engine.version );
// figure out if it's Chrome or Safari
if( /Chrome\/(\S+)/.test( user_agent ) )
{
browser.version = RegExp.$1;
browser.chrome = parseInt( browser.version );
}
else if( /Version\/(\S+)/.test( user_agent ) )
{
browser.version = RegExp.$1;
browser.safari = parseInt( browser.version );
}
else
{
// Approximate version
var safariVersion = 1;
if( engine.webkit < 100 )
safariVersion = 1;
else if( engine.webkit < 312 )
safariVersion = 1.2;
else if( engine.webkit < 412 )
safariVersion = 1.3;
else
safariVersion = 2;
browser.safari = browser.version = safariVersion;
}
}
else if( /KHTML\/(\S+)/.test( user_agent ) || /Konqueror\/([^;]+)/.test( user_agent ) )
{
engine.version = browser.version = RegExp.$1;
engine.khtml = browser.konq = parseInt( engine.version );
}
else if( /rv:([^\)]+)\) Gecko\/\d{8}/.test( user_agent ) )
{
engine.version = RegExp.$1;
engine.gecko = parseInt( engine.version );
// Determine if it's Firefox
if ( /Firefox\/(\S+)/.test( user_agent ) )
{
browser.version = RegExp.$1;
browser.firefox = parseInt( browser.version );
}
}
else if( /MSIE ([^;]+)/.test( user_agent ) )
{
engine.version = browser.version = RegExp.$1;
engine.ie = browser.ie = parseInt( engine.version );
}
else if( /Trident.*rv[ :]*(11[\.\d]+)/.test( user_agent ) )
{
engine.version = browser.version = RegExp.$1;
engine.ie = browser.ie = parseInt( engine.version );
}
// Detect browsers
browser.ie = engine.ie;
browser.opera = engine.opera;
// Detect platform (using navigator.plateform)
var plateform = navigator.platform;
// system.windows = plateform.indexOf( 'Win' ) === 0;
// system.mac = plateform.indexOf( 'Mac' ) === 0;
// system.x11 = ( plateform === 'X11' ) || ( plateform.indexOf( 'Linux' ) === 0);
// Detect platform (using navigator.userAgent)
system.windows = !!user_agent.match( /Win/ );
system.mac = !!user_agent.match( /Mac/ );
// system.x11 = ( plateform === 'X11' ) || ( plateform.indexOf( 'Linux' ) === 0);
// Detect windows operating systems
if( system.windows )
{
if( /Win(?:dows )?([^do]{2})\s?(\d+\.\d+)?/.test( user_agent ) )
{
if( RegExp.$1 === 'NT' )
{
switch( RegExp.$2 )
{
case '5.0':
system.windows = '2000';
break;
case '5.1':
system.windows = 'XP';
break;
case '6.0':
system.windows = 'Vista';
break;
default:
system.windows = 'NT';
break;
}
}
else if( RegExp.$1 === '9x' )
{
system.windows = 'ME';
}
else
{
system.windows = RegExp.$1;
}
}
}
// Detect mobile (mix between OS and device)
system.nokia = !!user_agent.match( /Nokia/i );
system.kindle_fire = !!user_agent.match( /Silk/ );
system.iphone = !!user_agent.match( /iPhone/ );
system.ipod = !!user_agent.match( /iPod/ );
system.ipad = !!user_agent.match( /iPad/ );
system.blackberry = !!user_agent.match( /BlackBerry/ ) || !!user_agent.match( /BB[0-9]+/ ) || !!user_agent.match( /PlayBook/ );
system.android = !!user_agent.match( /Android/ );
system.opera_mini = !!user_agent.match( /Opera Mini/i );
system.windows_mobile = !!user_agent.match( /IEMobile/i );
// iOS / OS X exception
system.ios = system.iphone || system.ipod || system.ipad;
system.osx = !system.ios && !!user_agent.match( /OS X/ );
// Detect gaming systems
system.wii = user_agent.indexOf( 'Wii' ) > -1;
system.playstation = /playstation/i.test( user_agent );
//Detect features (Not as reliable as Modernizr)
features.touch = !!( ( 'ontouchstart' in window ) || window.DocumentTouch && document instanceof DocumentTouch );
features.media_query = !!( window.matchMedia || window.msMatchMedia );
// Set up
this.user_agent = user_agent;
this.plateform = plateform;
this.browser = browser;
this.engine = engine;
this.system = system;
this.features = features;
this.categories = [ 'engine', 'browser', 'system', 'features' ];
},
/**
* Add detected informations to the DOM (on <html> by default)
* @return {object} Context
*/
init_classes : function()
{
// Don't add
if( !this.options.targets || this.options.targets.length === 0 )
return false;
// Set up
var targets = [],
target = null;
// Each element that need to add classes
for( var i = 0, len = this.options.targets.length; i < len; i++ )
{
// Target
target = this.options.targets[ i ];
// String
if( typeof target === 'string' )
{
// Target
switch( target )
{
case 'html' :
targets.push( document.documentElement );
break;
case 'body' :
targets.push( document.body );
break;
default :
var temp_targets = document.querySelectorAll( target );
for( var j = 0; j < temp_targets.length; j++ )
targets.push( temp_targets[ j ] );
break;
}
}
// DOM Element
else if( target instanceof Element )
{
targets.push( target );
}
// Targets found
if( targets.length )
{
this.classes = [];
// Each category
for( var category in this )
{
// Allowed
if( this.categories.indexOf( category ) !== -1 )
{
// Each property in category
for( var property in this[ category ] )
{
var value = this[ category ][ property ];
// Ignore version
if( property !== 'version' )
{
// Feature
if( category === 'features' )
{
this.classes.push( category + '-' + ( value ? '' : 'no-' ) + property );
}
// Not feature
else
{
if( value )
{
this.classes.push( category + '-' + property );
if( category === 'browser' )
this.classes.push( category + '-' + property + '-' + value );
}
}
}
}
}
}
// Add classes
for( var j = 0; j < targets.length; j++ )
targets[ j ].classList.add.apply( targets[ j ].classList, this.classes );
}
}
return this;
}
} );