YouTube 按键加速播放

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

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

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

QingJ © 2025

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