forked from 3rd-Eden/useragent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseragent.js
More file actions
308 lines (279 loc) · 9.41 KB
/
useragent.js
File metadata and controls
308 lines (279 loc) · 9.41 KB
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
// low level user agent parsing, usefull for the most and common use cases
// returns true or false based on the check
exports.browser = function browser( ua ){
// make sure we are dealing with a lowercase useragent string
ua = ua.toLowerCase();
// inspired by the jQuery browser sniff
var result = {};
result.version = ( ua.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [ 0, "0" ] )[1];
result.webkit = ua.indexOf( "webkit" ) !== -1;
result.opera = ua.indexOf( "opera" ) !== -1;
result.ie = ua.indexOf( "msie" ) !== -1 && !result.opera;
result.firefox = ua.indexOf( "mozilla" ) !== -1 && ! ( result.webkit || ua.indexOf( "compatible" ) !== -1 );
result.chrome = ua.indexOf( "chrome" ) !== -1 && result.webkit;
result.safari = ua.indexOf( "safari") !== -1 && !result.chrome;
result.mobile_safari = ua.indexOf( "apple" ) !== -1 && ua.indexOf( "mobile" ) !== -1 && result.safari;
return result;
};
exports.parser = function parser( ua, js_ua ){
var V1 = V2 = V3 = family = false,
i = 0, length = browsers.length,
os, ua_obj, sequence;
// check we can parse down the ua based the parse object
function parse( parser_obj ){
var V1 = V2 = V3 = falsefamily = false,
match;
// check if we have positive match
match = parser_obj.regexp.exec( ua );
if( match ){
// check if we need to replace some items
if( parser_obj.family_replacement ){
if( parser_obj.family_replacement.indexOf( "$1" ) !== -1 )
family = parser_obj.family_replacement.replace( /\$1/, match[0] );
else
family = parser_obj.family_replacement;
} else
family = match[1];
// check for which version information is available
if( parser_obj.V1_replacement )
V1 = parser_obj.V1_replacement;
else if( match.length >= 2 )
V1 = match[2];
if( match.length >= 3 ){
V2 = match[3];
if( match.length >= 4 )
V3 = match[4];
}
}
// return the parse results
return {
match: match ? match[0] : false, // the matched string
family: family || "Other",
V1: V1,
V2: V2,
V3: V3
}
};
// create a pretty user agent string from the ua object
function pretty( user_agent, os ){
// use a supplied ua obj, or the buildin
var ua = user_agent ? user_agent : ua_obj
format = "$0";
// generate a pretty output format
if( ua.V1 ){
format = format + " $1";
if( ua.V2 ){
format = format + ".$2";
if( ua.V3 ){
// check if its a digit or not,
if( !isNaN( ua.V3 ) )
format = format + ".$3";
else
format = format + "$3";
}
}
}
// operating system
if( ua.os && os )
format = format + " / $4";
// replace placeholder with actual content
return format.replace( "$0", ua.family )
.replace( "$1", ua.V1 )
.replace( "$2", ua.V2 )
.replace( "$3", ua.V3 )
.replace( "$4", ua.os )
};
// the actual finding of the correct user agent group.
for( ; i < length; i++ ){
ua_obj = parse( browsers[i] );
// no need to continue parsing other sequence
if( ua_obj.match )
break;
}
// are we working with a chrome frame perhaps?
if( js_ua && ua.indexOf( "chromeframe" ) != -1 )
ua_obj.family = "Chrome Frame(" + ua_obj.family + " " + ua_obj.V1 + ")";
// based on operating system guessing from [ dronnikov@gmail.com ]
// rewritten to use UA style parsing
for( i = 0, length = operatingSystems.length; i < length; i++ ){
os = parse( operatingSystems[i] );
if( os.match ){
ua_obj.os = os;
break;
}
}
return {
// return results
family: ua_obj.family,
V1: ua_obj.V1,
V2: ua_obj.V2,
V3: ua_obj.V3,
match: ua_obj.match,
os: ua_obj.os,
// return methods that can be used
pretty: pretty,
prettyOs: function( os ){ return pretty( os || ua_obj.os ) }
}
};
// doing a .join("|") because its easier to maintain as array,
// if you want raw perfomance, compile this a string.
var browser_slash_v123_names = [
"Jasmine",
"ANTGalio",
"Midori",
"Fresco",
"Lobo",
"Maxthon",
"Lynx",
"OmniWeb",
"Dillo",
"Camino",
"Demeter",
"Fluid",
"Fennec",
"Shiira",
"Sunrise",
"Chrome",
"Flock",
"Netscape",
"Lunascape",
"Epiphany",
"WebPilot",
"Vodafone",
"NetFront",
"Konqueror",
"SeaMonkey",
"Kazehakase",
"Vienna",
"Iceape",
"Iceweasel",
"IceWeasel",
"Iron",
"K-Meleon",
"Sleipnir",
"Galeon",
"GranParadiso",
"Opera Mini",
"iCab",
"NetNewsWire",
"Iron",
"Iris",
"SeaRequin"
].join( "|" ),
browser_slash_v12_names = [
"Bolt",
"Jasmine",
"Maxthon",
"Lynx",
"Arora",
"IBrowse",
"Dillo",
"Camino",
"Shiira",
"Fennec",
"Phoenix",
"Chrome",
"Flock",
"Netscape",
"Lunascape",
"Epiphany",
"WebPilot",
"Opera Mini",
"Opera",
"Vodafone",
"NetFront",
"Konqueror",
"SeaMonkey",
"Kazehakase",
"Vienna",
"Iceape",
"Iceweasel",
"IceWeasel",
"Iron",
"K-Meleon",
"Sleipnir",
"Galeon",
"GranParadiso",
"iCab",
"NetNewsWire",
"Iron",
"Space Bison",
"Stainless",
"Orca"
].join( "|" ),
// the parser regexp storage, this will be used by the UA parser
browsers = [
// TOP CASES
// opera must go first
{ regexp: /^(Opera)\/(\d+)\.(\d+) \(Nintendo Wii/, family_replacement:"Wii" },
// must go before browser v1.v2
// eg: Minefield/3.1a1pre
{ regexp:/(Namoroka|Shiretoko|Minefield)\/(\d+)\.(\d+)\.(\d+(?:pre)?)/, family_replacement:"Firefox ($1)" },
{ regexp:/(Namoroka|Shiretoko|Minefield)\/(\d+)\.(\d+)([ab]\d+[a-z]*)?/, family_replacement:"Firefox ($1)" },
{ regexp:/(SeaMonkey|Fennec|Camino)\/(\d+)\.(\d+)([ab]?\d+[a-z]*)/ },
// eg: Flock/2.0b2
{ regexp:/(Flock)\/(\d+)\.(\d+)(b\d+?)/ },
// eg: Fennec/0.9pre
{ regexp:/(Fennec)\/(\d+)\.(\d+)(pre)/ },
{ regexp:/(Navigator)\/(\d+)\.(\d+)\.(\d+)/, family_replacement:"Netscape" },
{ regexp:/(Navigator)\/(\d+)\.(\d+)([ab]\d+)/, family_replacement:"Netscape" },
{ regexp:/(Netscape6)\/(\d+)\.(\d+)\.(\d+)/, family_replacement:"Netscape" },
{ regexp:/(MyIBrow)\/(\d+)\.(\d+)/, family_replacement:"My Internet Browser" },
{ regexp:/(Firefox).*Tablet browser (\d+)\.(\d+)\.(\d+)/, family_replacement:"MicroB" },
// Opera will stop at 9.80 and hide the real version in the Version string
// http://dev.opera.com/articles/view/opera-ua-string-changes/
{ regexp:/(Fennec)\/(\d+)\.(\d+)(pre)/ },
{ regexp:/(Firefox)\/(\d+)\.(\d+)\.(\d+(?:pre)?) \(Swiftfox\)/, family_replacement:"Swiftfox" },
{ regexp:/(Firefox)\/(\d+)\.(\d+)([ab]\d+[a-z]*)? \(Swiftfox\)/, family_replacement:"Swiftfox" },
// lowercase konqueror
{ regexp:/(konqueror)\/(\d+)\.(\d+)\.(\d+)/, family_replacement:"Konqueror" },
// MAIN CASES, this will match about 50% of the browsers
// browsers with v123
{ regexp:new RegExp( "(" + browser_slash_v123_names + ")\\/(\\d+)\\.(\\d+)\\.(\\d+)" ) },
// browser with v12
{ regexp:new RegExp( "(" + browser_slash_v12_names +")\\/(\\d+)\\.(\\d+)" ) },
// browser v123, space instead of slash
{ regexp:/(iRider|Crazy Browser|SkipStone|iCab|Lunascape|Sleipnir|Maemo Browser) (\d+)\.(\d+)\.(\d+)/ },
// browser v12, space instead of slash
{ regexp:/(iCab|Lunascape|Opera|Android) (\d+)\.(\d+)/ },
{ regexp:/(IEMobile) (\d+)\.(\d+)/, family_replacement:"IE Mobile" },
// do these after the "edge" cases
{ regexp:/(Firefox)\/(\d+)\.(\d+)\.(\d+)/ },
{ regexp:/(Firefox)\/(\d+)\.(\d+)(pre|[ab]\d+[a-z]*)?/ },
// SPECIAL CASES
{ regexp:/(Obigo|OBIGO)[^\d]*(\d+)(?:.(\d+))?/, family_replacement:"Obigo" },
{ regexp:/(MAXTHON|Maxthon) (\d+)\.(\d+)/, family_replacement:"Maxthon" },
{ regexp:/(Maxthon|MyIE2|Uzbl|Shiira)/, V1_replacement:"0" },
{ regexp:/(PLAYSTATION) (\d+)/, family_replacement:"PlayStation" },
{ regexp:/(PlayStation Portable)[^\d]+(\d+).(\d+)/ },
{ regexp:/(BrowseX) \((\d+)\.(\d+)\.(\d+)/ },
{ regexp:/(Opera)\/(\d+)\.(\d+).*Opera Mobi/, family_replacement:"Opera Mobile" },
{ regexp:/(POLARIS)\/(\d+)\.(\d+)/, family_replacement:"Polaris" },
{ regexp:/(BonEcho)\/(\d+)\.(\d+)\.(\d+)/, family_replacement:"Bon Echo" },
{ regexp:/(iPod|iPhone|iPad)(?:[a-zA-Z;\s])* OS (\d+)_(\d+)(?:_(\d+))?/ },
{ regexp:/(Avant)/, V1_replacement:"1" },
{ regexp:/(Nokia)[EN]?(\d+)/ },
{ regexp:/(Black[bB]erry)(\d+)/, family_replacement:"Blackberry" },
{ regexp:/(OmniWeb)\/v(\d+)\.(\d+)/ },
{ regexp:/(Blazer)\/(\d+)\.(\d+)/, family_replacement:"Palm Blazer" },
{ regexp:/(Pre)\/(\d+)\.(\d+)/, family_replacement:"Palm Pre" },
{ regexp:/(Links) \((\d+)\.(\d+)/ },
{ regexp:/(QtWeb) Internet Browser\/(\d+)\.(\d+)/ },
{ regexp:/(Version)\/(\d+)\.(\d+)(?:\.(\d+))?.*Safari\//, family_replacement:"Safari" },
{ regexp:/(OLPC)\/Update(\d+)\.(\d+)/ },
{ regexp:/(OLPC)\/Update()\.(\d+)/, V1_replacement:"0" },
{ regexp:/(SamsungSGHi560)/, family_replacement:"Samsung SGHi560" },
{ regexp:/^(SonyEricssonK800i)/, family_replacement:"Sony Ericsson K800i" },
{ regexp:/(Teleca Q7)/ },
{ regexp:/(MSIE) (\d+)\.(\d+)/, family_replacement:"Internet Explorer" }
],
operatingSystems = [
{ regexp: /(iPod|iPhone|iPad)* OS (\d+)_(\d+)(?:_(\d+))?/, family_replacement:"iOS" },
{ regexp: /(windows) nt 6\.0/i, family_replacement:"Windows", V1_replacement:"Vista" },
{ regexp: /(windows) nt 6\.\d+/i, family_replacement:"Windows", V1_replacement:"7" },
{ regexp: /(windows) nt 5\.2+/i, family_replacement:"Windows", V1_replacement:"2003"},
{ regexp: /(windows) nt 5\.1+/i, family_replacement:"Windows", V1_replacement:"XP"},
{ regexp: /(windows) nt 5\.0+/i, family_replacement:"Windows", V1_replacement:"2000"},
{ regexp: /(os x) (\d+)[._](\d+)(?:[._](\d+))?/i, family_replacement:"OS X"},
{ regexp: /(linux)/i, family_replacement:"Linux"}
];