Twitch - Disable automatic video downscale

Disables the automatic downscaling of Twitch streams while tabbed away

26.03.2025 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Twitch - Disable automatic video downscale
// @namespace    CommanderRoot
// @copyright    CommanderRoot
// @license      Unlicense
// @version      1.2.4
// @description  Disables the automatic downscaling of Twitch streams while tabbed away
// @author       https://twitter.com/CommanderRoot
// @match        https://www.twitch.tv/*
// @match        https://m.twitch.tv/*
// @match        https://player.twitch.tv/*
// @grant        none
// @run-at       document-start
// ==/UserScript==
"use strict";

// Try to trick the site into thinking it's never hidden
Object.defineProperty(document, 'visibilityState', { value: 'visible', writable: false });
Object.defineProperty(document, 'webkitVisibilityState', { value: 'visible', writable: false });
document.hasFocus = function () { return true; };
document.dispatchEvent(new Event('visibilitychange'));
const initialHidden = document.hidden;
let didInitialPlay = false;
let lastVideoPlaying = null;

// visibilitychange events are captured and stopped
document.addEventListener('visibilitychange', function (e) {
  if (typeof chrome !== 'undefined' && initialHidden === true && didInitialPlay === false) {
    // Allow propagation to prevent black screen on Chrome when a stream was opened in a new tab
  } else {
    e.stopImmediatePropagation();
  }

  // Try to play the video on Chrome
  if (typeof chrome !== 'undefined') {
    if (document.hidden) {
      const videos = document.getElementsByTagName('video');
      if (videos.length > 0) {
        lastVideoPlaying = !videos[0].paused && !videos[0].ended;
      } else {
        lastVideoPlaying = false;
      }
    } else {
      playVideo();
    }
  }
}, true);

function setQualitySettings() {
  // Set the player quality to "Source"
  try {
    window.localStorage.setItem('s-qs-ts', Math.floor(Date.now()));
    window.localStorage.setItem('video-quality', '{"default":"chunked"}');
  } catch (e) {
    console.log(e);
  }
}

function playVideo() {
  const videos = document.getElementsByTagName('video');
  if (videos.length > 0) {
    if ((didInitialPlay === false || lastVideoPlaying === true) && !videos[0].ended) {
      videos[0].play();
      didInitialPlay = true;
    }
  }
}

setQualitySettings();

// Add event handler for when we switch between pages
// This is useful when we switch for example from a Clip
// without "Source" to a livestream
window.addEventListener('popstate', () => {
  setQualitySettings();
});