Compare commits

..

6 Commits

2 changed files with 70 additions and 44 deletions

View File

@ -1,5 +1,5 @@
// ==UserScript==
// @name Better xCloud
// @namespace https://github.com/redphx
// @version 3.1.2
// @version 3.1.3
// ==/UserScript==

View File

@ -1,7 +1,7 @@
// ==UserScript==
// @name Better xCloud
// @namespace https://github.com/redphx
// @version 3.1.2
// @version 3.1.3
// @description Improve Xbox Cloud Gaming (xCloud) experience
// @author redphx
// @license MIT
@ -13,7 +13,7 @@
// ==/UserScript==
'use strict';
const SCRIPT_VERSION = '3.1.2';
const SCRIPT_VERSION = '3.1.3';
const SCRIPT_HOME = 'https://github.com/redphx/better-xcloud';
const ENABLE_XCLOUD_LOGGER = false;
@ -49,6 +49,10 @@ const BxEvent = {
STREAM_STARTED: 'bx-stream-started',
STREAM_PLAYING: 'bx-stream-playing',
STREAM_STOPPED: 'bx-stream-stopped',
STREAM_ERROR_PAGE: 'bx-stream-error-page',
STREAM_MENU_SHOWN: 'bx-stream-menu-shown',
STREAM_MENU_HIDDEN: 'bx-stream-menu-hidden',
STREAM_WEBRTC_CONNECTED: 'bx-stream-webrtc-connected',
STREAM_WEBRTC_DISCONNECTED: 'bx-stream-webrtc-disconnected',
@ -58,6 +62,11 @@ const BxEvent = {
DATA_CHANNEL_CREATED: 'bx-data-channel-created',
dispatch: (target, eventName, data) => {
if (!eventName) {
alert('BxEvent.dispatch(): eventName is null');
return;
}
const event = new Event(eventName);
if (data) {
@ -3431,14 +3440,12 @@ class TouchController {
}
static #show() {
document.querySelector('#BabylonCanvasContainer-main').parentElement.classList.remove('bx-gone');
// TouchController.loadCustomLayout(GAME_XBOX_TITLE_ID, TouchController.#currentLayoutId, 0);
document.querySelector('#BabylonCanvasContainer-main').parentElement.classList.remove('bx-offscreen');
TouchController.#showing = true;
}
static #hide() {
document.querySelector('#BabylonCanvasContainer-main').parentElement.classList.add('bx-gone');
// TouchController.#dispatchMessage(TouchController.#EVENT_HIDE_CONTROLLER);
document.querySelector('#BabylonCanvasContainer-main').parentElement.classList.add('bx-offscreen');
TouchController.#showing = false;
}
@ -3489,11 +3496,7 @@ class TouchController {
url += `${xboxTitleId}.json`;
}
NATIVE_FETCH(url)
.then(resp => resp.json(), () => {
TouchController.#customLayouts[xboxTitleId] = null;
// Wait for BX_EXPOSED.touch_layout_manager
setTimeout(() => dispatchLayouts(null), 1000);
})
.then(resp => resp.json())
.then(json => {
// Normalize data
const schema_version = json.schema_version || 1;
@ -3515,6 +3518,11 @@ class TouchController {
// Wait for BX_EXPOSED.touch_layout_manager
setTimeout(() => dispatchLayouts(json), 1000);
})
.catch(() => {
TouchController.#customLayouts[xboxTitleId] = null;
// Wait for BX_EXPOSED.touch_layout_manager
setTimeout(() => dispatchLayouts(null), 1000);
});
}
@ -4781,8 +4789,8 @@ class MkbHandler {
this.#$message.addEventListener('click', this.#onActivatePointerLock);
document.documentElement.appendChild(this.#$message);
window.addEventListener('bx-stream-menu-shown', this.#onStreamMenuShown);
window.addEventListener('bx-stream-menu-hidden', this.#onStreamMenuHidden);
window.addEventListener(BxEvent.STREAM_MENU_SHOWN, this.#onStreamMenuShown);
window.addEventListener(BxEvent.STREAM_MENU_HIDDEN, this.#onStreamMenuHidden);
this.#waitForPointerLock(true);
}
@ -4799,8 +4807,8 @@ class MkbHandler {
document.removeEventListener('pointerlockchange', this.#onPointerLockChange);
document.removeEventListener('pointerlockerror', this.#onPointerLockError);
window.removeEventListener('bx-stream-menu-shown', this.#onStreamMenuShown);
window.removeEventListener('bx-stream-menu-hidden', this.#onStreamMenuHidden);
window.removeEventListener(BxEvent.STREAM_MENU_SHOWN, this.#onStreamMenuShown);
window.removeEventListener(BxEvent.STREAM_MENU_HIDDEN, this.#onStreamMenuHidden);
}
start = () => {
@ -9790,13 +9798,13 @@ function injectStreamMenuButtons() {
}
item.removedNodes.forEach($node => {
if (!$node.className || !$node.className.startsWith) {
if (!$node || !$node.className || !$node.className.startsWith) {
return;
}
if ($node.className.startsWith('StreamMenu')) {
if (!document.querySelector('div[class^=PureInStreamConfirmationModal]')) {
window.dispatchEvent(new Event('bx-stream-menu-hidden'));
BxEvent.dispatch(window, BxEvent.STREAM_MENU_HIDDEN);
}
}
});
@ -9806,6 +9814,12 @@ function injectStreamMenuButtons() {
return;
}
// Error Page: .PureErrorPage.ErrorScreen
if ($node.className.includes('PureErrorPage')) {
BxEvent.dispatch(window, BxEvent.STREAM_ERROR_PAGE);
return;
}
if (PREF_DISABLE_FEEDBACK_DIALOG && $node.className.startsWith('PostStreamFeedbackScreen')) {
const $btnClose = $node.querySelector('button');
$btnClose && $btnClose.click();
@ -9814,7 +9828,7 @@ function injectStreamMenuButtons() {
// Render badges
if ($node.className.startsWith('StreamMenu')) {
window.dispatchEvent(new Event('bx-stream-menu-shown'));
BxEvent.dispatch(window, BxEvent.STREAM_MENU_SHOWN);
// Hide Quick bar when closing HUD
const $btnCloseHud = document.querySelector('button[class*=StreamMenu-module__backButton]');
@ -10456,14 +10470,10 @@ function patchHistoryMethod(type) {
function onHistoryChanged(e) {
if (e.arguments && e.arguments[0] && e.arguments[0].origin === 'better-xcloud') {
if (e && e.arguments && e.arguments[0] && e.arguments[0].origin === 'better-xcloud') {
return;
}
// Stop MKB listeners
MkbHandler.INSTANCE.destroy();
IS_PLAYING = false;
setTimeout(RemotePlay.detect, 10);
const $settings = document.querySelector('.better_xcloud_settings');
@ -10471,28 +10481,10 @@ function onHistoryChanged(e) {
$settings.classList.add('bx-gone');
}
const $quickBar = document.querySelector('.bx-quick-settings-bar');
if ($quickBar) {
$quickBar.classList.add('bx-gone');
}
STREAM_AUDIO_GAIN_NODE = null;
$STREAM_VIDEO = null;
StreamStats.onStoppedPlaying();
const $screenshotBtn = document.querySelector('.bx-screenshot-button');
if ($screenshotBtn) {
$screenshotBtn.style = '';
}
MouseCursorHider.stop();
TouchController.reset();
LoadingScreen.reset();
GamepadHandler.stopPolling();
setTimeout(checkHeader, 2000);
BxEvent.dispatch(window, BxEvent.STREAM_STOPPED);
}
@ -10587,6 +10579,40 @@ window.addEventListener(BxEvent.STREAM_PLAYING, e => {
}
});
window.addEventListener(BxEvent.STREAM_ERROR_PAGE, e => {
BxEvent.dispatch(window, BxEvent.STREAM_STOPPED);
});
window.addEventListener(BxEvent.STREAM_STOPPED, e => {
if (!IS_PLAYING) {
return;
}
IS_PLAYING = false;
// Stop MKB listeners
MkbHandler.INSTANCE.destroy();
const $quickBar = document.querySelector('.bx-quick-settings-bar');
if ($quickBar) {
$quickBar.classList.add('bx-gone');
}
STREAM_AUDIO_GAIN_NODE = null;
$STREAM_VIDEO = null;
StreamStats.onStoppedPlaying();
const $screenshotBtn = document.querySelector('.bx-screenshot-button');
if ($screenshotBtn) {
$screenshotBtn.style = '';
}
MouseCursorHider.stop();
TouchController.reset();
GamepadHandler.stopPolling();
});
PreloadedState.override();