// ==UserScript== // @name 通用视频倍速控制 // @name:zh-CN 通用视频倍速控制 // @name:en Video Speed Control (any site) // @namespace https://github.com/yilecoding/helper-scripts // @version 2025.04.17.9 // @description 任意网站的 HTML5 视频/音频都能用快捷键调速:加速、减速、恢复 1.0、一键切常用倍速、快进快退。站点把速度重置回 1 时自动恢复,按站点记住倍速。 // @description:zh-CN 任意网站的 HTML5 视频/音频都能用快捷键调速:加速、减速、恢复 1.0、一键切常用倍速、快进快退。站点把速度重置回 1 时自动恢复,按站点记住倍速。 // @description:en Keyboard speed control for any HTML5 video/audio: faster, slower, reset to 1.0, toggle a preferred speed, seek back/forward. Re-applies your speed when the site resets it to 1, and remembers the speed per site. // @author 壹樂(V:ita1125) // @match *://*/* // @grant GM_getValue // @grant GM_setValue // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @license MIT // @run-at document-start // @homepageURL https://github.com/yile2020/helper-scripts // @supportURL https://github.com/yile2020/helper-scripts/issues // @updateURL https://raw.githubusercontent.com/yile2020/helper-scripts/main/video-speed/video-speed.user.js // @downloadURL https://raw.githubusercontent.com/yile2020/helper-scripts/main/video-speed/video-speed.user.js // ==/UserScript== // 脚本持续更新维护,欢迎 star / watch 仓库获取最新版本: // https://github.com/yile2020/helper-scripts /* * @run-at 是 document-start:键盘监听要在页面自己的监听「之前」注册到捕获阶段, * 才轮得到我们先看按键。DOM 相关的启动等 DOMContentLoaded(见文件末尾)。 */ (function () { 'use strict'; /** 统一日志前缀。控制台过滤框输入「倍速」即可只看本脚本。 */ const TAG = '[倍速控制]'; // ========================================================================= // 配置 // ========================================================================= const CONFIG = { /* * 快捷键,写的是 KeyboardEvent.code(物理键位),不是 e.key。 * e.key 随键盘布局和输入法变:俄语布局按 X 得到「ч」,中文输入法激活时是「Process」, * 用 e.key 的话这些用户一个键都按不出来。code 只认键位,跟布局无关。 * * 默认键避开了常见播放器已经占用的:YouTube 的 K J L F M C I T N P、数字、< >; * B 站的 D(弹幕开关)、F、M、[ ]。按住 Shift/Ctrl/Alt/Meta 时一律不响应, * 把组合键留给浏览器和页面。 */ keys: { slower: 'KeyZ', faster: 'KeyX', reset: 'KeyR', // 恢复 1.0 preferred: 'KeyG', // 切到常用倍速,再按切回之前的速度 rewind: 'KeyQ', forward: 'KeyE', }, preferredRate: 2.0, seekSeconds: 5, step: 0.25, // 加速 / 减速步长 minRate: 0.1, maxRate: 16, // Chrome 允许的上限就是 16,再大直接抛 NotSupportedError /* * 用户手势之后这么久以内发生的 ratechange,算用户在播放器自带菜单里主动改的, * 我们跟着改,不再把它拉回去。超出这个窗口的算站点重置。 */ userChangeWindowMs: 1000, /* * 站点把速度锁死在 1(每次 ratechange 都改回去)时,我们改、它改回、我们再改…… * 测试里不设上限 0.5 秒就是 55 万次 ratechange。fightWindowMs 内被重置超过 fightMax 次 * 就对这个元素让步,直到用户再按快捷键。 */ fightMax: 5, fightWindowMs: 2000, osdMs: 1000, // 改速提示停留多久后淡出 }; // ========================================================================= // 媒体元素登记 // ========================================================================= const Media = (() => { const known = new Set(); /** 媒体元素 → 最近一次被用户点到 / 被我们操作的时间。WeakMap,元素被回收时自动消失。 */ const touched = new WeakMap(); const newListeners = []; function track(el) { if (known.has(el)) return; known.add(el); for (const fn of newListeners) fn(el); } /** 新发现一个媒体元素时回调(含启动时已经在页面上的)。 */ function onNew(fn) { newListeners.push(fn); } /* * 动态插入的媒体靠 MutationObserver 发现,好让倍速在它开播前就到位,不用等用户按键。 * MutationObserver 看不进 shadow root 里面,所以每发现一个 open shadow root 就单独 observe 它。 */ const observedRoots = new WeakSet(); const observer = new MutationObserver(records => { for (const rec of records) { for (const node of rec.addedNodes) if (node.nodeType === 1) collect(node); } }); function observeRoot(root) { if (observedRoots.has(root)) return; observedRoots.add(root); observer.observe(root, { childList: true, subtree: true }); } const isMedia = node => node.nodeType === 1 && (node.tagName === 'VIDEO' || node.tagName === 'AUDIO'); /** * 深度收集:连同 open shadow DOM 一起找。 * document.querySelectorAll 不会穿进 shadow root,Web Components 封装的播放器 * ( 里面套