Twitch - Disable automatic video downscale

Disables the automatic downscaling of Twitch streams while tabbed away

目前为 2025-03-20 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Twitch - Disable automatic video downscale
  3. // @namespace CommanderRoot
  4. // @copyright CommanderRoot
  5. // @license Unlicense
  6. // @version 1.2.3
  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. // Try to trick the site into thinking it's never hidden
  18. Object.defineProperty(document, 'visibilityState', { value: 'visible', writable: false });
  19. Object.defineProperty(document, 'webkitVisibilityState', { value: 'visible', writable: false });
  20. document.hasFocus = function () { return true; };
  21. document.dispatchEvent(new Event('visibilitychange'));
  22. let lastVideoPlaying = null;
  23.  
  24. // visibilitychange events are captured and stopped
  25. document.addEventListener('visibilitychange', function (e) {
  26. e.stopImmediatePropagation();
  27.  
  28. // Try to play the video on Chrome
  29. if (typeof chrome !== 'undefined') {
  30. if (document.hidden) {
  31. const videos = document.getElementsByTagName('video');
  32. if (videos.length > 0) {
  33. lastVideoPlaying = !videos[0].paused && !videos[0].ended;
  34. } else {
  35. lastVideoPlaying = false;
  36. }
  37. } else {
  38. playVideo();
  39. }
  40. }
  41. }, true);
  42.  
  43. function setQualitySettings() {
  44. // Set the player quality to "Source"
  45. try {
  46. window.localStorage.setItem('s-qs-ts', Math.floor(Date.now()));
  47. window.localStorage.setItem('video-quality', '{"default":"chunked"}');
  48. } catch (e) {
  49. console.log(e);
  50. }
  51. }
  52.  
  53. function playVideo() {
  54. const videos = document.getElementsByTagName('video');
  55. if (videos.length > 0) {
  56. if (lastVideoPlaying === true && !videos[0].ended) {
  57. videos[0].play();
  58. }
  59. }
  60. }
  61.  
  62. setQualitySettings();
  63.  
  64. // Add event handler for when we switch between pages
  65. // This is useful when we switch for example from a Clip
  66. // without "Source" to a livestream
  67. window.addEventListener('popstate', () => {
  68. setQualitySettings();
  69. });