Refactor header.ts

This commit is contained in:
redphx 2024-07-13 18:00:15 +07:00
parent 66d5d9edc6
commit 5bfcf3a044
5 changed files with 73 additions and 65 deletions

View File

@ -16,7 +16,6 @@ import { PrefKey, getPref } from "@utils/preferences";
import { LoadingScreen } from "@modules/loading-screen"; import { LoadingScreen } from "@modules/loading-screen";
import { MouseCursorHider } from "@modules/mkb/mouse-cursor-hider"; import { MouseCursorHider } from "@modules/mkb/mouse-cursor-hider";
import { TouchController } from "@modules/touch-controller"; import { TouchController } from "@modules/touch-controller";
import { watchHeader } from "@modules/ui/header";
import { checkForUpdate, disablePwa } from "@utils/utils"; import { checkForUpdate, disablePwa } from "@utils/utils";
import { Patcher } from "@modules/patcher"; import { Patcher } from "@modules/patcher";
import { RemotePlay } from "@modules/remote-play"; import { RemotePlay } from "@modules/remote-play";
@ -34,6 +33,7 @@ import { GuideMenu, GuideMenuTab } from "./modules/ui/guide-menu";
import { StreamSettings } from "./modules/stream/stream-settings"; import { StreamSettings } from "./modules/stream/stream-settings";
import { updateVideoPlayer } from "./modules/stream/stream-settings-utils"; import { updateVideoPlayer } from "./modules/stream/stream-settings-utils";
import { UiSection } from "./enums/ui-sections"; import { UiSection } from "./enums/ui-sections";
import { HeaderSection } from "./modules/ui/header";
// Handle login page // Handle login page
@ -127,11 +127,11 @@ window.history.replaceState = patchHistoryMethod('replaceState');
window.addEventListener(BxEvent.XCLOUD_SERVERS_UNAVAILABLE, e => { window.addEventListener(BxEvent.XCLOUD_SERVERS_UNAVAILABLE, e => {
STATES.supportedRegion = false; STATES.supportedRegion = false;
window.setTimeout(watchHeader, 2000); window.setTimeout(HeaderSection.watchHeader, 2000);
}); });
window.addEventListener(BxEvent.XCLOUD_SERVERS_READY, e => { window.addEventListener(BxEvent.XCLOUD_SERVERS_READY, e => {
watchHeader(); HeaderSection.watchHeader();
}); });
window.addEventListener(BxEvent.STREAM_LOADING, e => { window.addEventListener(BxEvent.STREAM_LOADING, e => {
@ -348,6 +348,9 @@ function main() {
STATES.pointerServerPort = AppInterface.startPointerServer() || 9269; STATES.pointerServerPort = AppInterface.startPointerServer() || 9269;
BxLogger.info('startPointerServer', 'Port', STATES.pointerServerPort.toString()); BxLogger.info('startPointerServer', 'Port', STATES.pointerServerPort.toString());
} }
// Preload Remote Play
getPref(PrefKey.REMOTE_PLAY_ENABLED) && BX_FLAGS.PreloadRemotePlay && RemotePlay.preload();
} }
main(); main();

View File

@ -7,6 +7,7 @@ import { getPref, PrefKey, setPref } from "@utils/preferences";
import { t } from "@utils/translation"; import { t } from "@utils/translation";
import { localRedirect } from "@modules/ui/ui"; import { localRedirect } from "@modules/ui/ui";
import { BxLogger } from "@utils/bx-logger"; import { BxLogger } from "@utils/bx-logger";
import { HeaderSection } from "./ui/header";
const LOG_TAG = 'RemotePlay'; const LOG_TAG = 'RemotePlay';
@ -97,6 +98,10 @@ export class RemotePlay {
RemotePlay.#getXhomeToken(() => { RemotePlay.#getXhomeToken(() => {
RemotePlay.#getConsolesList(() => { RemotePlay.#getConsolesList(() => {
BxLogger.info(LOG_TAG, 'Consoles', RemotePlay.#CONSOLES); BxLogger.info(LOG_TAG, 'Consoles', RemotePlay.#CONSOLES);
if (RemotePlay.#CONSOLES && RemotePlay.#CONSOLES.length > 0) {
STATES.supportedRegion && HeaderSection.showRemotePlayButton();
}
RemotePlay.#renderConsoles(); RemotePlay.#renderConsoles();
BxEvent.dispatch(window, BxEvent.REMOTE_PLAY_READY); BxEvent.dispatch(window, BxEvent.REMOTE_PLAY_READY);
}); });

