YouTube.com quality change buttons (Mobile / Desktop)

Adds quality change buttons below the video (144p, 240p, 360p, 480p, 720p, 1080p...) works on mobile (m.youtube.com) and on desktop.

目前為 2024-02-11 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name YouTube.com quality change buttons (Mobile / Desktop)
  3. // @namespace m-youtube-com-quality-change-buttons
  4. // @version 1.11
  5. // @description Adds quality change buttons below the video (144p, 240p, 360p, 480p, 720p, 1080p...) works on mobile (m.youtube.com) and on desktop.
  6. // @author hlorand.hu
  7. // @match https://m.youtube.com/*
  8. // @match https://youtube.com/*
  9. // @match https://*.youtube.com/*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
  11. // @grant none
  12. // @license https://creativecommons.org/licenses/by-nc-sa/4.0/
  13. // @run-at document-idle
  14. // ==/UserScript==
  15.  
  16. // Screenshot: https://ibb.co/pyXQd4C
  17.  
  18. (function() {
  19. //'use strict';
  20.  
  21. function addbuttons(){
  22. document.getElementById("qualitybuttons").innerHTML = "";
  23.  
  24. var player = document.getElementById('movie_player');
  25.  
  26. // it is neccesary to start the video, to getAvailableQualityData
  27. player.click(); // start video
  28. player.click(); // pause video
  29.  
  30. const qualities = player.getAvailableQualityData();
  31.  
  32. qualities.forEach((q)=>{
  33.  
  34. let button = document.createElement('button');
  35. button.setAttribute("quality", q.quality);
  36. button.textContent = q.qualityLabel.replace("p50","p").replace("p60","p"); // remove fps from label
  37. button.className = "qualitybutton";
  38.  
  39. button.style.margin = "4px";
  40. button.style.padding = "4px";
  41. button.style.position = "relative";
  42.  
  43. // get current quality
  44. if( player.getPlaybackQualityLabel() == q.qualityLabel ){
  45. button.style.backgroundColor = "darkorange";
  46. } else{
  47. button.style.backgroundColor = "green";
  48. }
  49.  
  50. button.onclick = function() {
  51. player.setPlaybackQualityRange( this.getAttribute("quality") );
  52.  
  53. // highlight the clicked button and desaturate the others
  54. document.querySelectorAll(".qualitybutton").forEach((btn)=>{
  55. btn.style.backgroundColor = "green";
  56. });
  57. this.style.backgroundColor = "darkorange";
  58. };
  59.  
  60. let target = document.getElementById('qualitybuttons');
  61. target.insertBefore(button, target.firstChild);
  62.  
  63. }); // end qualities foreach
  64.  
  65. } // end addbuttons
  66.  
  67.  
  68. // Periodically check if the buttons are visible (sometimes YouTube redraws its interface).
  69. setInterval(()=>{
  70.  
  71. // Creating a div that will contain buttons.
  72. if( document.getElementById("qualitybuttons") == undefined ){
  73.  
  74. // placement of buttons on desktop
  75. let parent = document.getElementById('above-the-fold');
  76.  
  77. // placement of buttons on tablet
  78. if( !parent ){
  79. parent = document.querySelector('.watch-below-the-player');
  80. }
  81.  
  82. // placement of buttons on mobile
  83. if( !parent ){
  84. parent = document.querySelector('.related-chips-slot-wrapper');
  85. }
  86.  
  87. let wrapper = document.createElement('div');
  88. wrapper.setAttribute("id","qualitybuttons");
  89. parent.insertBefore(wrapper, parent.firstChild);
  90. addbuttons();
  91.  
  92. }
  93.  
  94. // Sometimes the buttons are not added, so I check and add them if necessary.
  95. if( document.getElementById("qualitybuttons").textContent.trim() === '' ){
  96. addbuttons();
  97. }
  98. }, 1000);
  99.  
  100. })();