enhance: getHighEntropyValuesが使用できなかった場合のフォールバックを追加
This commit is contained in:
parent
21fb769f95
commit
ec4fc20ce4
|
@ -2,6 +2,11 @@
|
||||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
import type * as Bowser_TR from 'bowser';
|
||||||
|
|
||||||
|
type Bowser_TypeReferenceOnly = typeof Bowser_TR;
|
||||||
|
|
||||||
|
let Bowser: Bowser_TypeReferenceOnly | null = null;
|
||||||
|
|
||||||
export type UserEnvironment = {
|
export type UserEnvironment = {
|
||||||
os: string;
|
os: string;
|
||||||
|
@ -13,42 +18,52 @@ export type UserEnvironment = {
|
||||||
|
|
||||||
export async function getUserEnvironment(): Promise<UserEnvironment> {
|
export async function getUserEnvironment(): Promise<UserEnvironment> {
|
||||||
if ('userAgentData' in navigator && navigator.userAgentData != null) {
|
if ('userAgentData' in navigator && navigator.userAgentData != null) {
|
||||||
const uaData: any = await navigator.userAgentData.getHighEntropyValues([
|
try {
|
||||||
'fullVersionList',
|
const uaData: any = await navigator.userAgentData.getHighEntropyValues([
|
||||||
'platformVersion',
|
'fullVersionList',
|
||||||
]);
|
'platformVersion',
|
||||||
|
]);
|
||||||
|
|
||||||
let osVersion = 'v' + uaData.platformVersion;
|
let osVersion = 'v' + uaData.platformVersion;
|
||||||
|
|
||||||
if (uaData.platform === 'Windows' && uaData.platformVersion != null) {
|
if (uaData.platform === 'Windows' && uaData.platformVersion != null) {
|
||||||
// https://learn.microsoft.com/ja-jp/microsoft-edge/web-platform/how-to-detect-win11
|
// https://learn.microsoft.com/ja-jp/microsoft-edge/web-platform/how-to-detect-win11
|
||||||
const majorPlatformVersion = parseInt(uaData.platformVersion.split('.')[0]);
|
const majorPlatformVersion = parseInt(uaData.platformVersion.split('.')[0]);
|
||||||
if (majorPlatformVersion >= 13) {
|
if (majorPlatformVersion >= 13) {
|
||||||
osVersion = '11 or later';
|
osVersion = '11 or later';
|
||||||
} else if (majorPlatformVersion > 0) {
|
} else if (majorPlatformVersion > 0) {
|
||||||
osVersion = '10';
|
osVersion = '10';
|
||||||
} else {
|
} else {
|
||||||
osVersion = '8.1 or earlier';
|
osVersion = '8.1 or earlier';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const browserData = uaData.fullVersionList.find((item) => !/^\s*not.+a.+brand\s*$/i.test(item.brand));
|
const browserData = uaData.fullVersionList.find((item) => !/^\s*not.+a.+brand\s*$/i.test(item.brand));
|
||||||
return {
|
return {
|
||||||
os: `${uaData.platform} ${osVersion}`,
|
os: `${uaData.platform} ${osVersion}`,
|
||||||
browser: browserData ? `${browserData.brand} v${browserData.version}` : 'Unknown',
|
browser: browserData ? `${browserData.brand} v${browserData.version}` : 'Unknown',
|
||||||
screenWidth: window.innerWidth,
|
screenWidth: window.innerWidth,
|
||||||
screenHeight: window.innerHeight,
|
screenHeight: window.innerHeight,
|
||||||
viaGetHighEntropyValues: true,
|
viaGetHighEntropyValues: true,
|
||||||
};
|
};
|
||||||
|
} catch {
|
||||||
|
return getViaUa();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const Bowser = (await import('bowser')).default;
|
return getViaUa();
|
||||||
const parsed = Bowser.parse(navigator.userAgent);
|
|
||||||
return {
|
|
||||||
os: `${parsed.os.name ?? 'Unknown'} ${parsed.os.version ?? ''} ${parsed.os.versionName ? `(${parsed.os.versionName})` : ''}`.trim(),
|
|
||||||
browser: `${parsed.browser.name ?? 'Unknown'} ${parsed.browser.version ?? ''}`.trim(),
|
|
||||||
screenWidth: window.innerWidth,
|
|
||||||
screenHeight: window.innerHeight,
|
|
||||||
viaGetHighEntropyValues: false,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getViaUa(): Promise<UserEnvironment> {
|
||||||
|
if (Bowser == null) {
|
||||||
|
Bowser = (await import('bowser')).default;
|
||||||
|
}
|
||||||
|
const parsed = Bowser.parse(navigator.userAgent);
|
||||||
|
return {
|
||||||
|
os: `${parsed.os.name ?? 'Unknown'} ${parsed.os.version ?? ''} ${parsed.os.versionName ? `(${parsed.os.versionName})` : ''}`.trim(),
|
||||||
|
browser: `${parsed.browser.name ?? 'Unknown'} ${parsed.browser.version ?? ''}`.trim(),
|
||||||
|
screenWidth: window.innerWidth,
|
||||||
|
screenHeight: window.innerHeight,
|
||||||
|
viaGetHighEntropyValues: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue