-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserAgent.js
80 lines (71 loc) · 1.57 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
/**
* Detect browser and os from UA
*/
const os = {
"ipad" : "iPad",
"ipod" : "iPod",
"iphone" : "iPhone",
"windows nt 10" : "Windows 10",
"windows nt 6.1" : "Windows 7",
"windows nt 6.2" : "Windows 8",
"windows nt 6.3" : "Windows 8",
"windows phone 10" : "Windows Mobile",
"windows phone" : "Windows Phone",
"windows" : "Windows",
"android" : "Android",
"mac" : "Mac",
"ubuntu" : "Ubuntu",
"linux" : "Linux",
"bada" : "Bada",
"meego" : "MeeGo",
};
const browser = {
"crios" : "CriOS",
"fxios" : "FxiOS",
"wv" : "WebKit",
"edge" : "EDGE",
"chrome" : "Chrome",
"firefox" : "Firefox",
"msie" : "Internet Explorer",
"opera" : "Opera",
"trident" : "Trident",
"safari" : "Safari",
};
const mobile = [
'mobile', 'ipad', 'ipod', 'iphone'
];
class UserAgent {
static parseUserAgent(user_agent) {
return {
"os" : UserAgent.getOs(user_agent),
"browser" : UserAgent.getBrowser(user_agent),
"mobile" : UserAgent.isMobile(user_agent)
};
};
static getOs(user_agent) {
for(let key in os) {
if(user_agent.toLowerCase().indexOf(key) !== -1) {
return os[key];
}
}
return '';
};
static getBrowser(user_agent) {
for(let key in browser) {
if(user_agent.toLowerCase().indexOf(key) !== -1) {
let regexp = new RegExp(".*(" + browser[key] + ")\/([0-9\.]+).*", 'i');
return user_agent.replace(regexp, "$1/$2");
}
}
return '';
};
static isMobile(user_agent) {
for(let i = 0; i < mobile.length; i++) {
if(user_agent.toLowerCase().indexOf(mobile[i]) !== -1) {
return true;
}
}
return false;
};
};
exports.UserAgent = UserAgent;