forked from fingerprintjs/fingerprintjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.ts
More file actions
426 lines (385 loc) · 14.3 KB
/
Copy pathbrowser.ts
File metadata and controls
426 lines (385 loc) · 14.3 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import { countTruthy } from './data'
import { isFunctionNative } from './misc'
/*
* Functions to help with features that vary through browsers
*/
/**
* Checks whether the browser is based on Trident (the Internet Explorer engine) without using user-agent.
*
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
export function isTrident(): boolean {
const w = window
const n = navigator
// The properties are checked to be in IE 10, IE 11 and not to be in other browsers in October 2020
return (
countTruthy([
'MSCSSMatrix' in w,
'msSetImmediate' in w,
'msIndexedDB' in w,
'msMaxTouchPoints' in n,
'msPointerEnabled' in n,
]) >= 4
)
}
/**
* Checks whether the browser is based on EdgeHTML (the pre-Chromium Edge engine) without using user-agent.
*
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
export function isEdgeHTML(): boolean {
// Based on research in October 2020
const w = window
const n = navigator
return (
countTruthy(['msWriteProfilerMark' in w, 'MSStream' in w, 'msLaunchUri' in n, 'msSaveBlob' in n]) >= 3 &&
!isTrident()
)
}
/**
* Checks whether the browser is based on Chromium without using user-agent.
*
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
export function isChromium(): boolean {
// Based on research in October 2020. Tested to detect Chromium 42-86.
const w = window
const n = navigator
return (
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
)
}
/**
* Checks whether the browser is based on mobile or desktop Safari without using user-agent.
* All iOS browsers use WebKit (the Safari engine).
*
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
export function isWebKit(): boolean {
// Based on research in August 2024
const w = window
const n = navigator
return (
countTruthy([
'ApplePayError' in w,
'CSSPrimitiveValue' in w,
'Counter' in w,
n.vendor.indexOf('Apple') === 0,
'RGBColor' in w,
'WebKitMediaKeys' in w,
]) >= 4
)
}
/**
* Checks whether this WebKit browser is a desktop browser.
* It doesn't check that the browser is based on WebKit, there is a separate function for this.
*
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
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
)
}
/**
* Checks whether this WebKit browser is Safari.
* It doesn't check that the browser is based on WebKit, there is a separate function for this.
*
* Warning! The function works properly only for Safari version 15.4 and newer.
*/
export function isSafariWebKit(): boolean {
// Checked in Safari, Chrome, Firefox, Yandex, UC Browser, Opera, Edge and DuckDuckGo.
// iOS Safari and Chrome were checked on iOS 11-18. DuckDuckGo was checked on iOS 17-18 and macOS 14-15.
// Desktop Safari versions 12-18 were checked.
// The other browsers were checked on iOS 17 and 18; there was no chance to check them on the other OS versions.
const w = window
return (
// Filters-out Chrome, Yandex, DuckDuckGo (macOS and iOS), Edge
isFunctionNative(w.print) &&
// Doesn't work in Safari < 15.4
String((w as unknown as Record<string, unknown>).browser) === '[object WebPageNamespace]'
)
}
/**
* Checks whether the browser is based on Gecko (Firefox engine) without using user-agent.
*
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
export function isGecko(): boolean {
const w = window
// Based on research in September 2020
return (
countTruthy([
'buildID' in navigator,
'MozAppearance' in (document.documentElement?.style ?? {}),
'onmozfullscreenchange' in w,
'mozInnerScreenX' in w,
'CSSMozDocumentRule' in w,
'CanvasCaptureMediaStream' in w,
]) >= 4
)
}
/**
* Checks whether the browser is based on Gecko version ≥120 (Firefox ≥120) without using user-agent.
* It doesn't check that the browser is based on Gecko; there is a separate function for this.
*
* @see https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/120 Firefox 120 release notes
*/
export function isGecko120OrNewer(): boolean {
// Checked in Firefox 119 vs. Firefox 120+
const w = window
const n = navigator
const { CSS } = w
// We use a threshold of 3 out of 4 because `globalPrivacyControl` was added in Firefox 120 on desktop,
// but only in Firefox 122 on mobile. Using >= 3 ensures detection works on both platforms for Firefox 120+.
// @see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/globalPrivacyControl#browser_compatibility
return (
countTruthy([
// User Activation API - added in Firefox 120
'userActivation' in n,
// CSS light-dark() function - added in Firefox 120
CSS.supports('color', 'light-dark(#000, #fff)'),
// CSS lh unit - added in Firefox 120
CSS.supports('height', '1lh'),
// Global Privacy Control - added in Firefox 120 (desktop) / Firefox 122 (mobile)
'globalPrivacyControl' in n,
]) >= 3
)
}
/**
* Checks whether the browser is based on Gecko version ≥143 (Firefox ≥143) without using user-agent.
* It doesn't check that the browser is based on Gecko; there is a separate function for this.
*
* Firefox 143 shipped Phase 2 fingerprinting protections (screen resolution, processor count, touch points)
* in Private Browsing and ETP Strict mode.
*
* @see https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/143 Firefox 143 release notes
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=1978414 Bug that shipped these protections
*/
export function isGecko143OrNewer(): boolean {
// Checked in Firefox 142 vs. Firefox 143+
const { CSS } = window
return (
countTruthy([
// CSS ::details-content pseudo-element - added in Firefox 143
CSS.supports('selector(::details-content)'),
// CSS ::before::marker nested pseudo-element - added in Firefox 143
CSS.supports('selector(::before::marker)'),
// CSS ::after::marker nested pseudo-element - added in Firefox 143
CSS.supports('selector(::after::marker)'),
// CompositionEvent.locale property - removed in Firefox 143 (Bug 1700969)
!('locale' in CompositionEvent.prototype),
]) >= 3
)
}
/**
* Checks whether the browser is based on Chromium version ≥86 without using user-agent.
* It doesn't check that the browser is based on Chromium, there is a separate function for this.
*/
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
)
}
/**
* Checks whether the browser is based on Chromium version ≥122 without using user-agent.
* It doesn't check that the browser is based on Chromium, there is a separate function for this.
*/
export function isChromium122OrNewer(): boolean {
// Checked in Chrome 121 vs Chrome 122 and 129 both on desktop and Android
const w = window
const { URLPattern } = w
return (
countTruthy([
'union' in Set.prototype,
'Iterator' in w,
URLPattern && 'hasRegExpGroups' in URLPattern.prototype,
'RGB8' in WebGLRenderingContext.prototype,
]) >= 3
)
}
/**
* Checks whether the browser is based on Chromium version ≥128 without using user-agent.
* It doesn't check that the browser is based on Chromium, there is a separate function for this.
*/
export function isChromium128OrNewer(): boolean {
// Checked in Chrome 127 vs Chrome 128+
const w = window
const d = document
const { CSS, Promise, AudioContext } = w
return (
countTruthy([
Promise && 'try' in Promise,
'caretPositionFromPoint' in d,
AudioContext && 'onerror' in AudioContext.prototype,
CSS.supports('ruby-align', 'space-around'),
]) >= 3
)
}
/**
* Checks whether the browser is based on WebKit version ≥606 (Safari ≥12) without using user-agent.
* It doesn't check that the browser is based on WebKit, there is a separate function for this.
*
* @see https://en.wikipedia.org/wiki/Safari_version_history#Release_history Safari-WebKit versions map
*/
export function isWebKit606OrNewer(): boolean {
// Checked in Safari 9–18
const w = window
return (
countTruthy([
'DOMRectList' in w,
'RTCPeerConnectionIceEvent' in w,
'SVGGeometryElement' in w,
'ontransitioncancel' in w,
]) >= 3
)
}
/**
* Checks whether the browser is based on WebKit version ≥616 (Safari ≥17) without using user-agent.
* It doesn't check that the browser is based on WebKit, there is a separate function for this.
*
* @see https://developer.apple.com/documentation/safari-release-notes/safari-17-release-notes Safari 17 release notes
* @see https://tauri.app/v1/references/webview-versions/#webkit-versions-in-safari Safari-WebKit versions map
*/
export function isWebKit616OrNewer(): boolean {
const w = window
const n = navigator
const { CSS, HTMLButtonElement } = w
return (
countTruthy([
!('getStorageUpdates' in n),
HTMLButtonElement && 'popover' in HTMLButtonElement.prototype,
'CSSCounterStyleRule' in w,
CSS.supports('font-size-adjust: ex-height 0.5'),
CSS.supports('text-transform: full-width'),
]) >= 4
)
}
/**
* Checks whether the device is an iPad.
* It doesn't check that the engine is WebKit and that the WebKit isn't desktop.
*/
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
)
}
/**
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
export function getFullscreenElement(): Element | null {
const d = document
return d.fullscreenElement || d.msFullscreenElement || d.mozFullScreenElement || d.webkitFullscreenElement || null
}
export function exitFullscreen(): Promise<void> {
const d = document
// `call` is required because the function throws an error without a proper "this" context
return (d.exitFullscreen || d.msExitFullscreen || d.mozCancelFullScreen || d.webkitExitFullscreen).call(d)
}
/**
* Checks whether the device runs on Android without using user-agent.
*
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
export function isAndroid(): boolean {
const isItChromium = isChromium()
const isItGecko = isGecko()
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
}
}
/**
* Checks whether the browser is Samsung Internet without using user-agent.
* It doesn't check that the browser is based on Chromium, please use `isChromium` before using this function.
*
* Warning for package users:
* This function is out of Semantic Versioning, i.e. can change unexpectedly. Usage is at your own risk.
*/
export function isSamsungInternet(): boolean {
// Checked in Samsung Internet 21, 25 and 27
const n = navigator
const w = window
const audioPrototype = Audio.prototype
const { visualViewport } = w
return (
countTruthy([
'srLatency' in audioPrototype,
'srChannelCount' in audioPrototype,
'devicePosture' in n, // Not available in HTTP
visualViewport && 'segments' in visualViewport,
'getTextInformation' in Image.prototype, // Not available in Samsung Internet 21
]) >= 3
)
}