m.YouTube.com more playback speeds

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

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

  1. // ==UserScript==
  2. // @name m.YouTube.com more playback speeds
  3. // @namespace m-youtube-com-more-playback-speeds
  4. // @version 1.8
  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. console.log(btn);
  25. } else {
  26. btn.style.backgroundColor = "blue";
  27. }
  28. });
  29.  
  30. let video = document.querySelector("video");
  31.  
  32. if(video && video.readyState >= 2) {
  33.  
  34. localStorage.setItem("yt_playbackspeed", speed);
  35.  
  36. video.playbackRate = speed;
  37. video.mozPreservesPitch = video.webkitPreservesPitch = video.preservePitch = true;
  38. }
  39. }
  40.  
  41. function addbuttons(){
  42. document.getElementById("speedbuttons").innerHTML = "";
  43.  
  44. const speeds = ["3.5","3.25","3.0","2.75","2.5","2.25","2.0","1.75","1.5","1.0"];
  45.  
  46. speeds.forEach((speed)=>{
  47. let button = document.createElement('button');
  48. button.textContent = speed;
  49. button.className = "speedbutton";
  50.  
  51. button.style.margin = "4px";
  52. button.style.padding = "4px";
  53.  
  54. button.style.backgroundColor = "blue";
  55. button.style.position = "relative";
  56.  
  57. button.onclick = function() {
  58. setspeed(this.textContent);
  59. };
  60.  
  61. let target = document.getElementById("speedbuttons");
  62. target.insertBefore(button, target.firstChild);
  63.  
  64. }); // end speeds foreach
  65.  
  66. } // end addbuttons
  67.  
  68. // Periodically check if the buttons are visible
  69. // (sometimes YouTube redraws its interface).
  70. setInterval(()=>{
  71. // Creating a div that will contain buttons.
  72. if( document.getElementById("speedbuttons") == undefined ){
  73. let parent = document.querySelector('.related-chips-slot-wrapper'); // placement of buttons
  74. let wrapper = document.createElement('div');
  75. wrapper.setAttribute("id","speedbuttons");
  76. parent.insertBefore(wrapper, parent.firstChild);
  77. addbuttons();
  78.  
  79. }
  80.  
  81. setspeed( localStorage.getItem("yt_playbackspeed") ?? 1 );
  82.  
  83. }, 1000);
  84.  
  85. })();