Youtube Automatic Max Quality

Set Youtube player automatically to max quality.

  1. // ==UserScript==
  2. // @name Youtube Automatic Max Quality
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-03-02
  5. // @description Set Youtube player automatically to max quality.
  6. // @author Santeri Hetekivi
  7. // @match *://*.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @grant none
  10. // @license Apache-2.0
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15. /**
  16. * Get element with selector and call callback with it.
  17. * @param {string} selector Selector for the element.
  18. * @param {function} callback Callback function to call with the element.
  19. */
  20. function forElement(selector, callback) {
  21. // Init forElement.timeoutCount.
  22. if (forElement.timeoutCount === undefined) {
  23. forElement.timeoutCount = {}
  24. }
  25. // Init forElement.timeoutCount[selector].
  26. if (forElement.timeoutCount[selector] === undefined) {
  27. forElement.timeoutCount[selector] = 0
  28. }
  29.  
  30. // Get element.
  31. const element = document.querySelector(selector)
  32.  
  33. // If element not found.
  34. if (element === null) {
  35. // try again after timeout.
  36. setTimeout(
  37. function () {
  38. forElement(selector, callback)
  39. },
  40. (
  41. // Base timeout.
  42. 100
  43. *
  44. // Increase timeout after each try.
  45. (forElement.timeoutCount[selector]++)
  46. )
  47. )
  48. }
  49. // If element found
  50. else {
  51. // reset timeout count
  52. forElement.timeoutCount[selector] = 0
  53. // and call callback with element.
  54. callback(element)
  55. }
  56. }
  57.  
  58. // Init previous video id.
  59. let videoIdPrevious = null;
  60.  
  61. /**
  62. * Run on url change.
  63. */
  64. function onUrlChange() {
  65. // Get video id from url.
  66. const videoId = (new URLSearchParams(window.location.search)).get("v");
  67. // If
  68. if (
  69. // did not get video id
  70. videoId === null
  71. ||
  72. // or video id did not change
  73. videoId === videoIdPrevious
  74. ) {
  75. // just return.
  76. return
  77. }
  78.  
  79. // Update previous video id.
  80. videoIdPrevious = videoId;
  81.  
  82. // Run for
  83. forElement(
  84. // video player
  85. ".html5-video-player",
  86. function (player) {
  87. // get max quality (cannot use getMaxPlaybackQuality because it returns 'auto')
  88. const maxQuality = player.getAvailableQualityLevels()[0]
  89. // set max quality.
  90. player.setPlaybackQualityRange(maxQuality, maxQuality)
  91. }
  92. )
  93.  
  94. // NOTE: If you dont have Youtube Premium or dont care for 1080p Premium, you dont need this:
  95. // Run for
  96. forElement(
  97. // settings button
  98. ".ytp-settings-button",
  99. function (buttonSettings) {
  100. // click settings button
  101. buttonSettings.click()
  102. // and run for
  103. forElement(
  104. // quality selection button
  105. ".ytp-panel .ytp-menuitem:nth-child(5)",
  106. function (buttonSelectQuality) {
  107. // click quality selection button
  108. buttonSelectQuality.click()
  109. // and run for first quality option
  110. forElement(
  111. ".ytp-quality-menu .ytp-menuitem:nth-child(1)",
  112. function (buttonMaxQuality) {
  113. // click max quality option.
  114. buttonMaxQuality.click()
  115. }
  116. )
  117. }
  118. )
  119. }
  120. )
  121. }
  122.  
  123. // Add event listener for
  124. window.addEventListener(
  125. // Youtube page data updated event.
  126. 'yt-page-data-updated',
  127. function () {
  128. onUrlChange();
  129. }
  130. );
  131.  
  132. // Run on url change once on first load.
  133. onUrlChange();
  134. })();

QingJ © 2025

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