-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
44 lines (41 loc) · 1.44 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
var browserslist = require('browserslist');
var parser = require('ua-parser-js');
function getBrowserName(agent) {
if (agent.browser.name === 'Android Browser') {
return 'android';
} else if (agent.os.name === 'BlackBerry') {
return 'bb';
} else if (agent.browser.name === 'Chrome' && agent.os.name === 'Android') {
return 'and_chr';
} else if (agent.browser.name === 'Firefox' && agent.os.name === 'Android') {
return 'and_ff';
} else if (agent.browser.name === 'IEMobile') {
return 'ie_mob';
} else if (agent.browser.name === 'Opera Mobi') {
return 'op_mob';
} else if (agent.browser.name === 'Safari' && agent.os.name === 'iOS') {
return 'ios_saf';
} else if (agent.browser.name === 'UCBrowser') {
return 'and_uc';
}
return agent.browser.name;
}
function getBrowserVersionFromUserAgent(userAgent) {
var agent = parser(userAgent);
var version = (agent.browser.version || agent.os.version || '').split('.');
var browserName = getBrowserName(agent);
while (version.length > 0) {
try {
return browserslist(browserName + ' ' + version.join('.'))[0];
} catch (e) {
// Ignore unknown browser query error
}
version.pop();
}
return 'unknown';
}
module.exports = function isBrowserSupported(userAgent, selections) {
var browsersSupported = browserslist(selections);
var browser = getBrowserVersionFromUserAgent(userAgent);
return browsersSupported.indexOf(browser) !== -1;
};