Refactor UserAgent class

This commit is contained in:
redphx 2024-05-11 09:35:38 +07:00
parent daaaea1f16
commit 011b75057a
2 changed files with 20 additions and 24 deletions

View File

@ -1,5 +1,4 @@
import { STATES } from "@utils/global"; import { STATES } from "@utils/global";
import { UserAgent } from "@utils/user-agent";
import { BxLogger } from "./bx-logger"; import { BxLogger } from "./bx-logger";
import { TouchController } from "@modules/touch-controller"; import { TouchController } from "@modules/touch-controller";
import { GamePassCloudGallery } from "./gamepass-gallery"; import { GamePassCloudGallery } from "./gamepass-gallery";
@ -12,19 +11,14 @@ export function overridePreloadState() {
Object.defineProperty(window, '__PRELOADED_STATE__', { Object.defineProperty(window, '__PRELOADED_STATE__', {
configurable: true, configurable: true,
get: () => { get: () => {
// @ts-ignore
return _state; return _state;
}, },
set: state => { set: state => {
// Override User-Agent // Override User-Agent
const userAgent = UserAgent.spoof(); try {
if (userAgent) { state.appContext.requestInfo.userAgent = window.navigator.userAgent;
try { } catch (e) {
// @ts-ignore BxLogger.error(LOG_TAG, e);
state.appContext.requestInfo.userAgent = userAgent;
} catch (e) {
BxLogger.error(LOG_TAG, e);
}
} }
// Add list of games with custom layouts to the official list // Add list of games with custom layouts to the official list

View File

@ -45,6 +45,8 @@ export class UserAgent {
if (!UserAgent.#config.custom) { if (!UserAgent.#config.custom) {
UserAgent.#config.custom = ''; UserAgent.#config.custom = '';
} }
UserAgent.spoof();
} }
static updateStorage(profile: UserAgentProfile, custom?: string) { static updateStorage(profile: UserAgentProfile, custom?: string) {
@ -63,16 +65,22 @@ export class UserAgent {
} }
static get(profile: UserAgentProfile): string { static get(profile: UserAgentProfile): string {
const defaultUserAgent = UserAgent.getDefault(); const defaultUserAgent = window.navigator.userAgent;
if (profile === UserAgentProfile.CUSTOM) {
return UserAgent.#config.custom || '';
}
return (UserAgent.#USER_AGENTS as any)[profile] || defaultUserAgent; switch (profile) {
case UserAgentProfile.DEFAULT:
return defaultUserAgent;
case UserAgentProfile.CUSTOM:
return UserAgent.#config.custom || defaultUserAgent;
default:
return UserAgent.#USER_AGENTS[profile] || defaultUserAgent;
}
} }
static isSafari(mobile=false): boolean { static isSafari(mobile=false): boolean {
const userAgent = (UserAgent.getDefault() || '').toLowerCase(); const userAgent = UserAgent.getDefault().toLowerCase();
let result = userAgent.includes('safari') && !userAgent.includes('chrom'); let result = userAgent.includes('safari') && !userAgent.includes('chrom');
if (result && mobile) { if (result && mobile) {
@ -83,21 +91,17 @@ export class UserAgent {
} }
static isMobile(): boolean { static isMobile(): boolean {
const userAgent = (UserAgent.getDefault() || '').toLowerCase(); const userAgent = UserAgent.getDefault().toLowerCase();
return /iphone|ipad|android/.test(userAgent); return /iphone|ipad|android/.test(userAgent);
} }
static spoof() { static spoof() {
let newUserAgent;
const profile = UserAgent.#config.profile; const profile = UserAgent.#config.profile;
if (profile === UserAgentProfile.DEFAULT) { if (profile === UserAgentProfile.DEFAULT) {
return; return;
} }
if (!newUserAgent) { const newUserAgent = UserAgent.get(profile);
newUserAgent = UserAgent.get(profile);
}
// Clear data of navigator.userAgentData, force xCloud to detect browser based on navigator.userAgent // Clear data of navigator.userAgentData, force xCloud to detect browser based on navigator.userAgent
(window.navigator as any).orgUserAgentData = (window.navigator as any).userAgentData; (window.navigator as any).orgUserAgentData = (window.navigator as any).userAgentData;
@ -108,7 +112,5 @@ export class UserAgent {
Object.defineProperty(window.navigator, 'userAgent', { Object.defineProperty(window.navigator, 'userAgent', {
value: newUserAgent, value: newUserAgent,
}); });
return newUserAgent;
} }
} }