Move User-Agent values to a separate localStorage item

This commit is contained in:
redphx
2024-05-11 09:08:08 +07:00
parent 77f7b647da
commit 9ce906c0b2
4 changed files with 44 additions and 12 deletions

View File

@@ -20,7 +20,6 @@ export enum PrefKey {
STREAM_CODEC_PROFILE = 'stream_codec_profile',
USER_AGENT_PROFILE = 'user_agent_profile',
USER_AGENT_CUSTOM = 'user_agent_custom',
STREAM_SIMPLIFY_MENU = 'stream_simplify_menu',
STREAM_COMBINE_SOURCES = 'stream_combine_sources',
@@ -450,9 +449,6 @@ export class Preferences {
[UserAgentProfile.CUSTOM]: t('custom'),
},
},
[PrefKey.USER_AGENT_CUSTOM]: {
default: '',
},
[PrefKey.VIDEO_CLARITY]: {
type: SettingElementType.NUMBER_STEPPER,
default: 0,
@@ -722,7 +718,6 @@ export class Preferences {
const setting = Preferences.SETTINGS[key];
let currentValue = this.get(key);
let $control;
let type;
if ('type' in setting) {
type = setting.type;
@@ -741,7 +736,7 @@ export class Preferences {
currentValue = Preferences.SETTINGS[key].default;
}
$control = SettingElement.render(type!, key as string, setting, currentValue, (e: any, value: any) => {
const $control = SettingElement.render(type!, key as string, setting, currentValue, (e: any, value: any) => {
this.set(key, value);
onChange && onChange(e, value);
}, params);

View File

@@ -1,4 +1,7 @@
import { PrefKey, getPref } from "@utils/preferences";
type UserAgentConfig = {
profile: UserAgentProfile,
custom?: string,
};
export enum UserAgentProfile {
EDGE_WINDOWS = 'edge-windows',
@@ -21,6 +24,9 @@ if (!!(window as any).chrome || window.navigator.userAgent.includes('Chrome')) {
}
export class UserAgent {
static readonly STORAGE_KEY = 'better_xcloud_user_agent';
static #config: UserAgentConfig;
static #USER_AGENTS: PartialRecord<UserAgentProfile, string> = {
[UserAgentProfile.EDGE_WINDOWS]: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${CHROMIUM_VERSION} Safari/537.36 Edg/${CHROMIUM_VERSION}`,
[UserAgentProfile.SAFARI_MACOS]: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.1',
@@ -30,6 +36,28 @@ export class UserAgent {
[UserAgentProfile.KIWI_V123]: 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.118 Mobile Safari/537.36',
}
static init() {
UserAgent.#config = JSON.parse(window.localStorage.getItem(UserAgent.STORAGE_KEY) || '{}') as UserAgentConfig;
if (!UserAgent.#config.profile) {
UserAgent.#config.profile = UserAgentProfile.DEFAULT;
}
if (!UserAgent.#config.custom) {
UserAgent.#config.custom = '';
}
}
static updateStorage(profile: UserAgentProfile, custom?: string) {
const clonedConfig = structuredClone(UserAgent.#config);
clonedConfig.profile = profile;
if (typeof custom !== 'undefined') {
clonedConfig.custom = custom;
}
window.localStorage.setItem(UserAgent.STORAGE_KEY, JSON.stringify(clonedConfig));
}
static getDefault(): string {
return (window.navigator as any).orgUserAgent || window.navigator.userAgent;
}
@@ -37,7 +65,7 @@ export class UserAgent {
static get(profile: UserAgentProfile): string {
const defaultUserAgent = UserAgent.getDefault();
if (profile === UserAgentProfile.CUSTOM) {
return getPref(PrefKey.USER_AGENT_CUSTOM);
return UserAgent.#config.custom || '';
}
return (UserAgent.#USER_AGENTS as any)[profile] || defaultUserAgent;
@@ -62,7 +90,7 @@ export class UserAgent {
static spoof() {
let newUserAgent;
const profile = getPref(PrefKey.USER_AGENT_PROFILE);
const profile = UserAgent.#config.profile;
if (profile === UserAgentProfile.DEFAULT) {
return;
}