-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathget-device.js
179 lines (163 loc) · 4.52 KB
/
get-device.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
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 { getWindow, getDocument } from 'ssr-window';
import { getSupport } from './get-support.js';
let deviceCalculated;
function calcDevice({ userAgent } = {}) {
const support = getSupport();
const window = getWindow();
const platform = window.navigator.platform;
const ua = userAgent || window.navigator.userAgent;
const device = {
ios: false,
android: false,
androidChrome: false,
desktop: false,
iphone: false,
ipod: false,
ipad: false,
edge: false,
ie: false,
firefox: false,
macos: false,
windows: false,
cordova: !!window.cordova,
electron: false,
capacitor: !!window.Capacitor,
nwjs: false,
};
const screenWidth = window.screen.width;
const screenHeight = window.screen.height;
const android = ua.match(/(Android);?[\s\/]+([\d.]+)?/); // eslint-disable-line
let ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
const ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
const iphone = !ipad && ua.match(/(iPhone\sOS|iOS|iPhone;\sCPU\sOS)\s([\d_]+)/);
const ie = ua.indexOf('MSIE ') >= 0 || ua.indexOf('Trident/') >= 0;
const edge = ua.indexOf('Edge/') >= 0;
const firefox = ua.indexOf('Gecko/') >= 0 && ua.indexOf('Firefox/') >= 0;
const windows = platform === 'Win32';
const electron = ua.toLowerCase().indexOf('electron') >= 0;
const nwjs =
typeof nw !== 'undefined' &&
typeof process !== 'undefined' &&
typeof process.versions !== 'undefined' &&
typeof process.versions.nw !== 'undefined';
let macos = platform === 'MacIntel';
// iPadOs 13 fix
const iPadScreens = [
'1024x1366',
'1366x1024',
'834x1194',
'1194x834',
'834x1112',
'1112x834',
'768x1024',
'1024x768',
'820x1180',
'1180x820',
'810x1080',
'1080x810',
];
if (
!ipad &&
macos &&
support.touch &&
iPadScreens.indexOf(`${screenWidth}x${screenHeight}`) >= 0
) {
ipad = ua.match(/(Version)\/([\d.]+)/);
if (!ipad) ipad = [0, 1, '13_0_0'];
macos = false;
}
device.ie = ie;
device.edge = edge;
device.firefox = firefox;
// Android
if (android) {
device.os = 'android';
device.osVersion = android[2];
device.android = true;
device.androidChrome = ua.toLowerCase().indexOf('chrome') >= 0;
}
if (ipad || iphone || ipod) {
device.os = 'ios';
device.ios = true;
}
// iOS
if (iphone && !ipod) {
device.osVersion = iphone[2].replace(/_/g, '.');
device.iphone = true;
}
if (ipad) {
device.osVersion = ipad[2].replace(/_/g, '.');
device.ipad = true;
}
if (ipod) {
device.osVersion = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
device.ipod = true;
}
// iOS 8+ changed UA
if (device.ios && device.osVersion && ua.indexOf('Version/') >= 0) {
if (device.osVersion.split('.')[0] === '10') {
device.osVersion = ua.toLowerCase().split('version/')[1].split(' ')[0];
}
}
// Webview
device.webView =
!!(
(iphone || ipad || ipod) &&
(ua.match(/.*AppleWebKit(?!.*Safari)/i) || window.navigator.standalone)
) ||
(window.matchMedia && window.matchMedia('(display-mode: standalone)').matches);
device.webview = device.webView;
device.standalone = device.webView;
// Desktop
device.desktop = !(device.ios || device.android) || electron || nwjs;
if (device.desktop) {
device.electron = electron;
device.nwjs = nwjs;
device.macos = macos;
device.windows = windows;
if (device.macos) {
device.os = 'macos';
}
if (device.windows) {
device.os = 'windows';
}
}
// Pixel Ratio
device.pixelRatio = window.devicePixelRatio || 1;
// Color Scheme
const DARK = '(prefers-color-scheme: dark)';
const LIGHT = '(prefers-color-scheme: light)';
device.prefersColorScheme = function prefersColorTheme() {
let theme;
if (window.matchMedia && window.matchMedia(LIGHT).matches) {
theme = 'light';
}
if (window.matchMedia && window.matchMedia(DARK).matches) {
theme = 'dark';
}
return theme;
};
// Export object
return device;
}
const IS_BROWSER = (() => {
const document = getDocument();
try {
// eslint-disable-next-line no-restricted-globals
return Boolean(
document &&
document.body &&
document.body.getBoundingClientRect &&
document.body.getBoundingClientRect().width > 0,
);
} catch (e) {
return false;
}
})();
function getDevice(overrides = {}, reset = IS_BROWSER) {
if (!deviceCalculated || reset) {
deviceCalculated = calcDevice(overrides);
}
return deviceCalculated;
}
export { getDevice };