-
Notifications
You must be signed in to change notification settings - Fork 733
/
Copy pathbrowserInfo.js
83 lines (69 loc) · 2.57 KB
/
browserInfo.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
import UAParser from 'ua-parser-js';
/**
* A requirements specification object.
* @typedef {Object.<string, Browser>} Requirements.
*/
/**
* A function to determine if the browser specification object fails to pass one of the restrictions
* passed in. It does this in a permissive way, whereby if no specification is made for a particular
* browser name, then it will return true. If, however, the browser is specified in the requirements
* object, then if it fails to pass the version requirements specified, then false will be returned.
* @param {Browser} browser - a browser specification object for the browser being tested.
* @param {Requirements} requirements - a requirements specification object specifying conditions
* to test.
* @return {boolean} - false if failed to pass any of the requirements, true otherwise.
*/
export function passesRequirements(browser, requirements) {
if (browser.major && browser.name) {
const entry = requirements[browser.name];
if (entry) {
if (browser.major < entry.major) {
return false;
} else if (browser.major === entry.major) {
if (entry.minor && (browser.minor < entry.minor || !browser.minor)) {
return false;
} else if (entry.minor && browser.minor === entry.minor) {
if (entry.patch && (browser.patch < entry.patch || !browser.patch)) {
return false;
}
}
}
}
}
return true;
}
export const userAgent =
window && window.navigator && window.navigator.userAgent ? window.navigator.userAgent : '';
const parser = new UAParser(userAgent);
/**
* General browser info
*/
const info = parser.getResult();
const browserVersion = (info.browser.version || '').split('.');
export const browser = {
name: info.browser.name,
major: browserVersion[0],
minor: browserVersion[1],
patch: browserVersion[2],
};
const osVersion = (info.os.version || '').split('.');
export const os = {
name: info.os.name,
major: osVersion[0],
minor: osVersion[1],
patch: osVersion[2],
};
// Check for presence of the touch event in DOM or multi-touch capabilities
export const isTouchDevice =
'ontouchstart' in window ||
window.navigator?.maxTouchPoints > 0 ||
window.navigator?.msMaxTouchPoints > 0;
function handlePointerDown(event) {
if (event.pointerType === 'mouse') {
localStorage.setItem('mouseUsed', 'true');
isMouseUsed = true;
window.removeEventListener('pointerdown', handlePointerDown);
}
}
window.addEventListener('pointerdown', handlePointerDown);
export let isMouseUsed = localStorage.getItem('mouseUsed') === 'true';