From 53676ad7330f3c41fc9f44758ade11ac1450478d Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:46:02 +0700 Subject: [PATCH] Fix errors with setTimeout/setInterval --- src/index.ts | 6 +++--- src/modules/loading-screen.ts | 2 +- src/modules/mkb/mkb-handler.ts | 4 ++-- src/modules/mkb/mkb-remapper.ts | 6 +++--- src/modules/mkb/mouse-cursor-hider.ts | 2 +- src/modules/screenshot.ts | 4 ++-- src/modules/settings.ts | 4 ++-- src/modules/stream/stream-badges.ts | 2 +- src/modules/stream/stream-stats.ts | 2 +- src/modules/stream/stream-ui.ts | 4 ++-- src/modules/touch-controller.ts | 12 ++++++------ src/modules/ui/header.ts | 2 +- src/modules/ui/ui.ts | 2 +- src/utils/history.ts | 4 ++-- src/utils/network.ts | 2 +- src/utils/toast.ts | 2 +- 16 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/index.ts b/src/index.ts index da4d359..ef3e8c6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,7 +33,7 @@ import { injectStreamMenuButtons } from "./modules/stream/stream-ui"; // Handle login page if (window.location.pathname.includes('/auth/msa')) { window.addEventListener('load', e => { - window.location.search.includes('loggedIn') && setTimeout(() => { + window.location.search.includes('loggedIn') && window.setTimeout(() => { const location = window.location; // @ts-ignore location.pathname.includes('/play') && location.reload(true); @@ -81,7 +81,7 @@ if (BX_FLAGS.SafariWorkaround && document.readyState !== 'loading') { // Automatically reload the page when running into the "We are sorry..." error message window.addEventListener('load', e => { - setTimeout(() => { + window.setTimeout(() => { if (document.body.classList.contains('legacyBackground')) { // Has error message -> reload page window.stop(); @@ -105,7 +105,7 @@ window.history.replaceState = patchHistoryMethod('replaceState'); window.addEventListener(BxEvent.XCLOUD_SERVERS_READY, e => { // Start rendering UI if (document.querySelector('div[class^=UnsupportedMarketPage]')) { - setTimeout(watchHeader, 2000); + window.setTimeout(watchHeader, 2000); } else { watchHeader(); } diff --git a/src/modules/loading-screen.ts b/src/modules/loading-screen.ts index 7d5bee6..5658b7e 100644 --- a/src/modules/loading-screen.ts +++ b/src/modules/loading-screen.ts @@ -135,7 +135,7 @@ export class LoadingScreen { $countDown.textContent = LoadingScreen.#secondsToString(secondsLeft); document.title = `[${$countDown.textContent}] ${LoadingScreen.#orgWebTitle}`; - LoadingScreen.#waitTimeInterval = setInterval(() => { + LoadingScreen.#waitTimeInterval = window.setInterval(() => { secondsLeft--; $countDown.textContent = LoadingScreen.#secondsToString(secondsLeft); document.title = `[${$countDown.textContent}] ${LoadingScreen.#orgWebTitle}`; diff --git a/src/modules/mkb/mkb-handler.ts b/src/modules/mkb/mkb-handler.ts index 0d6b3a5..6f58da2 100644 --- a/src/modules/mkb/mkb-handler.ts +++ b/src/modules/mkb/mkb-handler.ts @@ -214,7 +214,7 @@ export class MkbHandler { this.#pressButton(buttonIndex, true); } - this.#wheelStoppedTimeout = setTimeout(() => { + this.#wheelStoppedTimeout = window.setTimeout(() => { this.#prevWheelCode = null; this.#pressButton(buttonIndex, false); }, 20); @@ -272,7 +272,7 @@ export class MkbHandler { this.#allowStickDecaying = false; this.#detectMouseStoppedTimeout && clearTimeout(this.#detectMouseStoppedTimeout); - this.#detectMouseStoppedTimeout = setTimeout(this.#onMouseStopped.bind(this), 100); + this.#detectMouseStoppedTimeout = window.setTimeout(this.#onMouseStopped.bind(this), 100); const deltaX = e.movementX; const deltaY = e.movementY; diff --git a/src/modules/mkb/mkb-remapper.ts b/src/modules/mkb/mkb-remapper.ts index 14c5557..4f0a2e2 100644 --- a/src/modules/mkb/mkb-remapper.ts +++ b/src/modules/mkb/mkb-remapper.ts @@ -151,7 +151,7 @@ export class MkbRemapper { this.#clearEventListeners(); this.#bindKey(this.#$.currentBindingKey!, KeyHelper.getKeyFromEvent(e)); - setTimeout(() => this.bindingDialog.hide(), 200); + window.setTimeout(() => this.bindingDialog.hide(), 200); }; #onMouseDown = (e: MouseEvent) => { @@ -159,7 +159,7 @@ export class MkbRemapper { this.#clearEventListeners(); this.#bindKey(this.#$.currentBindingKey!, KeyHelper.getKeyFromEvent(e)); - setTimeout(() => this.bindingDialog.hide(), 200); + window.setTimeout(() => this.bindingDialog.hide(), 200); }; #onKeyDown = (e: KeyboardEvent) => { @@ -171,7 +171,7 @@ export class MkbRemapper { this.#bindKey(this.#$.currentBindingKey!, KeyHelper.getKeyFromEvent(e)); } - setTimeout(() => this.bindingDialog.hide(), 200); + window.setTimeout(() => this.bindingDialog.hide(), 200); }; #onBindingKey = (e: MouseEvent) => { diff --git a/src/modules/mkb/mouse-cursor-hider.ts b/src/modules/mkb/mouse-cursor-hider.ts index 3e05933..596d6db 100644 --- a/src/modules/mkb/mouse-cursor-hider.ts +++ b/src/modules/mkb/mouse-cursor-hider.ts @@ -18,7 +18,7 @@ export class MouseCursorHider { !MouseCursorHider.#cursorVisible && MouseCursorHider.show(); // Setup timeout MouseCursorHider.#timeout && clearTimeout(MouseCursorHider.#timeout); - MouseCursorHider.#timeout = setTimeout(MouseCursorHider.hide, 3000); + MouseCursorHider.#timeout = window.setTimeout(MouseCursorHider.hide, 3000); } static start() { diff --git a/src/modules/screenshot.ts b/src/modules/screenshot.ts index 980454f..aa658b4 100644 --- a/src/modules/screenshot.ts +++ b/src/modules/screenshot.ts @@ -67,7 +67,7 @@ export function setupScreenshotButton() { takeScreenshot(() => { // Hide button $btn.setAttribute('data-showing', 'false'); - setTimeout(() => { + window.setTimeout(() => { if (!timeout) { $btn.setAttribute('data-capturing', 'false'); } @@ -84,7 +84,7 @@ export function setupScreenshotButton() { $btn.setAttribute('data-capturing', 'false'); timeout && clearTimeout(timeout); - timeout = setTimeout(() => { + timeout = window.setTimeout(() => { timeout = null; $btn.setAttribute('data-showing', 'false'); $btn.setAttribute('data-capturing', 'false'); diff --git a/src/modules/settings.ts b/src/modules/settings.ts index 495aff0..5275d74 100644 --- a/src/modules/settings.ts +++ b/src/modules/settings.ts @@ -76,7 +76,7 @@ export class SettingElement { $control.addEventListener('mousedown', function(e) { const self = this; const orgScrollTop = self.scrollTop; - setTimeout(() => (self.scrollTop = orgScrollTop), 0); + window.setTimeout(() => (self.scrollTop = orgScrollTop), 0); }); $control.addEventListener('mousemove', e => e.preventDefault()); @@ -209,7 +209,7 @@ export class SettingElement { isHolding = true; const args = arguments; - interval = setInterval(() => { + interval = window.setInterval(() => { const event = new Event('click'); (event as any).arguments = args; diff --git a/src/modules/stream/stream-badges.ts b/src/modules/stream/stream-badges.ts index 0c55444..2d97a6d 100644 --- a/src/modules/stream/stream-badges.ts +++ b/src/modules/stream/stream-badges.ts @@ -215,7 +215,7 @@ export class StreamBadges { await StreamBadges.#updateBadges(true); StreamBadges.#stop(); - StreamBadges.#interval = setInterval(StreamBadges.#updateBadges, StreamBadges.#REFRESH_INTERVAL); + StreamBadges.#interval = window.setInterval(StreamBadges.#updateBadges, StreamBadges.#REFRESH_INTERVAL); return $wrapper; } diff --git a/src/modules/stream/stream-stats.ts b/src/modules/stream/stream-stats.ts index 51b7e92..8a341ec 100644 --- a/src/modules/stream/stream-stats.ts +++ b/src/modules/stream/stream-stats.ts @@ -39,7 +39,7 @@ export class StreamStats { StreamStats.#$container.classList.remove('bx-gone'); StreamStats.#$container.setAttribute('data-display', glancing ? 'glancing' : 'fixed'); - StreamStats.#interval = setInterval(StreamStats.update, StreamStats.#updateInterval); + StreamStats.#interval = window.setInterval(StreamStats.update, StreamStats.#updateInterval); } static stop(glancing=false) { diff --git a/src/modules/stream/stream-ui.ts b/src/modules/stream/stream-ui.ts index 39ae0f7..7c41c42 100644 --- a/src/modules/stream/stream-ui.ts +++ b/src/modules/stream/stream-ui.ts @@ -20,7 +20,7 @@ class MouseHoldEvent { this.#isHolding = false; this.#timeout && clearTimeout(this.#timeout); - this.#timeout = setTimeout(() => { + this.#timeout = window.setTimeout(() => { _this.#isHolding = true; _this.#callback(); }, this.#duration); @@ -87,7 +87,7 @@ function cloneStreamHudButton($orgButton: HTMLElement, label: string, svgIcon: I const left = document.getElementById('StreamHud')?.style.left; if (left === '0px') { timeout && clearTimeout(timeout); - timeout = setTimeout(() => { + timeout = window.setTimeout(() => { $container.style.pointerEvents = 'auto'; }, 100); } diff --git a/src/modules/touch-controller.ts b/src/modules/touch-controller.ts index 3d55391..97e6c26 100644 --- a/src/modules/touch-controller.ts +++ b/src/modules/touch-controller.ts @@ -79,7 +79,7 @@ export class TouchController { } static #dispatchMessage(msg: any) { - TouchController.#dataChannel && setTimeout(() => { + TouchController.#dataChannel && window.setTimeout(() => { TouchController.#dataChannel!.dispatchEvent(msg); }, 10); } @@ -100,7 +100,7 @@ export class TouchController { if (retries > 2) { TouchController.#customLayouts[xboxTitleId] = null; // Wait for BX_EXPOSED.touch_layout_manager - setTimeout(() => TouchController.#dispatchLayouts(null), 1000); + window.setTimeout(() => TouchController.#dispatchLayouts(null), 1000); return; } @@ -136,7 +136,7 @@ export class TouchController { TouchController.#customLayouts[xboxTitleId] = json; // Wait for BX_EXPOSED.touch_layout_manager - setTimeout(() => TouchController.#dispatchLayouts(json), 1000); + window.setTimeout(() => TouchController.#dispatchLayouts(json), 1000); } catch (e) { // Retry TouchController.getCustomLayouts(xboxTitleId, retries + 1); @@ -166,7 +166,7 @@ export class TouchController { // Show a toast with layout's name layoutChanged && Toast.show(t('touch-control-layout'), layout.name); - setTimeout(() => { + window.setTimeout(() => { window.BX_EXPOSED.touch_layout_manager.changeLayoutForScope({ type: 'showLayout', scope: xboxTitleId, @@ -221,7 +221,7 @@ export class TouchController { return; } - clickTimeout = setTimeout(() => { + clickTimeout = window.setTimeout(() => { clickTimeout = null; }, 400); }); @@ -260,7 +260,7 @@ export class TouchController { // Fix sometimes the touch controller doesn't show at the beginning dataChannel.addEventListener('open', () => { - setTimeout(TouchController.#show, 1000); + window.setTimeout(TouchController.#show, 1000); }); let focused = false; diff --git a/src/modules/ui/header.ts b/src/modules/ui/header.ts index 0b5d270..e3ba6cc 100644 --- a/src/modules/ui/header.ts +++ b/src/modules/ui/header.ts @@ -76,7 +76,7 @@ export function watchHeader() { let timeout: number | null; const observer = new MutationObserver(mutationList => { timeout && clearTimeout(timeout); - timeout = setTimeout(checkHeader, 2000); + timeout = window.setTimeout(checkHeader, 2000); }); observer.observe($header, {subtree: true, childList: true}); diff --git a/src/modules/ui/ui.ts b/src/modules/ui/ui.ts index 019c51b..6ce7502 100644 --- a/src/modules/ui/ui.ts +++ b/src/modules/ui/ui.ts @@ -25,7 +25,7 @@ export function localRedirect(path: string) { }, ''); $anchor.addEventListener('click', e => { // Remove element after clicking on it - setTimeout(() => { + window.setTimeout(() => { $pageContent.removeChild($anchor); }, 1000); }); diff --git a/src/utils/history.ts b/src/utils/history.ts index bbed3c5..2fb67b9 100644 --- a/src/utils/history.ts +++ b/src/utils/history.ts @@ -23,7 +23,7 @@ export function onHistoryChanged(e: PopStateEvent) { return; } - setTimeout(RemotePlay.detect, 10); + window.setTimeout(RemotePlay.detect, 10); const $settings = document.querySelector('.bx-settings-container'); if ($settings) { @@ -34,7 +34,7 @@ export function onHistoryChanged(e: PopStateEvent) { RemotePlay.detachPopup(); LoadingScreen.reset(); - setTimeout(checkHeader, 2000); + window.setTimeout(checkHeader, 2000); BxEvent.dispatch(window, BxEvent.STREAM_STOPPED); } diff --git a/src/utils/network.ts b/src/utils/network.ts index d8046ac..310598c 100644 --- a/src/utils/network.ts +++ b/src/utils/network.ts @@ -543,7 +543,7 @@ export function interceptHttpRequests() { for (const blocked of BLOCKED_URLS) { if ((this as any)._url.startsWith(blocked)) { if (blocked === 'https://dc.services.visualstudio.com') { - setTimeout(clearAllLogs, 1000); + window.setTimeout(clearAllLogs, 1000); } return false; } diff --git a/src/utils/toast.ts b/src/utils/toast.ts index 3c8dfbc..55108bd 100644 --- a/src/utils/toast.ts +++ b/src/utils/toast.ts @@ -37,7 +37,7 @@ export class Toast { Toast.#isShowing = true; Toast.#timeout && clearTimeout(Toast.#timeout); - Toast.#timeout = setTimeout(Toast.#hide, Toast.#DURATION); + Toast.#timeout = window.setTimeout(Toast.#hide, Toast.#DURATION); // Get values from item const [msg, status, _] = Toast.#stack.shift()!;