-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
detect.js
113 lines (95 loc) · 3.14 KB
/
detect.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
let _detected;
export function utilDetect(refresh) {
if (_detected && !refresh) return _detected;
_detected = {};
const ua = navigator.userAgent;
let m = null;
/* Browser */
m = ua.match(/(edge)\/?\s*(\.?\d+(\.\d+)*)/i); // Edge
if (m !== null) {
_detected.browser = m[1];
_detected.version = m[2];
}
if (!_detected.browser) {
m = ua.match(/Trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/i); // IE11
if (m !== null) {
_detected.browser = 'msie';
_detected.version = m[1];
}
}
if (!_detected.browser) {
m = ua.match(/(opr)\/?\s*(\.?\d+(\.\d+)*)/i); // Opera 15+
if (m !== null) {
_detected.browser = 'Opera';
_detected.version = m[2];
}
}
if (!_detected.browser) {
m = ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if (m !== null) {
_detected.browser = m[1];
_detected.version = m[2];
m = ua.match(/version\/([\.\d]+)/i);
if (m !== null) _detected.version = m[1];
}
}
if (!_detected.browser) {
_detected.browser = navigator.appName;
_detected.version = navigator.appVersion;
}
// keep major.minor version only..
_detected.version = _detected.version.split(/\W/).slice(0,2).join('.');
// detect other browser capabilities
// Legacy Opera has incomplete svg style support. See #715
_detected.opera = (_detected.browser.toLowerCase() === 'opera' && Number(_detected.version) < 15 );
if (_detected.browser.toLowerCase() === 'msie') {
_detected.ie = true;
_detected.browser = 'Internet Explorer';
_detected.support = false;
} else {
_detected.ie = false;
_detected.support = true;
}
_detected.filedrop = (window.FileReader && 'ondrop' in window);
/* Platform */
if (/Win/.test(ua)) {
_detected.os = 'win';
_detected.platform = 'Windows';
} else if (/Mac/.test(ua)) {
_detected.os = 'mac';
_detected.platform = 'Macintosh';
} else if (/X11/.test(ua) || /Linux/.test(ua)) {
_detected.os = 'linux';
_detected.platform = 'Linux';
} else {
_detected.os = 'win';
_detected.platform = 'Unknown';
}
_detected.isMobileWebKit = (/\b(iPad|iPhone|iPod)\b/.test(ua) ||
// HACK: iPadOS 13+ requests desktop sites by default by using a Mac user agent,
// so assume any "mac" with multitouch is actually iOS
(navigator.platform === 'MacIntel' && 'maxTouchPoints' in navigator && navigator.maxTouchPoints > 1)) &&
/WebKit/.test(ua) &&
!/Edge/.test(ua) &&
!window.MSStream;
/* Locale */
// An array of locales requested by the browser in priority order.
_detected.browserLocales = Array.from(new Set( // remove duplicates
[navigator.language]
.concat(navigator.languages || [])
.concat([
// old property for backwards compatibility
navigator.userLanguage
])
// remove any undefined values
.filter(Boolean)
));
/* Host */
const loc = window.top.location;
let origin = loc.origin;
if (!origin) { // for unpatched IE11
origin = loc.protocol + '//' + loc.hostname + (loc.port ? ':' + loc.port: '');
}
_detected.host = origin + loc.pathname;
return _detected;
}