View File

@ -1,5 +1,5 @@
import { SCRIPT_VERSION } from "@utils/global"; import { SCRIPT_VERSION } from "@utils/global";
import { createButton, ButtonStyle } from "@utils/html"; import { createButton, ButtonStyle, CE } from "@utils/html";
import { BxIcon } from "@utils/bx-icon"; import { BxIcon } from "@utils/bx-icon";
import { getPreferredServerRegion } from "@utils/region"; import { getPreferredServerRegion } from "@utils/region";
import { PrefKey, getPref } from "@utils/preferences"; import { PrefKey, getPref } from "@utils/preferences";
@ -7,35 +7,20 @@ import { RemotePlay } from "@modules/remote-play";
import { t } from "@utils/translation"; import { t } from "@utils/translation";
import { setupSettingsUi } from "./global-settings"; import { setupSettingsUi } from "./global-settings";
export class HeaderSection {
static #$remotePlayBtn = createButton({
classes: ['bx-header-remote-play-button', 'bx-gone'],
icon: BxIcon.REMOTE_PLAY,
title: t('remote-play'),
style: ButtonStyle.GHOST | ButtonStyle.FOCUSABLE,
onClick: e => {
RemotePlay.togglePopup();
},
});
function injectSettingsButton($parent?: HTMLElement) { static #$settingsBtn = createButton({
if (!$parent) {
return;
}
const PREF_PREFERRED_REGION = getPreferredServerRegion(true);
const PREF_LATEST_VERSION = getPref(PrefKey.LATEST_VERSION);
const $headerFragment = document.createDocumentFragment();
// Remote Play button
if (getPref(PrefKey.REMOTE_PLAY_ENABLED)) {
const $remotePlayBtn = createButton({
classes: ['bx-header-remote-play-button'],
icon: BxIcon.REMOTE_PLAY,
title: t('remote-play'),
style: ButtonStyle.GHOST | ButtonStyle.FOCUSABLE,
onClick: e => {
RemotePlay.togglePopup();
},
});
$headerFragment.appendChild($remotePlayBtn);
}
// Setup Settings button
const $settingsBtn = createButton({
classes: ['bx-header-settings-button'], classes: ['bx-header-settings-button'],
label: PREF_PREFERRED_REGION, label: '???',
style: ButtonStyle.GHOST | ButtonStyle.FOCUSABLE | ButtonStyle.FULL_HEIGHT, style: ButtonStyle.GHOST | ButtonStyle.FOCUSABLE | ButtonStyle.FULL_HEIGHT,
onClick: e => { onClick: e => {
setupSettingsUi(); setupSettingsUi();
@ -47,39 +32,57 @@ function injectSettingsButton($parent?: HTMLElement) {
}, },
}); });
// Show new update status static #$buttonsWrapper = CE('div', {},
if (!SCRIPT_VERSION.includes('beta') && PREF_LATEST_VERSION && PREF_LATEST_VERSION !== SCRIPT_VERSION) { getPref(PrefKey.REMOTE_PLAY_ENABLED) ? HeaderSection.#$remotePlayBtn : null,
$settingsBtn.setAttribute('data-update-available', 'true'); HeaderSection.#$settingsBtn,
);
static #injectSettingsButton($parent?: HTMLElement) {
if (!$parent) {
return;
}
const PREF_LATEST_VERSION = getPref(PrefKey.LATEST_VERSION);
// Setup Settings button
const $settingsBtn = HeaderSection.#$settingsBtn;
$settingsBtn.querySelector('span')!.textContent = getPreferredServerRegion(true);
// Show new update status
if (!SCRIPT_VERSION.includes('beta') && PREF_LATEST_VERSION && PREF_LATEST_VERSION !== SCRIPT_VERSION) {
$settingsBtn.setAttribute('data-update-available', 'true');
}
// Add the Settings button to the web page
$parent.appendChild(HeaderSection.#$buttonsWrapper);
} }
// Add the Settings button to the web page static checkHeader() {
$headerFragment.appendChild($settingsBtn); const $button = document.querySelector('.bx-header-settings-button');
$parent.appendChild($headerFragment);
}
if (!$button) {
export function checkHeader() { const $rightHeader = document.querySelector('#PageContent div[class*=EdgewaterHeader-module__rightSectionSpacing]');
const $button = document.querySelector('.bx-header-settings-button'); HeaderSection.#injectSettingsButton($rightHeader as HTMLElement);
}
if (!$button) {
const $rightHeader = document.querySelector('#PageContent div[class*=EdgewaterHeader-module__rightSectionSpacing]');
injectSettingsButton($rightHeader as HTMLElement);
}
}
export function watchHeader() {
const $header = document.querySelector('#PageContent header');
if (!$header) {
return;
} }
let timeout: number | null; static showRemotePlayButton() {
const observer = new MutationObserver(mutationList => { HeaderSection.#$remotePlayBtn.classList.remove('bx-gone');
timeout && clearTimeout(timeout); }
timeout = window.setTimeout(checkHeader, 2000);
});
observer.observe($header, {subtree: true, childList: true});
checkHeader(); static watchHeader() {
const $header = document.querySelector('#PageContent header');
if (!$header) {
return;
}
let timeout: number | null;
const observer = new MutationObserver(mutationList => {
timeout && clearTimeout(timeout);
timeout = window.setTimeout(HeaderSection.checkHeader, 2000);
});
observer.observe($header, {subtree: true, childList: true});
HeaderSection.checkHeader();
}
} }

