YouTube 按键加速播放

在YouTube上按住右箭头键时视频加速到2.5倍速,避免与快进功能冲突

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

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

QingJ © 2025

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