browser.js
3.06 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
/**
* @fileoverview This module detects the kind of well-known browser and version.
* @author NHN.
* FE Development Lab <dl_javascript@nhn.com>
*/
'use strict';
/**
* This object has an information that indicate the kind of browser.<br>
* The list below is a detectable browser list.
* - ie8 ~ ie11
* - chrome
* - firefox
* - safari
* - edge
* @memberof tui.util
* @example
* //-- #1. Get Module --//
* var util = require('tui-code-snippet'); // node, commonjs
* var util = tui.util; // distribution file
*
* //-- #2. Use property --//
* util.browser.chrome === true; // chrome
* util.browser.firefox === true; // firefox
* util.browser.safari === true; // safari
* util.browser.msie === true; // IE
* util.browser.edge === true; // edge
* util.browser.others === true; // other browser
* util.browser.version; // browser version
*/
var browser = {
chrome: false,
firefox: false,
safari: false,
msie: false,
edge: false,
others: false,
version: 0
};
if (window && window.navigator) {
detectBrowser();
}
/**
* Detect the browser.
* @private
*/
function detectBrowser() {
var nav = window.navigator;
var appName = nav.appName.replace(/\s/g, '_');
var userAgent = nav.userAgent;
var rIE = /MSIE\s([0-9]+[.0-9]*)/;
var rIE11 = /Trident.*rv:11\./;
var rEdge = /Edge\/(\d+)\./;
var versionRegex = {
firefox: /Firefox\/(\d+)\./,
chrome: /Chrome\/(\d+)\./,
safari: /Version\/([\d.]+).*Safari\/(\d+)/
};
var key, tmp;
var detector = {
Microsoft_Internet_Explorer: function() { // eslint-disable-line camelcase
var detectedVersion = userAgent.match(rIE);
if (detectedVersion) { // ie8 ~ ie10
browser.msie = true;
browser.version = parseFloat(detectedVersion[1]);
} else { // no version information
browser.others = true;
}
},
Netscape: function() { // eslint-disable-line complexity
var detected = false;
if (rIE11.exec(userAgent)) {
browser.msie = true;
browser.version = 11;
detected = true;
} else if (rEdge.exec(userAgent)) {
browser.edge = true;
browser.version = userAgent.match(rEdge)[1];
detected = true;
} else {
for (key in versionRegex) {
if (versionRegex.hasOwnProperty(key)) {
tmp = userAgent.match(versionRegex[key]);
if (tmp && tmp.length > 1) { // eslint-disable-line max-depth
browser[key] = detected = true;
browser.version = parseFloat(tmp[1] || 0);
break;
}
}
}
}
if (!detected) {
browser.others = true;
}
}
};
var fn = detector[appName];
if (fn) {
detector[appName]();
}
}
module.exports = browser;