View File

@ -1,7 +1,7 @@
import { BxEvent } from "@utils/bx-event"; import { BxEvent } from "@utils/bx-event";
import { LoadingScreen } from "@modules/loading-screen"; import { LoadingScreen } from "@modules/loading-screen";
import { RemotePlay } from "@modules/remote-play"; import { RemotePlay } from "@modules/remote-play";
import { checkHeader } from "@modules/ui/header"; import { HeaderSection } from "@/modules/ui/header";
export function patchHistoryMethod(type: 'pushState' | 'replaceState') { export function patchHistoryMethod(type: 'pushState' | 'replaceState') {
const orig = window.history[type]; const orig = window.history[type];
@ -34,7 +34,7 @@ export function onHistoryChanged(e: PopStateEvent) {
RemotePlay.detachPopup(); RemotePlay.detachPopup();
LoadingScreen.reset(); LoadingScreen.reset();
window.setTimeout(checkHeader, 2000); window.setTimeout(HeaderSection.watchHeader, 2000);
BxEvent.dispatch(window, BxEvent.STREAM_STOPPED); BxEvent.dispatch(window, BxEvent.STREAM_STOPPED);
} }

View File

@ -25,9 +25,6 @@ class XcloudInterceptor {
const obj = await response.clone().json(); const obj = await response.clone().json();
// Preload Remote Play
getPref(PrefKey.REMOTE_PLAY_ENABLED) && BX_FLAGS.PreloadRemotePlay && RemotePlay.preload();
// Store xCloud token // Store xCloud token
RemotePlay.XCLOUD_TOKEN = obj.gsToken; RemotePlay.XCLOUD_TOKEN = obj.gsToken;