-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile-useragent-parse.min.js
More file actions
236 lines (216 loc) · 9.36 KB
/
mobile-useragent-parse.min.js
File metadata and controls
236 lines (216 loc) · 9.36 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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global['mobile-useragent-parse'] = factory());
}(this, (function () { 'use strict';
/**
* 规则
*/
// 设备检测规则
var DEVICES = [['ipad', 'ipad'],
// ipod要放在iphone 之前。
['ipod', 'ipod'], ['iphone', /\biphone\b|\biph(\d)/], ['xiaomi', /(?:mi |redmi |hm )(?:note )?(?:([a-z0-9]+))?/], ['huawei', /(?:huawei|honor)(?: ([_\-a-z0-9]+))?/], ['samsung', /(?:samsung|sm-|gt-)([0-9a-z]+)/], ['oppo', /\boppo(?: ([a-z0-9]+))?/], ['vivo', /\bvivo(?: ([a-z0-9]+))?/], ['zte', /\bzte(?: ([a-z0-9]+))?/], ['htc', /\bhtc(?: ([a-z0-9]+))?/], ['meizu', /(\bm([0-9cx]{1,4})\b)|(\bmeizu[\/ ]([a-z0-9]+)\b)/], ['lenovo', /\b(?:lenovo|zuk)(?: ([a-z0-9]+))?/], ['coolpad', /\bcoolpad[_ ]?([a-z0-9]+)/], ['lg', /\blg([a-z0-9]+)/], ['chuizi', /\bsm([0-9]+)/], ['jianguo', /\bod([0-9]+)/],
// 兜底儿
['android', /\bandroid\b|\badr\b/]];
var OS = [['ios', function (ua) {
if (/\bcpu(?: iphone)? os /.test(ua)) {
return (/\bcpu(?: iphone)? os ([0-9._]+)/
);
} else if (ua.indexOf('iph os ') !== -1) {
return (/\biph os ([0-9_]+)/
);
} else {
return (/\bios\b/
);
}
}], ['android', function (ua) {
if (ua.indexOf('android') >= 0) {
return (/\bandroid[ \/-]?([0-9.x]+)?/
);
} else if (ua.indexOf('adr') >= 0) {
if (ua.indexOf('mqqbrowser') >= 0) {
return (/\badr[ ]\(linux; u; ([0-9.]+)?/
);
} else {
return (/\badr(?:[ ]([0-9.]+))?/
);
}
}
return 'android';
}]];
var ENGINE = [['edgehtml', /edge\/([0-9.]+)/], ['trident', /\b(?:msie |ie |trident\/[0-9].*rv[ :])([0-9.]+)/], ['blink', function () {
return 'chrome' in window && 'CSS' in window && /\bapplewebkit[\/]?([0-9.+]+)/;
}], ['webkit', /\bapplewebkit[\/]?([0-9.+]+)/], ['gecko', function (ua) {
var match = ua.match(/\brv:([\d\w.]+).*\bgecko\/(\d+)/);
if (match) {
return {
version: match[1] + '.' + match[2]
};
}
}], ['presto', /\bpresto\/([0-9.]+)/], ['androidwebkit', /\bandroidwebkit\/([0-9.]+)/], ['coolpadwebkit', /\bcoolpadwebkit\/([0-9.]+)/], ['u2', /\bu2\/([0-9.]+)/], ['u3', /\bu3\/([0-9.]+)/]];
var BROWSER = [['shoubai', /\bbaiduboxapp([0-9.]+)?/], ['weixin', /\bmicromessenger\/([\d.]+)/], ['qq', /\bm?qqbrowser\/([0-9.]+)/], ['baidu', /\b(?:ba?idubrowser|baiduhd)[ \/]([0-9.x]+)/], ['mi', /\bmiuibrowser\/([0-9.]+)/], ['opera', function (ua) {
var re_opera_old = /\bopera.+version\/([0-9.ab]+)/;
var re_opera_new = /\bopr\/([0-9.]+)/;
return re_opera_old.test(ua) ? re_opera_old : re_opera_new;
}], ['oupeng', /\boupeng\/([0-9.]+)/],
// UC放在Android前。
['uc', function (ua) {
if (ua.indexOf('ucbrowser/') >= 0) {
return (/\bucbrowser\/([0-9.]+)/
);
} else if (ua.indexOf('ubrowser/') >= 0) {
return (/\bubrowser\/([0-9.]+)/
);
} else if (/\buc\/[0-9]/.test(ua)) {
return (/\buc\/([0-9.]+)/
);
} else if (ua.indexOf('ucweb') >= 0) {
return (/\bucweb([0-9.]+)?/
);
} else {
return (/\b(?:ucbrowser|uc)\b/
);
}
}], ['oppobrowser', /\boppobrowser\/([0-9.]+)/], ['chrome', / (?:chrome|crios|crmo)\/([0-9.]+)/],
// Android需要在safari之前。
['android', function (ua) {
if (ua.indexOf('android') === -1) {
return;
}
return (/\bversion\/([0-9.]+(?: beta)?)/
);
}], ['safari', /\bversion\/([0-9.]+(?: beta)?)(?: mobile(?:\/[a-z0-9]+)?)? safari\//],
// 如果不能被识别为 Safari,则猜测是 WebView。
['webview', /\bcpu(?: iphone)? os (?:[0-9._]+).+\bapplewebkit\b/], ['firefox', /\bfirefox\/([0-9.ab]+)/], ['nokia', /\bnokiabrowser\/([0-9.]+)/]];
var Rule = {
device: DEVICES,
os: OS,
browser: BROWSER,
engine: ENGINE
};
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* 探测器
*/
var Detector = function () {
function Detector(rules) {
_classCallCheck(this, Detector);
this._rules = rules;
}
_createClass(Detector, [{
key: 'detect',
value: function detect(name, expression, ua) {
var expr = typeof expression === 'function' ? expression(ua) : expression;
var info = {
name: '',
version: ''
};
if (!expr) {
return info;
}
if (Object.prototype.toString.call(expr) === '[object RegExp]') {
var m = expr.exec(ua);
if (m) {
info.name = name;
if (m.length >= 2 && m[1]) {
info.version = m[1].replace(/_/g, ".");
}
return info;
}
}
if (Object.prototype.toString.call(expr) === '[object Object]') {
if (expr.hasOwnProperty("version")) {
info.version = expr.version;
info.name = name;
}
return info;
}
if (Object.prototype.toString.call(expr) === '[object String]') {
if (ua.indexOf(expr) > -1) {
info.name = name;
}
return info;
}
}
}, {
key: 'getDevice',
value: function getDevice(ua, patternArray) {
for (var index = 0; index < patternArray.length; index++) {
var deviceInfo = this.detect(patternArray[index][0], patternArray[index][1], ua);
if (deviceInfo && deviceInfo.name) {
return deviceInfo;
}
}
return {
name: '',
version: ''
};
}
}, {
key: 'getOs',
value: function getOs(ua, patternArray) {
for (var index = 0; index < patternArray.length; index++) {
var osInfo = this.detect(patternArray[index][0], patternArray[index][1], ua);
if (osInfo && osInfo.name) {
return osInfo;
}
}
return {
name: '',
version: ''
};
}
}, {
key: 'getEngine',
value: function getEngine(ua, patternArray) {
for (var index = 0; index < patternArray.length; index++) {
var engineInfo = this.detect(patternArray[index][0], patternArray[index][1], ua);
if (engineInfo && engineInfo.name) {
return engineInfo;
}
}
return {
name: '',
version: ''
};
}
}, {
key: 'getBrowser',
value: function getBrowser(ua, patternArray) {
for (var index = 0; index < patternArray.length; index++) {
var browserInfo = this.detect(patternArray[index][0], patternArray[index][1], ua);
if (browserInfo && browserInfo.name) {
return browserInfo;
}
}
return {
name: '',
version: ''
};
}
}, {
key: 'parse',
value: function parse(ua) {
ua = (ua || '').toLowerCase();
var d = {};
d.device = this.getDevice(ua, this._rules.device);
d.os = this.getOs(ua, this._rules.os);
d.engine = this.getEngine(ua, this._rules.engine);
d.browser = this.getBrowser(ua, this._rules.browser);
return d;
}
}]);
return Detector;
}();
/**
* 探测器
*/
var userAgent = navigator.userAgent || '';
var appVersion = navigator.appVersion || '';
var vendor = navigator.vendor || '';
var ua = userAgent + ' ' + appVersion + ' ' + vendor;
var detector = new Detector(Rule);
var d = detector.parse(ua);
return d;
})));