-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser-check.js
139 lines (125 loc) · 4.42 KB
/
browser-check.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
const Browser = (function(window){
let document = window.document,
navigator = window.navigator,
agent = navigator.userAgent.toLowerCase(),
IEMode = document.documentMode,
chrome = window.chrome || false,
System = {
//user-agent
agent: agent,
//是否未IE
isIE: /msie/.test(agent),
//Gecko内核
isGecko: agent.indexOf("gecko")>0 && agent.indexOf("like gecko")<0,
//webkit内核
isWebkit: agent.indexOf("webkit")>0,
//是否支持subTitle
supportSubTitle:function(){
return "track" in document.createElement("track");
},
//是否支持style scoped
supportScope:function(){
return "scoped" in document.createElement("style");
},
//获取IE版本号
ieVersion: function(){
try{
return agent.match(/msie([\d.]+)/)[1] || 0;
}catch(e){
return IEMode;
}
},
//Opera版本号
operaVersion:function(){
try {
if(window.opera){
return agent.match(/opera.([\d.]+)/)[1];
}else if(agent.indexOf("opr")>0){
return agent.match(/opr\/[\d.]+)/)[1];
}
} catch (error) {
return 0;
}
},
//version过滤,如31.0.252.152只保留31.0
versionFilter:function(){
if(arguments.length===1 && typeof arguments[0] === "string"){
const version = arguments[0];
const start = version.indexOf(".");
if(start > 0){
const end = version.indexOf(".", start+1);
if(end !== -1){
return version.substr(0, end);
}
}
return version;
}else if(arguments.length === 1){
return arguments[0];
}
return 0;
}
};
try {
//浏览器类型(IE、Opera、Chrome、Safari、Firefox)
System.type = System.isIE ? "IE" :
window.opera || (agent.indexOf("opr") > 0) ? "Opera" :
(agent.indexOf("chrome")>0) ? "Chrome" :
//Safari
window.openDatabase ? "Safari" :
(agent.indexOf("firefox") > 0) ? "Firefox" : "unknown";
//版本号
System.version = (System.type==="IE") ? System.ieVersion() :
(System.type==="Firefox") ? agent.match(/firefox\/([\d.]+)/)[1] :
(System.type==="Chrome") ? agent.match(/chrome\/([\d.]+)/)[1] :
(System.type==="Opera") ? System.operaVersion() :
(System.type === "Safari") ? agent.match(/version\/([\d.]+)/)[1] :
"0";
//带壳浏览器,针对国内浏览器
System.shell = function(){
//遨游浏览器
if(agent.indexOf("maxthon") > 0){
System.version = agent.match(/maxthon\/([\d.]+)/)[1] || System.version;
return "傲游浏览器";
}
//QQ浏览器
if(agent.indexOf("qqbrowser") > 0){
System.version = agent.match(/qqbrowser\/([\d.]+)/)[1] || System.version;
return "QQ浏览器";
}
//搜狗浏览器
if(agent.indexOf("se 2.x") > 0){
return "搜狗浏览器";
}
if(chrome && System.type !== "Opera"){
let external = window.external,
clientInfo = window.clientInformation,
//客户端语言:zh-cn,zh.360下面返回undefined
clientLanguage = clientInfo.languages;
//猎豹浏览器
if(agent.indexOf("lbbrowser") > 0){
return "猎豹浏览器";
}
//百度浏览器
if(agent.indexOf("bidubrowser") > 0){
System.version = agent.match(/bidubrowser\/([\d.]+)/)[1] || agent.match(/chrome\/([\d.]+)/)[1];
}
//360极速浏览器和360安全浏览器
if(System.supportSubTitle() && typeof clientLanguage == "undefined"){
let storeKeyLen = Object.keys(chrome.webstore).length;
return storeKeyLen > 1 ? "360极速浏览器" : "360安全浏览器";
}
return "Chrome";
}
return System.type;
};
//浏览器名称(如果是带壳浏览器,返回壳名称)
System.name = System.shell();
//对版本号进行过滤处理
System.version = System.versionFilter(System.version);
} catch (error) {
}
return {
client: System
}
})(window);
module.exports = Browser;