Fadexz / Toggle Cloud Gaming Stream Render (Video Playback)

// ==UserScript==
// @name         Toggle Cloud Gaming Stream Render (Video Playback)
// @namespace    ToggleCloudGamingStreamRender
// @version      1.0.0
// @description  If you press the 'F2' key on your keyboard all video and audio on the page will pause and mute, useful if you don't want to render the video and audio (reduce CPU & GPU Load) but still want your controller input to go through
// @copyright    2024, Fadexz (https://openuserjs.org/users/Fadexz)
// @license      MIT
// @author       Fadexz
// @match        https://www.xbox.com/*/play/launch/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=xbox.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const TOGGLE_KEY = 'F2';

    var videos = document.getElementsByTagName("video");
    var audios = document.getElementsByTagName("audio");
    var isPaused = false;
    var i;

    // Toggle pause of video and audio on key press
    window.addEventListener("keydown", function(event) {
        if (event.key === TOGGLE_KEY) {
            for (i = 0; i < videos.length; ++i) {
                if (isPaused) {
                    videos[i].play();
                } else {
                    videos[i].pause();
                }
            }
            for (i = 0; i < audios.length; ++i) {
                if (isPaused) {
                    audios[i].muted = false;
                } else {
                    audios[i].muted = true;
                }
            }
            isPaused = !isPaused;
        }
    });

    // Pause video again when focus is returned to the tab (prevent unpause of video)
    window.addEventListener("focus", function() {
        if (isPaused) {
            for (i = 0; i < videos.length; ++i) {
                videos[i].pause();
            }
        }
    });

})();