Twitch - Disable automatic video downscale

Disables the automatic downscaling of Twitch streams while tabbed away

  1. // ==UserScript==
  2. // @name Twitch - Disable automatic video downscale
  3. // @namespace CommanderRoot
  4. // @copyright CommanderRoot
  5. // @license Unlicense
  6. // @version 1.2.7
  7. // @description Disables the automatic downscaling of Twitch streams while tabbed away
  8. // @author https://twitter.com/CommanderRoot
  9. // @match https://www.twitch.tv/*
  10. // @match https://m.twitch.tv/*
  11. // @match https://player.twitch.tv/*
  12. // @grant none
  13. // @run-at document-start
  14. // ==/UserScript==
  15. "use strict";
  16.  
  17.  
  18. // CONFIG start ------
  19. const doOnlySetting = false; // false = do some trickery with document hidden state / true = only set the localStorage option
  20. // CONFIG end --------
  21.  
  22.  
  23. // Code
  24. if (doOnlySetting === false) {
  25. // Try to trick the site into thinking it's never hidden
  26. Object.defineProperty(document, 'visibilityState', { value: 'visible', writable: false });
  27. Object.defineProperty(document, 'webkitVisibilityState', { value: 'visible', writable: false });
  28. document.hasFocus = function () { return true; };
  29. const initialHidden = document.hidden;
  30. let didInitialPlay = false;
  31. let lastVideoPlaying = false;
  32.  
  33. // visibilitychange events are captured and stopped
  34. document.addEventListener('visibilitychange', function (e) {
  35. if (document.hidden === false && initialHidden === true && didInitialPlay === false) {
  36. // Allow propagation to prevent black screen when a stream was opened in a new tab
  37. } else {
  38. e.stopImmediatePropagation();
  39. }
  40. if (document.hidden) {
  41. didInitialPlay = true;
  42. }
  43.  
  44. // Try to play the video on Chrome
  45. if (typeof chrome !== 'undefined') {
  46. if (document.hidden === true) {
  47. const videos = document.getElementsByTagName('video');
  48. if (videos.length > 0) {
  49. lastVideoPlaying = !videos[0].paused && !videos[0].ended;
  50. } else {
  51. lastVideoPlaying = false;
  52. }
  53. } else {
  54. playVideo();
  55. }
  56. }
  57. }, true);
  58.  
  59. function playVideo() {
  60. const videos = document.getElementsByTagName('video');
  61. if (videos.length > 0) {
  62. if ((didInitialPlay === false || lastVideoPlaying === true) && !videos[0].ended) {
  63. videos[0].play();
  64. didInitialPlay = true;
  65. }
  66. }
  67. }
  68. }
  69.  
  70. function setQualitySettings() {
  71. // Set the player quality to "Source"
  72. try {
  73. window.localStorage.setItem('s-qs-ts', Math.floor(Date.now()));
  74. window.localStorage.setItem('video-quality', '{"default":"chunked"}');
  75. } catch (e) {
  76. console.log(e);
  77. }
  78. }
  79.  
  80. setQualitySettings();
  81.  
  82. // Add event handler for when we switch between pages
  83. // This is useful when we switch for example from a Clip
  84. // without "Source" to a livestream
  85. window.addEventListener('popstate', () => {
  86. setQualitySettings();
  87. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址