Twitch - Fullscreen Optimized

This resolves the frame drops and stuttering issues when watching Twitch in full screen on low-end GPUs.解决低端gpu观看twitch全屏时的掉帧、卡顿问题。

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Twitch - Fullscreen Optimized
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  This resolves the frame drops and stuttering issues when watching Twitch in full screen on low-end GPUs.解决低端gpu观看twitch全屏时的掉帧、卡顿问题。
// @author       Martin______X
// @match        https://www.twitch.tv/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=twitch.tv
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    "use strict";

    // Fullscreen Switch
    function togglePureFullscreen() {
        const player = document.querySelector(".video-player");
        if (!player) return;
        if (!document.fullscreenElement) {
            player.requestFullscreen().catch(e => {});
        } else {
            document.exitFullscreen().catch(e => {});
        }
    }

    // Intercept fullscreen button & double click event.
    const mouseEvents = ['click', 'dblclick'];
    mouseEvents.forEach(type => {
        document.addEventListener(type, (e) => {
            const isBtn = e.target.closest('[data-a-target="player-fullscreen-button"]');
            const isVid = type === 'dblclick' && e.target.closest('.video-player__container');

            if (isBtn || isVid) {
                e.stopImmediatePropagation(); // Stop twitch
                e.preventDefault();
                togglePureFullscreen();
            }
        }, true); // switch fullscreen mode before Twitch.
    });

    // Intercept shortcut F key.
    document.addEventListener("keydown", (e) => {
        const isTyping = ["INPUT", "TEXTAREA"].includes(document.activeElement.tagName) || document.activeElement.isContentEditable;
        if (!isTyping && e.key.toLowerCase() === 'f') {
            e.stopImmediatePropagation();
            e.preventDefault();
            togglePureFullscreen();
        }
    }, true);

    // override css to protect render performance
    const style = document.createElement("style");
    style.textContent = `
            :fullscreen .video-player,
            :fullscreen .video-player__container,
            :fullscreen .channel-root__player-container,
            :fullscreen .channel-root__player,
            :fullscreen body,
            :fullscreen html {
                      height: 100% !important;
                      max-height: 100% !important;
                      overflow: hidden !important;
            }
    `;
    document.head.appendChild(style);
})();