Skip to content

Commit

Permalink
get actual macos version if possible to be more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed Oct 13, 2023
1 parent d259f21 commit 06904d6
Showing 1 changed file with 58 additions and 13 deletions.
71 changes: 58 additions & 13 deletions Extension/src/common/user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class UserAgent {

static PLATFORM_VERSION = 'platformVersion';

static MIN_WINDOWS_11_PLATFORM_VERSION = '13';
static MIN_WINDOWS_11_PLATFORM_VERSION = 13;

static parser = new UAParser(navigator.userAgent);

Expand Down Expand Up @@ -61,27 +61,68 @@ export class UserAgent {
return UserAgent.parser.getOS().version;
}

/**
* Returns current platform version.
* Uses NavigatorUAData.getHighEntropyValues() to get platform version.
*
* @returns Actual platform version as string if possible to detect, undefined otherwise.
*/
static async getPlatformVersion(): Promise<string | undefined> {
let platformVersion: string | undefined;
try {
// @ts-ignore
const ua = await navigator.userAgentData.getHighEntropyValues([UserAgent.PLATFORM_VERSION]);
platformVersion = ua[UserAgent.PLATFORM_VERSION];
} catch (e) {
// do nothing
}
return platformVersion;
}

/**
* Returns actual Windows version if it is parsed from user agent as Windows 10.
*
* @see {@link https://learn.microsoft.com/en-us/microsoft-edge/web-platform/how-to-detect-win11#sample-code-for-detecting-windows-11}.
*
* @returns Actual Windows version.
*/
static async getActualWindowsVersion(): Promise<string> {
let windowsVersion = UserAgent.WINDOWS_10_OS_VERSION;
try {
// @ts-ignore
const ua = await navigator.userAgentData.getHighEntropyValues([UserAgent.PLATFORM_VERSION]);
// @ts-ignore
const majorPlatformVersion = ua[UserAgent.PLATFORM_VERSION].split('.')[0];
static async getActualWindowsVersion(version: string): Promise<string> {
let actualVersion = version;
const platformVersion = await UserAgent.getPlatformVersion();

if (typeof platformVersion !== 'undefined') {
const rawMajorPlatformVersion = platformVersion.split('.')[0];
const majorPlatformVersion = rawMajorPlatformVersion && parseInt(rawMajorPlatformVersion, 10);

if (!majorPlatformVersion
|| Number.isNaN(majorPlatformVersion)) {
return actualVersion;
}

if (majorPlatformVersion >= UserAgent.MIN_WINDOWS_11_PLATFORM_VERSION) {
windowsVersion = UserAgent.WINDOWS_11_OS_VERSION;
actualVersion = UserAgent.WINDOWS_11_OS_VERSION;
}
} catch (e) {
// do nothing
}
return windowsVersion;

return actualVersion;
}

/**
* Returns actual MacOS version if it is possible to detect, otherwise returns passed `version`.
*
* @param version MacOS version parsed from user agent.
*
* @returns Actual MacOS version.
*/
static async getActualMacosVersion(version: string): Promise<string> {
let actualVersion = version;
const platformVersion = await UserAgent.getPlatformVersion();

if (typeof platformVersion !== 'undefined') {
actualVersion = platformVersion;
}

return actualVersion;
}

/**
Expand All @@ -101,7 +142,11 @@ export class UserAgent {
if (typeof osVersion !== 'undefined') {
// windows 11 is parsed as windows 10 from user agent
if (UserAgent.isWindows && osVersion === UserAgent.WINDOWS_10_OS_VERSION) {
osVersion = await UserAgent.getActualWindowsVersion();
osVersion = await UserAgent.getActualWindowsVersion(osVersion);
} else if (UserAgent.isMacOs) {
// mac os version can be parsed from user agent as 10.15.7
// so it also might be more specific version like 13.5.2
osVersion = await UserAgent.getActualMacosVersion(osVersion);
}
systemInfo += ` ${osVersion}`;
}
Expand Down

0 comments on commit 06904d6

Please sign in to comment.