-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
295 lines (270 loc) · 10 KB
/
index.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
'use strict'
var REGSTR_EDGE = /edge\/[\d.]+/gi;
var REGSTR_IE = /trident\/[\d.]+/gi;
var REGSTR_OLD_IE = /msie\s[\d.]+/gi;
var REGSTR_FF = /firefox\/[\d.]+/gi;
var REGSTR_CHROME = /chrome\/[\d.]+/gi;
var REGSTR_SAF = /safari\/[\d.]+/gi;
var REGSTR_OPERA = /opr\/[\d.]+/gi;
var REGSTR_OPERA_TOUCH = /opt\/[\d.]+/gi;
var REGSTR_WECHAR = /MicroMessenger\/[\d.]+/gi;
var REGSTR_VIVSLDI = /Vivaldi\/[\d.]+/gi;
var REGSTR_UC = /UBrowser\/[\d.]+/gi;
var REGSTR_MI = /MiuiBrowser\/[\d.]+/gi;
var REGSTR_SAMSUNG = /SamsungBrowser\/[\d.]+/gi;
var REGSTR_QQ = /QQBrowser\/[\d.]+/gi;
var REGSTR_FB = /FBAV\/[\d.]+/gi;
var REGSTR_APPLE_PHONE = /(iphone|ipod|ipad)/gi;
var REGSTR_APPLE_SYSTEM_VERSION = /OS [\d._]*/gi;
var REGSTR_NUM = /[^0-9|_.]/gi;
var REGSTR_UNDERLINR = /_/gi;
var REGSTR_ISMOBILE = /iphone|ios|android|mobile|ipad|symbos|iemobile|phone|bb10/i;
var spider_sign_arr = [
"baiduspider", "googlebot",
"360spider", "bingbot",
"bytespider", "applebot",
"msnbot", "sosospider",
"sosoimagespider", "baidu transcoder",
"sogou pic spider", "sogou web spider", "sogou-test-spider",
"youdaobot", "easouspider",
"huaweisymantecspider", "yisouspider",
"yahoo! slurp", "yahoo contentmatch crawler",
"twiceler", "qihoobot",
"naverBot", "surveybot",
"discobot", "yanga worldsearch bot",
"bot", "spider",
];
/**
* @description
* @param {*String} useragent
* @returns {*Json} { "Browser Name": "unknow", "Browser Version": "unknow" }
*/
function getBrowserInfo(useragent) {
let browserInfo = {
"Browser Name": "unknow",
"Browser Version": "unknow"
};
let agent = useragent.toString().toLowerCase();
// Spider
let isSpider = spider_sign_arr.some(function (item) {
if (agent.indexOf(item) > 0) {
browserInfo["Browser Name"] = "Spider " + item;
browserInfo["Browser Version"] = "";
return true;
}
}) || false;
if (isSpider) return browserInfo;
// IE
if (agent.indexOf('trident') > 0) {
browserInfo["Browser Name"] = "IE+";
browserInfo["Browser Version"] = agent.match(REGSTR_IE)[0].split('/')[1];
return browserInfo;
}
// OLD_IE
if (agent.indexOf('msie') > 0) {
browserInfo["Browser Name"] = "IE-";
browserInfo["Browser Version"] = agent.match(REGSTR_OLD_IE)[0].split(' ')[1];
return browserInfo;
}
// SamsungBrowser
if (agent.indexOf('samsungbrowser') > 0) {
browserInfo["Browser Name"] = "SamsungBrowser";
browserInfo["Browser Version"] = agent.match(REGSTR_SAMSUNG)[0].split('/')[1];
return browserInfo;
}
// FaceBook
if (agent.indexOf('fbav') > 0) {
browserInfo["Browser Name"] = "FaceBook";
browserInfo["Browser Version"] = agent.match(REGSTR_FB)[0].split('/')[1];
return browserInfo;
}
// QQBrowser
if (agent.indexOf('qqbrowser') > 0) {
browserInfo["Browser Name"] = "QQBrowser";
browserInfo["Browser Version"] = agent.match(REGSTR_QQ)[0].split('/')[1];
return browserInfo;
}
// MiuiBrowser
if (agent.indexOf('miuibrowser') > 0) {
browserInfo["Browser Name"] = "MiuiBrowser";
browserInfo["Browser Version"] = agent.match(REGSTR_MI)[0].split('/')[1];
return browserInfo;
}
// UBrowser
if (agent.indexOf('ubrowser') > 0) {
browserInfo["Browser Name"] = "UC";
browserInfo["Browser Version"] = agent.match(REGSTR_UC)[0].split('/')[1];
return browserInfo;
}
// Vivaldi
if (agent.indexOf('vivaldi') > 0) {
browserInfo["Browser Name"] = "Vivaldi";
browserInfo["Browser Version"] = agent.match(REGSTR_VIVSLDI)[0].split('/')[1];
return browserInfo;
}
// Edge
if (agent.indexOf('edge') > 0) {
browserInfo["Browser Name"] = "Edge";
browserInfo["Browser Version"] = agent.match(REGSTR_EDGE)[0].split('/')[1];
return browserInfo;
}
// Firefox
if (agent.indexOf('firefox') > 0) {
browserInfo["Browser Name"] = "Firefox";
browserInfo["Browser Version"] = agent.match(REGSTR_FF)[0].split('/')[1];
return browserInfo;
}
// Opera
if (agent.indexOf('opr') > 0) {
browserInfo["Browser Name"] = "Opera";
browserInfo["Browser Version"] = agent.match(REGSTR_OPERA)[0].split('/')[1];
return browserInfo;
}
// Opera Touch
if (agent.indexOf('opt') > 0) {
browserInfo["Browser Name"] = "Opera Touch";
browserInfo["Browser Version"] = agent.match(REGSTR_OPERA_TOUCH)[0].split('/')[1];
return browserInfo;
}
// WeChat
if (agent.indexOf('micromessenger') > 0) {
browserInfo["Browser Name"] = "WeChat";
browserInfo["Browser Version"] = agent.match(REGSTR_WECHAR)[0].split('/')[1];
return browserInfo;
}
// Safari
if (agent.indexOf('safari') > 0 && agent.indexOf('chrome') < 0) {
browserInfo["Browser Name"] = "Safari";
browserInfo["Browser Version"] = agent.match(REGSTR_SAF)[0].split('/')[1];
return browserInfo;
}
// Chrome
if (agent.indexOf('chrome') > 0) {
browserInfo["Browser Name"] = "Chrome";
browserInfo["Browser Version"] = agent.match(REGSTR_CHROME)[0].split('/')[1];
return browserInfo;
}
return browserInfo;
}
/**
* @description 获取客户端系统
* @param {*String} useragent
* @returns {*Json} {"System OS Name":"Unknown"}
*/
function getSystemOS(useragent) {
let systemOS = "Unknown";
let agent = useragent.toString().toLowerCase();
if (agent.indexOf('mac os x') > -1) {
let type = agent.match(REGSTR_APPLE_PHONE);
if (type) {
let name = (type[0]).toString();
systemOS = name.charAt(0).toUpperCase() + name.slice(1) + " " + getVersion(useragent, 'iphone');
} else {
systemOS = 'Mac OS ' + getVersion(useragent, 'mac os x');
}
} else if (agent.indexOf('symbos') > -1) {
systemOS = 'SymbOS ' + getVersion(useragent, 'symbos');
} else if (agent.indexOf('cros') > -1) {
systemOS = 'Chromium OS ' + getVersion(useragent, 'cros');
} else if (agent.indexOf('windows phone') > -1) {
systemOS = 'Windows Phone ' + getVersion(useragent, 'windows phone');
} else if (agent.indexOf('android') > -1) {
systemOS = 'Android ' + getVersion(useragent, 'android');
} else if (agent.indexOf('bb10') > -1) {
systemOS = 'BlackBerry ' + getVersion(useragent, 'bb10');
} else if (agent.indexOf('iemobile') > -1) {
systemOS = 'WinPhone ' + getVersion(useragent, 'iemobile');
} else if (agent.indexOf('ubuntu') > -1) {
systemOS = 'Ubuntu ' + getVersion(useragent, 'ubuntu');
} else if (agent.indexOf('debian') > -1) {
systemOS = 'Debian ' + getVersion(useragent, 'debian');
} else if (agent.indexOf('linux') > -1) {
systemOS = 'Linux ' + getVersion(useragent, 'linux');
} else if (agent.indexOf('windows nt 5.0') > -1) {
systemOS = 'Windows 2000'
} else if (agent.indexOf('windows nt 5.1') > -1 || agent.indexOf('windows nt 5.2') > -1) {
systemOS = 'Windows XP'
} else if (agent.indexOf('windows nt 6.0') > -1) {
systemOS = 'Windows Vista'
} else if (agent.indexOf('windows nt 6.1') > -1 || agent.indexOf('windows 7') > -1) {
systemOS = 'Windows 7'
} else if (agent.indexOf('windows nt 6.2') > -1 || agent.indexOf('windows 8') > -1) {
systemOS = 'Windows 8'
} else if (agent.indexOf('windows nt 6.3') > -1) {
systemOS = 'Windows 8.1'
} else if (agent.indexOf('windows nt 6.2') > -1 || agent.indexOf('windows nt 10.0') > -1) {
systemOS = 'Windows 10'
}
return { "System OS Name": systemOS };
}
function getVersion(useragent, type) {
let subNum = {
"linux": 8,
"android": 8,
"bb10": 5,
"iemobile": 9,
"cros": 5,
"ubuntu": 7,
"windows phone": 14,
"mac os x": 9,
}[type];
let agent = useragent.toString().toLowerCase();
if (type === "cros" || type === "mac os x") {
return agent.substr(agent.indexOf(type) + subNum, agent.indexOf(')', agent.indexOf(type)) - agent
.indexOf(type) - subNum).replace(REGSTR_UNDERLINR, '.');
}
if (type === "ubuntu") {
return agent.substr(agent.indexOf(type) + subNum, agent.indexOf(' ', agent.indexOf(type)) - agent
.indexOf(type) - subNum);
}
if (type === "iphone") {
return agent.match(REGSTR_APPLE_SYSTEM_VERSION).toString().replace(REGSTR_NUM, '').replace(
REGSTR_UNDERLINR, '.');
}
return agent.substr(agent.indexOf(type) + subNum, agent.indexOf(';', agent.indexOf(type)) - agent
.indexOf(type) - subNum);
}
/**
* @description 检查是不是爬虫
* @param {*String} useragent
* @returns {*Json} { "isSpider": false }
*/
function checkSpider(useragent) {
let agent = useragent.toString().toLowerCase();
let isSpider = spider_sign_arr.some(function (item) {
if (agent.indexOf(item) > 0) {
return true;
}
}) || false;
return { "isSpider": isSpider };
}
/**
* @description 检查客户端类型
* @param {*String} useragent
* @param {*Boolean} [upperCase=true]
* @returns {*Json} { "machineType": "Unknown" }
*/
function getMachineType(useragent, upperCase = true) {
let agent = useragent.toString().toLowerCase();
let machineType = upperCase ? "Unknown" : "unknown";
if (!REGSTR_ISMOBILE.test(agent)) {
machineType = upperCase ? "PC" : "pc";
} else {
machineType = upperCase ? "Mobile" : "mobile";
}
return { "machineType": machineType };
}
function getAllInfo(useragent) {
let systemOS = getSystemOS(useragent);
let browserInfo = getBrowserInfo(useragent);
let isSpider = checkSpider(useragent);
let machineType = getMachineType(useragent);
return Object.assign(systemOS, browserInfo, isSpider, machineType);
}
module.exports = {
getSystemOS: getSystemOS,
getBrowserInfo: getBrowserInfo,
checkSpider: checkSpider,
getMachineType: getMachineType,
getAllInfo: getAllInfo,
}