YouTube Speed Control

Hold right arrow key to speed up YouTube video to 2.5x, without interfering with the forward function

当前为 2024-12-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Speed Control
  3. // @name:zh YouTube 按键加速播放
  4. // @namespace https://github.com/landrarwolf/youtube-speed-control
  5. // @version 0.4
  6. // @description Hold right arrow key to speed up YouTube video to 2.5x, without interfering with the forward function
  7. // @description:zh 在YouTube上按住右箭头键时视频加速到2.5倍速,避免与快进功能冲突
  8. // @author landrarwolf
  9. // @match https://www.youtube.com/*
  10. // @license MIT
  11. // @supportURL https://github.com/landrarwolf/youtube-speed-control/issues
  12. // @homepageURL https://github.com/landrarwolf/youtube-speed-control
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. // 在文件开头添加语言配置
  17. const i18n = {
  18. en: {
  19. speedIndicator: '⚡ 2.5x Speed'
  20. },
  21. zh: {
  22. speedIndicator: '⚡ 2.5x 加速中'
  23. }
  24. };
  25.  
  26. // 获取当前语言
  27. function getCurrentLanguage() {
  28. const lang = navigator.language.toLowerCase().split('-')[0];
  29. return lang in i18n ? lang : 'en';
  30. }
  31.  
  32. (function() {
  33. 'use strict';
  34. let normalSpeed = 1.0; // 保存正常播放速度
  35. let speedTimeout = null; // 用于延迟处理速度变化
  36. let isSpeedUp = false; // 标记是否处于加速状态
  37. let pressStartTime = 0; // 记录按键开始时间
  38. let speedIndicator = null; // 速度提示元素
  39. // 创建速度提示元素
  40. function createSpeedIndicator() {
  41. const indicator = document.createElement('div');
  42. indicator.style.cssText = `
  43. position: fixed;
  44. top: 20px;
  45. left: 50%;
  46. transform: translateX(-50%);
  47. background-color: rgba(0, 0, 0, 0.8);
  48. color: white;
  49. padding: 8px 16px;
  50. border-radius: 4px;
  51. z-index: 9999;
  52. font-size: 14px;
  53. font-family: Arial, sans-serif;
  54. display: none;
  55. transition: opacity 0.2s;
  56. `;
  57. // 使用当前语言的文本
  58. indicator.textContent = i18n[getCurrentLanguage()].speedIndicator;
  59. document.body.appendChild(indicator);
  60. return indicator;
  61. }
  62. // 显示速度提示
  63. function showSpeedIndicator() {
  64. if (!speedIndicator) {
  65. speedIndicator = createSpeedIndicator();
  66. }
  67. speedIndicator.style.display = 'block';
  68. speedIndicator.style.opacity = '1';
  69. }
  70. // 隐藏速度提示
  71. function hideSpeedIndicator() {
  72. if (speedIndicator) {
  73. speedIndicator.style.opacity = '0';
  74. setTimeout(() => {
  75. speedIndicator.style.display = 'none';
  76. }, 200);
  77. }
  78. }
  79.  
  80. // 监听键盘按下事件
  81. document.addEventListener('keydown', function(event) {
  82. if (event.key === 'ArrowRight') {
  83. if (!event.repeat) {
  84. pressStartTime = Date.now();
  85. speedTimeout = setTimeout(() => {
  86. const video = document.querySelector('video');
  87. if (video) {
  88. normalSpeed = video.playbackRate;
  89. video.playbackRate = 2.5;
  90. isSpeedUp = true;
  91. showSpeedIndicator();
  92. }
  93. event.preventDefault();
  94. event.stopPropagation();
  95. }, 200);
  96. } else {
  97. if (Date.now() - pressStartTime > 200) {
  98. event.preventDefault();
  99. event.stopPropagation();
  100. }
  101. }
  102. }
  103. }, true);
  104. // 监听键盘释放事件
  105. document.addEventListener('keyup', function(event) {
  106. if (event.key === 'ArrowRight') {
  107. const pressDuration = Date.now() - pressStartTime;
  108. if (speedTimeout) {
  109. clearTimeout(speedTimeout);
  110. speedTimeout = null;
  111. }
  112. if (pressDuration < 200) {
  113. return;
  114. }
  115. if (isSpeedUp) {
  116. const video = document.querySelector('video');
  117. if (video) {
  118. video.playbackRate = normalSpeed;
  119. isSpeedUp = false;
  120. hideSpeedIndicator();
  121. }
  122. event.preventDefault();
  123. event.stopPropagation();
  124. }
  125. }
  126. }, true);
  127. })();

QingJ © 2025

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