-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathbrowser.ts
179 lines (159 loc) · 5.67 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
169
170
171
172
173
174
175
176
177
178
179
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, '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 if (strIncludes(userAgent, 'safari')) {
return BrowserKind.Safari
} 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
const w = window
const n = navigator
const c = 'connection'
// Chrome removes all words "Android" from `navigator` when desktop version is requested
// Firefox keeps "Android" in `navigator.appVersion` when desktop version is requested
if (isItChromium) {
return (
countTruthy([
!('SharedWorker' in w),
// `typechange` is deprecated, but it's still present on Android (tested on Chrome Mobile 117)
// Removal proposal https://bugs.chromium.org/p/chromium/issues/detail?id=699892
// Note: this expression returns true on ChromeOS, so additional detectors are required to avoid false-positives
n[c] && 'ontypechange' in n[c],
!('sinkId' in new Audio()),
]) >= 2
)
} else if (isItGecko) {
return countTruthy(['onorientationchange' in w, 'orientation' in w, /android/i.test(n.appVersion)]) >= 2
} else {
// 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.
return false
}
}
// 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. Checked in macOS Chrome 128, Android Chrome 127.
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-18
// Chrome on iPadOS (both mobile and desktop modes): 11-18
// Safari on iOS (both mobile and desktop modes): 9-18
// Chrome on iOS (both mobile and desktop modes): 9-18
// 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([
// Since iOS 13. Doesn't work in Chrome on iPadOS <15, but works in desktop mode.
'MediaSource' in window,
// Since iOS 12. Doesn't work in Chrome on iPadOS.
!!Element.prototype.webkitRequestFullscreen,
// iPhone 4S that runs iOS 9 matches this, but it is not supported
// Doesn't work in incognito mode of Safari ≥17 with split screen because of tracking prevention
screenRatio > 0.65 && screenRatio < 1.53,
]) >= 2
)
}