From 8c7e4650d421ffc20e13f642ab1e6ff168b41570 Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Fri, 2 Aug 2024 07:07:59 +0700 Subject: [PATCH] Create PatcherUtils --- dist/better-xcloud.user.js | 46 +++++++++++++++++++++++-------- src/modules/patcher.ts | 56 +++++++++++++++++++++++++++++--------- 2 files changed, 77 insertions(+), 25 deletions(-) diff --git a/dist/better-xcloud.user.js b/dist/better-xcloud.user.js index 1c3225b..fffd23d 100644 --- a/dist/better-xcloud.user.js +++ b/dist/better-xcloud.user.js @@ -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, @@ -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, ".track=function(", ".track=function(e){},!!function(", index); }, 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){")) diff --git a/src/modules/patcher.ts b/src/modules/patcher.ts index f3562ca..c2d54e0 100644 --- a/src/modules/patcher.ts +++ b/src/modules/patcher.ts @@ -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, text, '.track=function(e){},!!function(', index); }, // 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; },