-
Notifications
You must be signed in to change notification settings - Fork 0
/
useragent.js
108 lines (100 loc) · 2.82 KB
/
useragent.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
// Based on fantastic jQuery useragent parser plugin https://gist.github.com/373298
function parse(uaStr) {
var agent = {
platform: {},
browser: {},
engine: {}
};
var ua = uaStr,
p = agent.platform,
b = agent.browser,
e = agent.engine;
// detect platform
if (/Windows/.test(ua)) {
p.name = 'win';
p.win = true;
} else if (/iPhone|iPod/.test(ua)) {
p.name = 'iphone';
p.iphone = true;
} else if (/iPad/.test(ua)) {
p.name = 'ipad';
p.ipad = true;
} else if (/Mac/.test(ua)) {
p.name = 'mac';
p.mac = true;
} else if (/Linux/.test(ua)) {
p.name = 'linux';
p.linux = true;
} else if (/Android/.test(ua)) {
p.name = 'android';
p.android = true;
} else {
p.name = 'other';
p.unknown = true;
}
// detect browser
if (/MSIE/.test(ua)) {
b.name = 'msie';
b.msie = true;
} else if (/Firefox/.test(ua)) {
b.name = 'firefox';
b.firefox = true;
} else if (/Chrome/.test(ua)) { // must be tested before Safari
b.name = 'chrome';
b.chrome = true;
} else if (/Safari/.test(ua)) {
b.name = 'safari';
b.safari = true;
} else if (/Opera/.test(ua)) {
b.name = 'opera';
b.opera = true;
} else {
b.name = 'other';
b.unknown = true;
}
// detect browser version
if (b.msie) {
b.version = /MSIE (\d+(\.\d+)*)/.exec(ua)[1];
} else if (b.firefox) {
b.version = /Firefox\/(\d+(\.\d+)*)/.exec(ua)[1];
} else if (b.chrome) {
b.version = /Chrome\/(\d+(\.\d+)*)/.exec(ua)[1];
} else if (b.safari) {
b.version = /Version\/(\d+(\.\d+)*)/.exec(ua)[1];
} else if (b.opera) {
b.version = /Version\/(\d+(\.\d+)*)/.exec(ua)[1];
} else {
b.version = 0;
}
// detect engine
if (/Trident/.test(ua) || b.msie) {
e.name = 'trident';
e.trident = true;
} else if (/WebKit/.test(ua)) { // must be tested before Gecko
e.name = 'webkit';
e.webkit = true;
} else if (/Gecko/.test(ua)) {
e.name = 'gecko';
e.gecko = true;
} else if (/Presto/.test(ua)) {
e.name = 'presto';
e.presto = true;
} else {
e.name = 'other';
e.unknown = true;
}
// detect engine version
if (e.trident) {
e.version = /Trident/.test(ua)? /Trident\/(\d+(\.\d+)*)/.exec(ua)[1]: 0;
} else if (e.gecko) {
e.version = /rv:(\d+(\.\d+)*)/.exec(ua)[1];
} else if (e.webkit) {
e.version = /WebKit\/(\d+(\.\d+)*)/.exec(ua)[1];
} else if (e.presto) {
e.version = /Presto\/(\d+(\.\d+)*)/.exec(ua)[1];
} else {
e.version = 0;
}
return agent;
};
exports.parse = parse;