-
Notifications
You must be signed in to change notification settings - Fork 77
/
browser.ts
168 lines (147 loc) · 5.06 KB
/
browser.ts
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
import { BotdError, BrowserEngineKind, BrowserKind, State } from '../types'
import { countTruthy } from './misc'
import { strIncludes } from './ponyfills'
export function getBrowserEngineKind(): BrowserEngineKind {
// Based on research in October 2020. Tested to detect Chromium 42-86.
const w = window
const n = navigator
if (
countTruthy([
'webkitPersistentStorage' in n,
'webkitTemporaryStorage' in n,
n.vendor.indexOf('Google') === 0,
'webkitResolveLocalFileSystemURL' in w,
'BatteryManager' in w,
'webkitMediaStream' in w,
'webkitSpeechGrammar' in w,
]) >= 5
) {
return BrowserEngineKind.Chromium
}
if (
countTruthy([
'ApplePayError' in w,
'CSSPrimitiveValue' in w,
'Counter' in w,
n.vendor.indexOf('Apple') === 0,
'getStorageUpdates' in n,
'WebKitMediaKeys' in w,
]) >= 4
) {
return BrowserEngineKind.Webkit
}
if (
countTruthy([
'buildID' in navigator,
'MozAppearance' in (document.documentElement?.style ?? {}),
'onmozfullscreenchange' in w,
'mozInnerScreenX' in w,
'CSSMozDocumentRule' in w,
'CanvasCaptureMediaStream' in w,
]) >= 4
) {
return BrowserEngineKind.Gecko
}
return BrowserEngineKind.Unknown
}
export function getBrowserKind(): BrowserKind {
const userAgent = navigator.userAgent?.toLowerCase()
if (strIncludes(userAgent, 'edg/')) {
return BrowserKind.Edge
} else if (strIncludes(userAgent, 'safari')) {
return BrowserKind.Safari
} else if (strIncludes(userAgent, 'trident') || strIncludes(userAgent, 'msie')) {
return BrowserKind.IE
} else if (strIncludes(userAgent, 'wechat')) {
return BrowserKind.WeChat
} else if (strIncludes(userAgent, 'firefox')) {
return BrowserKind.Firefox
} else if (strIncludes(userAgent, 'opera') || strIncludes(userAgent, 'opr')) {
return BrowserKind.Opera
} else if (strIncludes(userAgent, 'chrome')) {
return BrowserKind.Chrome
} else {
return BrowserKind.Unknown
}
}
// Source: https://github.com/fingerprintjs/fingerprintjs/blob/master/src/utils/browser.ts#L223
export function isAndroid(): boolean {
const browserEngineKind = getBrowserEngineKind()
const isItChromium = browserEngineKind === BrowserEngineKind.Chromium
const isItGecko = browserEngineKind === BrowserEngineKind.Gecko
// Only 2 browser engines are presented on Android.
// Actually, there is also Android 4.1 browser, but it's not worth detecting it at the moment.
if (!isItChromium && !isItGecko) return false
const w = window
// Chrome removes all words "Android" from `navigator` when desktop version is requested
// Firefox keeps "Android" in `navigator.appVersion` when desktop version is requested
return (
countTruthy([
'onorientationchange' in w,
'orientation' in w,
isItChromium && !('SharedWorker' in w),
isItGecko && /android/i.test(navigator.appVersion),
]) >= 2
)
}
// Source: https://github.com/fingerprintjs/fingerprintjs/blob/109f8ef802169df3fa1c5d1baa4b7bc0abbc1d91/src/utils/browser.ts#L102C1-L118C2
export function isDesktopWebKit(): boolean {
// Checked in Safari and DuckDuckGo
const w = window
const { HTMLElement, Document } = w
return (
countTruthy([
'safari' in w, // Always false in Karma and BrowserStack Automate
!('ongestureend' in w),
!('TouchEvent' in w),
!('orientation' in w),
HTMLElement && !('autocapitalize' in HTMLElement.prototype),
Document && 'pointerLockElement' in Document.prototype,
]) >= 4
)
}
export function getMozAppearanceSupport(): boolean {
if (window.CSS === undefined) {
throw new BotdError(State.Undefined, 'window.CSS is undefined')
}
return CSS.supports('-moz-appearance', 'auto')
}
export function getDocumentFocus(): boolean {
if (document.hasFocus === undefined) {
return false
}
return document.hasFocus()
}
export function isChromium86OrNewer(): boolean {
// Checked in Chrome 85 vs Chrome 86 both on desktop and Android
const w = window
return (
countTruthy([
!('MediaSettingsRange' in w),
'RTCEncodedAudioFrame' in w,
'' + w.Intl === '[object Intl]',
'' + w.Reflect === '[object Reflect]',
]) >= 3
)
}
export function isIPad(): boolean {
// Checked on:
// Safari on iPadOS (both mobile and desktop modes): 8, 11, 12, 13, 14
// Chrome on iPadOS (both mobile and desktop modes): 11, 12, 13, 14
// Safari on iOS (both mobile and desktop modes): 9, 10, 11, 12, 13, 14
// Chrome on iOS (both mobile and desktop modes): 9, 10, 11, 12, 13, 14
// Before iOS 13. Safari tampers the value in "request desktop site" mode since iOS 13.
if (navigator.platform === 'iPad') {
return true
}
const s = screen
const screenRatio = s.width / s.height
return (
countTruthy([
'MediaSource' in window, // Since iOS 13
!!Element.prototype.webkitRequestFullscreen, // Since iOS 12
// iPhone 4S that runs iOS 9 matches this. But it won't match the criteria above, so it won't be detected as iPad.
screenRatio > 0.65 && screenRatio < 1.53,
]) >= 2
)
}