-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.js
executable file
·98 lines (80 loc) · 2.04 KB
/
environment.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
import browser from 'is-in-browser'
/**
* Browser matching rules.
*
* @type {Array}
*/
const BROWSER_RULES = [
['edge', /Edge\/([0-9\._]+)/],
['chrome', /(?!Chrom.*OPR)Chrom(?:e|ium)\/([0-9\.]+)(:?\s|$)/],
['firefox', /Firefox\/([0-9\.]+)(?:\s|$)/],
['opera', /Opera\/([0-9\.]+)(?:\s|$)/],
['opera', /OPR\/([0-9\.]+)(:?\s|$)$/],
['ie', /Trident\/7\.0.*rv\:([0-9\.]+)\).*Gecko$/],
['ie', /MSIE\s([0-9\.]+);.*Trident\/[4-7].0/],
['ie', /MSIE\s(7\.0)/],
['android', /Android\s([0-9\.]+)/],
['safari', /Version\/([0-9\._]+).*Safari/],
]
/**
* DOM event matching rules.
*
* @type {Array}
*/
const EVENT_RULES = [['beforeinput', el => 'onbeforeinput' in el]]
/**
* Operating system matching rules.
*
* @type {Array}
*/
const OS_RULES = [
['ios', /os ([\.\_\d]+) like mac os/i], // must be before the macos rule
['macos', /mac os x/i],
['android', /android/i],
['firefoxos', /mozilla\/[a-z\.\_\d]+ \((?:mobile)|(?:tablet)/i],
['windows', /windows\s*(?:nt)?\s*([\.\_\d]+)/i],
]
/**
* Define variables to store the result.
*/
let BROWSER
const EVENTS = {}
let OS
/**
* Run the matchers when in browser.
*/
if (browser) {
const { userAgent } = window.navigator
for (const [name, regexp] of BROWSER_RULES) {
if (regexp.test(userAgent)) {
BROWSER = name
break
}
}
for (const [name, regexp] of OS_RULES) {
if (regexp.test(userAgent)) {
OS = name
break
}
}
const testEl = window.document.createElement('div')
testEl.contentEditable = true
for (const [name, testFn] of EVENT_RULES) {
EVENTS[name] = testFn(testEl)
}
}
/**
* Export.
*
* @type {Object}
*/
export const IS_CHROME = BROWSER === 'chrome'
export const IS_FIREFOX = BROWSER === 'firefox'
export const IS_SAFARI = BROWSER === 'safari'
export const IS_IE = BROWSER === 'ie'
export const IS_EDGE = BROWSER === 'edge'
export const IS_ANDROID = OS === 'android'
export const IS_IOS = OS === 'ios'
export const IS_MAC = OS === 'macos'
export const IS_WINDOWS = OS === 'windows'
export const SUPPORTED_EVENTS = EVENTS