m.YouTube.com more playback speeds

Adds 2.25x 2.5x 2.75x 3x speed buttons below the video

当前为 2023-12-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name m.YouTube.com more playback speeds
  3. // @namespace m-youtube-com-more-playback-speeds
  4. // @version 1.9
  5. // @description Adds 2.25x 2.5x 2.75x 3x speed buttons below the video
  6. // @author hlorand.hu
  7. // @match https://m.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
  9. // @grant none
  10. // @license https://creativecommons.org/licenses/by-nc-sa/4.0/
  11. // ==/UserScript==
  12.  
  13. // Screenshot: https://ibb.co/chtmD9F
  14.  
  15. (function() {
  16. //'use strict';
  17.  
  18. function setspeed(speed){
  19.  
  20. // highlight
  21. document.querySelectorAll(".speedbutton").forEach((btn)=>{
  22. if( btn.textContent == speed){
  23. btn.style.backgroundColor = "darkorange";
  24. } else {
  25. btn.style.backgroundColor = "blue";
  26. }
  27. });
  28.  
  29. let video = document.querySelector("video");
  30.  
  31. if(video && video.readyState >= 2) {
  32.  
  33. localStorage.setItem("yt_playbackspeed", speed);
  34.  
  35. video.playbackRate = speed;
  36. video.mozPreservesPitch = video.webkitPreservesPitch = video.preservePitch = true;
  37. }
  38. }
  39.  
  40. function addbuttons(){
  41. document.getElementById("speedbuttons").innerHTML = "";
  42.  
  43. const speeds = ["3.5","3.25","3.0","2.75","2.5","2.25","2.0","1.75","1.5","1.0"];
  44.  
  45. speeds.forEach((speed)=>{
  46. let button = document.createElement('button');
  47. button.textContent = speed;
  48. button.className = "speedbutton";
  49.  
  50. button.style.margin = "4px";
  51. button.style.padding = "4px";
  52.  
  53. button.style.backgroundColor = "blue";
  54. button.style.position = "relative";
  55.  
  56. button.onclick = function() {
  57. setspeed(this.textContent);
  58. };
  59.  
  60. let target = document.getElementById("speedbuttons");
  61. target.insertBefore(button, target.firstChild);
  62.  
  63. }); // end speeds foreach
  64.  
  65. } // end addbuttons
  66.  
  67. // Periodically check if the buttons are visible
  68. // (sometimes YouTube redraws its interface).
  69. setInterval(()=>{
  70. // Creating a div that will contain buttons.
  71. if( document.getElementById("speedbuttons") == undefined ){
  72. let parent = document.querySelector('.related-chips-slot-wrapper'); // placement of buttons
  73. let wrapper = document.createElement('div');
  74. wrapper.setAttribute("id","speedbuttons");
  75. parent.insertBefore(wrapper, parent.firstChild);
  76. addbuttons();
  77.  
  78. }
  79.  
  80. setspeed( localStorage.getItem("yt_playbackspeed") ?? 1 );
  81.  
  82. }, 1000);
  83.  
  84. })();