diff --git a/src/modules/patcher.ts b/src/modules/patcher.ts index 5f72a7e..d50eb2e 100644 --- a/src/modules/patcher.ts +++ b/src/modules/patcher.ts @@ -3,9 +3,10 @@ import { BX_FLAGS } from "@utils/bx-flags"; import { getPref, PrefKey } from "@utils/preferences"; import { VibrationManager } from "@modules/vibration-manager"; import { BxLogger } from "@utils/bx-logger"; -import { hashCode } from "@utils/utils"; +import { hashCode, renderString } from "@utils/utils"; import { BxEvent } from "@/utils/bx-event"; +import codeControllerShortcuts from "./patches/controller-shortcuts.js" with { type: "text" }; import codeLocalCoOpEnable from "./patches/local-co-op-enable.js" with { type: "text" }; import codeRemotePlayEnable from "./patches/remote-play-enable.js" with { type: "text" }; import codeRemotePlayKeepAlive from "./patches/remote-play-keep-alive.js" with { type: "text" }; @@ -175,11 +176,10 @@ if (!!window.BX_REMOTE_PLAY_CONFIG) { const match = codeBlock.match(/this\.gamepadTimestamps\.set\((\w+)\.index/); if (match) { const gamepadVar = match[1]; - const newCode = ` -if (${gamepadVar}.buttons[17] && ${gamepadVar}.buttons[17].value === 1) { - window.dispatchEvent(new Event('${BxEvent.CAPTURE_SCREENSHOT}')); -} -`; + const newCode = renderString(codeControllerShortcuts, { + gamepadVar, + }); + codeBlock = codeBlock.replace('this.gamepadTimestamps.set', newCode + 'this.gamepadTimestamps.set'); } diff --git a/src/modules/patches/controller-shortcuts.js b/src/modules/patches/controller-shortcuts.js new file mode 100644 index 0000000..c8da989 --- /dev/null +++ b/src/modules/patches/controller-shortcuts.js @@ -0,0 +1,5 @@ +const currentGamepad = ${gamepadVar}; + +if (currentGamepad.buttons[17] && currentGamepad.buttons[17].value === 1) { + window.dispatchEvent(new Event(BxEvent.CAPTURE_SCREENSHOT)); +} diff --git a/src/utils/bx-event.ts b/src/utils/bx-event.ts index a30956b..a23a156 100644 --- a/src/utils/bx-event.ts +++ b/src/utils/bx-event.ts @@ -61,3 +61,5 @@ export namespace BxEvent { target.dispatchEvent(event); } } + +(window as any).BxEvent = BxEvent; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 56dcea2..1a498b7 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -65,3 +65,15 @@ export function hashCode(str: string): number { return hash; } + + +export function renderString(str: string, obj: any){ + return str.replace(/\$\{.+?\}/g, match => { + const key = match.substring(2, match.length - 1); + if (key in obj) { + return obj[key]; + } + + return match; + }); +}