blob: 3b8d43fb2c1e8e22e6a226e9d24a20cb2ed1bd12 (
plain)
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
|
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
export type UserEnvironment = {
os: string;
browser: string;
userAgent: string;
screenWidth: number;
screenHeight: number;
viaGetHighEntropyValues: true;
} | {
userAgent: string;
screenWidth: number;
screenHeight: number;
viaGetHighEntropyValues: false;
};
export async function getUserEnvironment(): Promise<UserEnvironment> {
if ('userAgentData' in navigator && navigator.userAgentData != null) {
try {
const uaData: any = await navigator.userAgentData.getHighEntropyValues([
'fullVersionList',
'platformVersion',
]);
let osVersion = 'v' + uaData.platformVersion;
if (uaData.platform === 'Windows' && uaData.platformVersion != null) {
// https://learn.microsoft.com/ja-jp/microsoft-edge/web-platform/how-to-detect-win11
const majorPlatformVersion = parseInt(uaData.platformVersion.split('.')[0]);
if (majorPlatformVersion >= 13) {
osVersion = '11 or later';
} else if (majorPlatformVersion > 0) {
osVersion = '10';
} else {
osVersion = '8.1 or earlier';
}
}
const browserData = uaData.fullVersionList.find((item) => !/^\s*not.+a.+brand\s*$/i.test(item.brand));
return {
os: `${uaData.platform} ${osVersion}`,
browser: browserData ? `${browserData.brand} v${browserData.version}` : 'Unknown',
userAgent: navigator.userAgent,
screenWidth: window.innerWidth,
screenHeight: window.innerHeight,
viaGetHighEntropyValues: true,
};
} catch {
return getViaUa();
}
} else {
return getViaUa();
}
}
function getViaUa(): UserEnvironment {
return {
userAgent: navigator.userAgent,
screenWidth: window.innerWidth,
screenHeight: window.innerHeight,
viaGetHighEntropyValues: false,
};
}
|