youtube播放视频倍速自定义,可记忆

youtube播放视频倍速自定义,刷新浏览器也不会丢失之前设置的速度

目前为 2019-08-19 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name youtube播放视频倍速自定义,可记忆
  3. // @namespace EsfB3XVPmbThEv39bdxQR2hzid30iMF9
  4. // @version 0.5
  5. // @description youtube播放视频倍速自定义,刷新浏览器也不会丢失之前设置的速度
  6. // @author tomoya
  7. // @include http*://*youtube.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. let videoSpeedElement;
  15.  
  16. setInterval(function () {
  17. if (document.querySelector("#count") && document.getElementById("video_speed_div") === null) {
  18. addSpeedBtn();
  19. }
  20. }, 100);
  21.  
  22. function addSpeedBtn() {
  23. let style = document.createElement('style');
  24. style.type = 'text/css';
  25. style.innerHTML = "#video_speed_div button { outline: 0; padding: 5px 7px; margin-left: 10px; background-color: #e2e0e0; border: 0; color: #222; cursor: pointer;} .video_speed_div-button-active { border: 0!important; background-color: #ff0000!important; color: #fff!important; }";
  26. document.getElementsByTagName('head').item(0).appendChild(style);
  27.  
  28. videoSpeedElement = document.createElement("span");
  29. videoSpeedElement.setAttribute("id", "video_speed_div");
  30.  
  31. let speedArr = [1, 1.5, 2, 2.5, 3];
  32.  
  33. for (let i = 0; i < speedArr.length; i++) {
  34. let speed = speedArr[i];
  35. let btn = document.createElement("button");
  36. btn.innerHTML = "x" + speed;
  37. btn.style.width = "40px";
  38. btn.setAttribute("id", "third_video_plugin_btn_" + speed);
  39. btn.addEventListener("click", changeVideoSpeed);
  40. videoSpeedElement.appendChild(btn);
  41. }
  42.  
  43. videoSpeedElement.style.width = "300%";
  44. videoSpeedElement.style.height = "30px";
  45.  
  46. let targetElement = document.querySelectorAll("[id='count']");
  47. for (var i = 0; i < targetElement.length; i++) {
  48. if (targetElement[i].className.indexOf("ytd-video-primary-info-renderer") > -1) {
  49. targetElement[i].appendChild(videoSpeedElement);
  50. }
  51. }
  52.  
  53. // 加载之间已经设置的速度
  54. let third_video_plugin_speed = localStorage.getItem("third_video_plugin_speed");
  55. if (!third_video_plugin_speed) third_video_plugin_speed = '1';
  56.  
  57. for (let i = 0; i < videoSpeedElement.childNodes.length; i++) {
  58. let btn = videoSpeedElement.childNodes[i];
  59. if (btn.getAttribute("id") === "third_video_plugin_btn_" + third_video_plugin_speed) {
  60. btn.click();
  61. }
  62. }
  63. document.getElementById("third_video_plugin_btn_" + third_video_plugin_speed).click();
  64. }
  65.  
  66. function changeVideoSpeed(e) {
  67. let speed = parseFloat(e.target.innerHTML.replace("x", ""));
  68. localStorage.setItem("third_video_plugin_speed", speed);
  69. document.querySelector(".html5-main-video").playbackRate = speed;
  70. for (let i = 0; i < videoSpeedElement.childNodes.length; i++) {
  71. let btn = videoSpeedElement.childNodes[i];
  72. btn.setAttribute("class", "");
  73. }
  74. e.target.setAttribute("class", "video_speed_div-button-active");
  75. }
  76.  
  77. })();

QingJ © 2025

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