Compare commits

...

5 Commits

Author SHA1 Message Date
619d70d3cb Update better-xcloud.user.js 2024-08-03 17:20:27 +07:00
fb123e00d7 Fix Settings button not showing on Header sometimes 2024-08-03 17:04:54 +07:00
39f7ee6ddb Add "detectBrowserRouterReady" patch 2024-08-02 20:47:28 +07:00
5db35cdcc9 Bug fixes 2024-08-02 07:19:27 +07:00
8c7e4650d4 Create PatcherUtils 2024-08-02 07:07:59 +07:00
5 changed files with 126 additions and 43 deletions

View File

@ -1,7 +1,7 @@
// ==UserScript==
// @name Better xCloud
// @namespace https://github.com/redphx
// @version 5.5.2
// @version 5.5.3-beta
// @description Improve Xbox Cloud Gaming (xCloud) experience
// @author redphx
// @license MIT
@ -120,7 +120,7 @@ function deepClone(obj) {
return {};
return JSON.parse(JSON.stringify(obj));
}
var SCRIPT_VERSION = "5.5.2", AppInterface = window.AppInterface;
var SCRIPT_VERSION = "5.5.3-beta", AppInterface = window.AppInterface;
UserAgent.init();
var userAgent = window.navigator.userAgent.toLowerCase(), isTv = userAgent.includes("smart-tv") || userAgent.includes("smarttv") || /\baft.*\b/.test(userAgent), isVr = window.navigator.userAgent.includes("VR") && window.navigator.userAgent.includes("OculusBrowser"), browserHasTouchSupport = "ontouchstart" in window || navigator.maxTouchPoints > 0, userAgentHasTouchSupport = !isTv && !isVr && browserHasTouchSupport, STATES = {
supportedRegion: !0,
@ -149,7 +149,7 @@ var userAgent = window.navigator.userAgent.toLowerCase(), isTv = userAgent.inclu
var BxEvent;
((BxEvent) => {
BxEvent.JUMP_BACK_IN_READY = "bx-jump-back-in-ready", BxEvent.POPSTATE = "bx-popstate", BxEvent.TITLE_INFO_READY = "bx-title-info-ready", BxEvent.SETTINGS_CHANGED = "bx-settings-changed", BxEvent.STREAM_LOADING = "bx-stream-loading", BxEvent.STREAM_STARTING = "bx-stream-starting", BxEvent.STREAM_STARTED = "bx-stream-started", BxEvent.STREAM_PLAYING = "bx-stream-playing", BxEvent.STREAM_STOPPED = "bx-stream-stopped", BxEvent.STREAM_ERROR_PAGE = "bx-stream-error-page", BxEvent.STREAM_WEBRTC_CONNECTED = "bx-stream-webrtc-connected", BxEvent.STREAM_WEBRTC_DISCONNECTED = "bx-stream-webrtc-disconnected", BxEvent.STREAM_SESSION_READY = "bx-stream-session-ready", BxEvent.CUSTOM_TOUCH_LAYOUTS_LOADED = "bx-custom-touch-layouts-loaded", BxEvent.TOUCH_LAYOUT_MANAGER_READY = "bx-touch-layout-manager-ready", BxEvent.REMOTE_PLAY_READY = "bx-remote-play-ready", BxEvent.REMOTE_PLAY_FAILED = "bx-remote-play-failed", BxEvent.XCLOUD_SERVERS_READY = "bx-servers-ready", BxEvent.XCLOUD_SERVERS_UNAVAILABLE = "bx-servers-unavailable", BxEvent.DATA_CHANNEL_CREATED = "bx-data-channel-created", BxEvent.GAME_BAR_ACTION_ACTIVATED = "bx-game-bar-action-activated", BxEvent.MICROPHONE_STATE_CHANGED = "bx-microphone-state-changed", BxEvent.CAPTURE_SCREENSHOT = "bx-capture-screenshot", BxEvent.POINTER_LOCK_REQUESTED = "bx-pointer-lock-requested", BxEvent.POINTER_LOCK_EXITED = "bx-pointer-lock-exited", BxEvent.NAVIGATION_FOCUS_CHANGED = "bx-nav-focus-changed", BxEvent.XCLOUD_DIALOG_SHOWN = "bx-xcloud-dialog-shown", BxEvent.XCLOUD_DIALOG_DISMISSED = "bx-xcloud-dialog-dismissed", BxEvent.XCLOUD_GUIDE_MENU_SHOWN = "bx-xcloud-guide-menu-shown", BxEvent.XCLOUD_POLLING_MODE_CHANGED = "bx-xcloud-polling-mode-changed", BxEvent.XCLOUD_RENDERING_COMPONENT = "bx-xcloud-rendering-page";
BxEvent.JUMP_BACK_IN_READY = "bx-jump-back-in-ready", BxEvent.POPSTATE = "bx-popstate", BxEvent.TITLE_INFO_READY = "bx-title-info-ready", BxEvent.SETTINGS_CHANGED = "bx-settings-changed", BxEvent.STREAM_LOADING = "bx-stream-loading", BxEvent.STREAM_STARTING = "bx-stream-starting", BxEvent.STREAM_STARTED = "bx-stream-started", BxEvent.STREAM_PLAYING = "bx-stream-playing", BxEvent.STREAM_STOPPED = "bx-stream-stopped", BxEvent.STREAM_ERROR_PAGE = "bx-stream-error-page", BxEvent.STREAM_WEBRTC_CONNECTED = "bx-stream-webrtc-connected", BxEvent.STREAM_WEBRTC_DISCONNECTED = "bx-stream-webrtc-disconnected", BxEvent.STREAM_SESSION_READY = "bx-stream-session-ready", BxEvent.CUSTOM_TOUCH_LAYOUTS_LOADED = "bx-custom-touch-layouts-loaded", BxEvent.TOUCH_LAYOUT_MANAGER_READY = "bx-touch-layout-manager-ready", BxEvent.REMOTE_PLAY_READY = "bx-remote-play-ready", BxEvent.REMOTE_PLAY_FAILED = "bx-remote-play-failed", BxEvent.XCLOUD_SERVERS_READY = "bx-servers-ready", BxEvent.XCLOUD_SERVERS_UNAVAILABLE = "bx-servers-unavailable", BxEvent.DATA_CHANNEL_CREATED = "bx-data-channel-created", BxEvent.GAME_BAR_ACTION_ACTIVATED = "bx-game-bar-action-activated", BxEvent.MICROPHONE_STATE_CHANGED = "bx-microphone-state-changed", BxEvent.CAPTURE_SCREENSHOT = "bx-capture-screenshot", BxEvent.POINTER_LOCK_REQUESTED = "bx-pointer-lock-requested", BxEvent.POINTER_LOCK_EXITED = "bx-pointer-lock-exited", BxEvent.NAVIGATION_FOCUS_CHANGED = "bx-nav-focus-changed", BxEvent.XCLOUD_DIALOG_SHOWN = "bx-xcloud-dialog-shown", BxEvent.XCLOUD_DIALOG_DISMISSED = "bx-xcloud-dialog-dismissed", BxEvent.XCLOUD_GUIDE_MENU_SHOWN = "bx-xcloud-guide-menu-shown", BxEvent.XCLOUD_POLLING_MODE_CHANGED = "bx-xcloud-polling-mode-changed", BxEvent.XCLOUD_RENDERING_COMPONENT = "bx-xcloud-rendering-page", BxEvent.XCLOUD_ROUTER_HISTORY_READY = "bx-xcloud-router-history-ready";
function dispatch(target, eventName, data) {
if (!target)
return;
@ -3592,14 +3592,34 @@ if (getPref("block_social_features"))
if (BX_FLAGS.FeatureGates)
FeatureGates = Object.assign(BX_FLAGS.FeatureGates, FeatureGates);
class PatcherUtils {
static indexOf(txt, searchString, startIndex, maxRange) {
const index = txt.indexOf(searchString, startIndex);
if (index < 0 || maxRange && index - startIndex > maxRange)
return -1;
return index;
}
static lastIndexOf(txt, searchString, startIndex, maxRange) {
const index = txt.lastIndexOf(searchString, startIndex);
if (index < 0 || maxRange && startIndex - index > maxRange)
return -1;
return index;
}
static insertAt(txt, index, insertString) {
return txt.substring(0, index) + insertString + txt.substring(index);
}
static replaceWith(txt, index, fromString, toString) {
return txt.substring(0, index) + toString + txt.substring(index + fromString.length);
}
}
var ENDING_CHUNKS_PATCH_NAME = "loadingEndingChunks", LOG_TAG3 = "Patcher", PATCHES = {
disableAiTrack(str) {
const index = str.indexOf(".track=function(");
if (index < 0)
return !1;
if (str.substring(0, index + 200).includes('"AppInsightsCore'))
if (PatcherUtils.indexOf(str, '"AppInsightsCore', index, 200) < 0)
return !1;
return str.substring(0, index) + ".track=function(e){},!!function(" + str.substring(index + ".track=function(".length);
return PatcherUtils.replaceWith(str, index, ".track=function(", ".track=function(e){},!!function(");
},
disableTelemetry(str) {
if (!str.includes(".disableTelemetry=function(){return!1}"))
@ -3953,31 +3973,33 @@ true,this._connectionType=`;
let index = str.indexOf('location:"PlayWithFriendsRow",');
if (index < 0)
return !1;
if (index = str.indexOf("return", index - 50), index < 0)
if (index = PatcherUtils.lastIndexOf(str, "return", index, 50), index < 0)
return !1;
return str = str.substring(0, index) + "return null;" + str.substring(index + 6), str;
return str = PatcherUtils.replaceWith(str, index, "return", "return null;"), str;
},
ignoreAllGamesSection(str) {
let index = str.indexOf('className:"AllGamesRow-module__allGamesRowContainer');
if (index < 0)
return !1;
if (index = str.indexOf("grid:!0,", index), index > -1 && (index = str.indexOf("(0,", index - 70)), index < 0)
if (index = PatcherUtils.indexOf(str, "grid:!0,", index, 1500), index < 0)
return !1;
return str = str.substring(0, index) + "true ? null :" + str.substring(index), str;
if (index = PatcherUtils.lastIndexOf(str, "(0,", index, 70), index < 0)
return !1;
return str = PatcherUtils.insertAt(str, index, "true ? null :"), str;
},
ignorePlayWithTouchSection(str) {
let index = str.indexOf('("Play_With_Touch"),');
if (index < 0)
return !1;
if (index = str.indexOf("const ", index - 30), index < 0)
if (index = PatcherUtils.lastIndexOf(str, "const ", index, 30), index < 0)
return !1;
return str = str.substring(0, index) + "return null;" + str.substring(index), str;
return str = PatcherUtils.insertAt(str, index, "return null;"), str;
},
ignoreSiglSections(str) {
let index = str.indexOf("SiglRow-module__heroCard___");
if (index < 0)
return !1;
if (index = str.indexOf("const[", index - 300), index < 0)
if (index = PatcherUtils.lastIndexOf(str, "const[", index, 300), index < 0)
return !1;
const PREF_HIDE_SECTIONS = getPref("ui_hide_sections"), siglIds = [], sections = {
"native-mkb": "8fa264dd-124f-4af3-97e8-596fcdf4b486",
@ -3995,7 +4017,7 @@ if (e && e.id) {
}
}
`;
return str = str.substring(0, index) + newCode + str.substring(index), str;
return str = PatcherUtils.insertAt(str, index, newCode), str;
},
overrideStorageGetSettings(str) {
if (!str.includes("}getSetting(e){"))
@ -4033,6 +4055,16 @@ if (this.baseStorageKey in window.BX_EXPOSED.overrideSettings) {
if (index = str.indexOf("return", index - 40), index < 0)
return !1;
return str = str.substring(0, index) + 'BxEvent.dispatch(window, BxEvent.XCLOUD_RENDERING_COMPONENT, {component: "product-details"});' + str.substring(index), str;
},
detectBrowserRouterReady(str) {
if (!str.includes("BrowserRouter:()=>"))
return !1;
let index = str.indexOf("{history:this.history,");
if (index < 0)
return !1;
if (index = PatcherUtils.lastIndexOf(str, "return", index, 100), index < 0)
return !1;
return str = PatcherUtils.insertAt(str, index, "window.BxEvent.dispatch(window, window.BxEvent.XCLOUD_ROUTER_HISTORY_READY, {history: this.history});"), str;
}
}, PATCH_ORDERS = [
...getPref("native_mkb_enabled") === "on" ? [
@ -4041,6 +4073,7 @@ if (this.baseStorageKey in window.BX_EXPOSED.overrideSettings) {
"disableNativeRequestPointerLock",
"exposeInputSink"
] : [],
"detectBrowserRouterReady",
"patchRequestInfoCrash",
"disableStreamGate",
"overrideSettings",
@ -4150,7 +4183,7 @@ class Patcher {
item[1][id] = eval(patchedFuncStr);
} catch (e) {
if (e instanceof Error)
BxLogger.error(LOG_TAG3, "Error", appliedPatches, e.message);
BxLogger.error(LOG_TAG3, "Error", appliedPatches, e.message, patchedFuncStr);
}
if (appliedPatches.length)
patchesMap[id] = appliedPatches;
@ -5763,18 +5796,16 @@ class HeaderSection {
$parent.appendChild(HeaderSection.#$buttonsWrapper);
}
static checkHeader() {
if (!HeaderSection.#$buttonsWrapper.isConnected) {
let $target = document.querySelector("#PageContent div[class*=EdgewaterHeader-module__rightSectionSpacing]");
if (!$target)
$target = document.querySelector("div[class^=UnsupportedMarketPage-module__buttons]");
$target && HeaderSection.#injectSettingsButton($target);
}
let $target = document.querySelector("#PageContent div[class*=EdgewaterHeader-module__rightSectionSpacing]");
if (!$target)
$target = document.querySelector("div[class^=UnsupportedMarketPage-module__buttons]");
$target && HeaderSection.#injectSettingsButton($target);
}
static showRemotePlayButton() {
HeaderSection.#$remotePlayBtn.classList.remove("bx-gone");
}
static watchHeader() {
let $root = document.querySelector("#PageContent header") || document.querySelector("#root");
const $root = document.querySelector("#PageContent header") || document.querySelector("#root");
if (!$root)
return;
HeaderSection.#observer && HeaderSection.#observer.disconnect(), HeaderSection.#observer = new MutationObserver((mutationList) => {

View File

@ -20,8 +20,35 @@ import { GamePassCloudGallery } from "@/enums/game-pass-gallery.js";
type PatchArray = (keyof typeof PATCHES)[];
const ENDING_CHUNKS_PATCH_NAME = 'loadingEndingChunks';
class PatcherUtils {
static indexOf(txt: string, searchString: string, startIndex: number, maxRange: number): number {
const index = txt.indexOf(searchString, startIndex);
if (index < 0 || (maxRange && index - startIndex > maxRange)) {
return -1;
}
return index;
}
static lastIndexOf(txt: string, searchString: string, startIndex: number, maxRange: number): number {
const index = txt.lastIndexOf(searchString, startIndex);
if (index < 0 || (maxRange && startIndex - index > maxRange)) {
return -1;
}
return index;
}
static insertAt(txt: string, index: number, insertString: string): string {
return txt.substring(0, index) + insertString + txt.substring(index);
}
static replaceWith(txt: string, index: number, fromString: string, toString: string): string {
return txt.substring(0, index) + toString + txt.substring(index + fromString.length);
}
}
const ENDING_CHUNKS_PATCH_NAME = 'loadingEndingChunks';
const LOG_TAG = 'Patcher';
const PATCHES = {
@ -33,11 +60,11 @@ const PATCHES = {
return false;
}
if (str.substring(0, index + 200).includes('"AppInsightsCore')) {
if (PatcherUtils.indexOf(str, '"AppInsightsCore', index, 200) < 0) {
return false;
}
return str.substring(0, index) + '.track=function(e){},!!function(' + str.substring(index + text.length);
return PatcherUtils.replaceWith(str, index, text, '.track=function(e){},!!function(');
},
// Set disableTelemetry() to true
@ -716,12 +743,12 @@ true` + text;
return false;
}
index = str.indexOf('return', index - 50);
index = PatcherUtils.lastIndexOf(str, 'return', index, 50);
if (index < 0) {
return false;
}
str = str.substring(0, index) + 'return null;' + str.substring(index + 6);
str = PatcherUtils.replaceWith(str, index, 'return', 'return null;');
return str;
},
@ -732,14 +759,17 @@ true` + text;
return false;
}
index = str.indexOf('grid:!0,', index);
index > -1 && (index = str.indexOf('(0,', index - 70));
index = PatcherUtils.indexOf(str, 'grid:!0,', index, 1500);
if (index < 0) {
return false;
}
str = str.substring(0, index) + 'true ? null :' + str.substring(index);
index = PatcherUtils.lastIndexOf(str, '(0,', index, 70);
if (index < 0) {
return false;
}
str = PatcherUtils.insertAt(str, index, 'true ? null :');
return str;
},
@ -750,12 +780,12 @@ true` + text;
return false;
}
index = str.indexOf('const ', index - 30);
index = PatcherUtils.lastIndexOf(str, 'const ', index, 30);
if (index < 0) {
return false;
}
str = str.substring(0, index) + 'return null;' + str.substring(index);
str = PatcherUtils.insertAt(str, index, 'return null;');
return str;
},
@ -766,7 +796,7 @@ true` + text;
return false;
}
index = str.indexOf('const[', index - 300);
index = PatcherUtils.lastIndexOf(str, 'const[', index, 300);
if (index < 0) {
return false;
}
@ -794,7 +824,7 @@ if (e && e.id) {
}
}
`;
str = str.substring(0, index) + newCode + str.substring(index);
str = PatcherUtils.insertAt(str, index, newCode);
return str;
},
@ -862,6 +892,26 @@ if (this.baseStorageKey in window.BX_EXPOSED.overrideSettings) {
str = str.substring(0, index) + 'BxEvent.dispatch(window, BxEvent.XCLOUD_RENDERING_COMPONENT, {component: "product-details"});' + str.substring(index);
return str;
},
detectBrowserRouterReady(str: string) {
const text = 'BrowserRouter:()=>';
if (!str.includes(text)) {
return false;
}
let index = str.indexOf('{history:this.history,');
if (index < 0) {
return false;
}
index = PatcherUtils.lastIndexOf(str, 'return', index, 100);
if (index < 0) {
return false;
}
str = PatcherUtils.insertAt(str, index, 'window.BxEvent.dispatch(window, window.BxEvent.XCLOUD_ROUTER_HISTORY_READY, {history: this.history});');
return str;
},
};
let PATCH_ORDERS: PatchArray = [
@ -872,6 +922,7 @@ let PATCH_ORDERS: PatchArray = [
'exposeInputSink',
] : []),
'detectBrowserRouterReady',
'patchRequestInfoCrash',
'disableStreamGate',
@ -1067,7 +1118,7 @@ export class Patcher {
item[1][id] = eval(patchedFuncStr);
} catch (e: unknown) {
if (e instanceof Error) {
BxLogger.error(LOG_TAG, 'Error', appliedPatches, e.message);
BxLogger.error(LOG_TAG, 'Error', appliedPatches, e.message, patchedFuncStr);
}
}
}

View File

@ -57,13 +57,12 @@ export class HeaderSection {
}
static checkHeader() {
if (!HeaderSection.#$buttonsWrapper.isConnected) {
let $target = document.querySelector('#PageContent div[class*=EdgewaterHeader-module__rightSectionSpacing]');
if (!$target) {
$target = document.querySelector("div[class^=UnsupportedMarketPage-module__buttons]");
}
$target && HeaderSection.#injectSettingsButton($target as HTMLElement);
let $target = document.querySelector('#PageContent div[class*=EdgewaterHeader-module__rightSectionSpacing]');
if (!$target) {
$target = document.querySelector("div[class^=UnsupportedMarketPage-module__buttons]");
}
$target && HeaderSection.#injectSettingsButton($target as HTMLElement);
}
static showRemotePlayButton() {
@ -71,7 +70,7 @@ export class HeaderSection {
}
static watchHeader() {
let $root = document.querySelector('#PageContent header') || document.querySelector('#root');
const $root = document.querySelector('#PageContent header') || document.querySelector('#root');
if (!$root) {
return;
}

View File

@ -53,6 +53,8 @@ export namespace BxEvent {
export const XCLOUD_RENDERING_COMPONENT = 'bx-xcloud-rendering-page';
export const XCLOUD_ROUTER_HISTORY_READY = 'bx-xcloud-router-history-ready';
export function dispatch(target: Element | Window | null, eventName: string, data?: any) {
if (!target) {
return;

View File

@ -6,7 +6,7 @@ import { getPref } from "./settings-storages/global-settings-storage";
export function addCss() {
const STYLUS_CSS = renderStylus();
const STYLUS_CSS = renderStylus() as unknown as string;
let css = STYLUS_CSS;
const PREF_HIDE_SECTIONS = getPref(PrefKey.UI_HIDE_SECTIONS);