// ==UserScript==
// @name 视频快进助手
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 自动加速当前网页所有视频,支持快捷键切换倍速、快进
// @match *://*/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function () {
'use strict';
var SPEEDS = [1, 1.5, 2, 3, 5, 10];
var idx = 3; // 默认 3 倍速
// 设置所有视频的倍速
function setSpeed(rate, autoplay) {
document.querySelectorAll('video').forEach(function (v) {
v.playbackRate = rate;
if (autoplay) {
v.play().catch(function () {});
}
});
}
// 循环切换倍速
function cycleSpeed() {
idx = (idx + 1) % SPEEDS.length;
setSpeed(SPEEDS[idx], true);
toast('倍速: ' + SPEEDS[idx] + 'x');
}
// 快进/快退
function skip(seconds) {
document.querySelectorAll('video').forEach(function (v) {
v.currentTime += seconds;
});
toast('快进 ' + seconds + ' 秒');
}
// 直接跳到结尾前 1 秒
function skipToEnd() {
document.querySelectorAll('video').forEach(function (v) {
if (v.duration && isFinite(v.duration)) {
v.currentTime = v.duration - 1;
v.play().catch(function () {});
}
});
toast('已跳到结尾前 1 秒');
}
// 屏幕提示
function toast(msg) {
var t = document.getElementById('speed-toast');
if (!t) {
t = document.createElement('div');
t.id = 'speed-toast';
t.style.cssText = 'position:fixed;top:16px;right:16px;z-index:2147483647;background:rgba(0,0,0,.8);color:#fff;padding:8px 14px;border-radius:6px;font:14px/1.4 sans-serif;pointer-events:none;';
document.body.appendChild(t);
}
t.textContent = msg;
t.style.display = 'block';
clearTimeout(t._timer);
t._timer = setTimeout(function () { t.style.display = 'none'; }, 1200);
}
// 页面加载后立即默认 3 倍速
setSpeed(SPEEDS[idx], false);
// 快捷键
document.addEventListener('keydown', function (e) {
var tag = (e.target && e.target.tagName) || '';
if (/INPUT|TEXTAREA|SELECT/.test(tag) || e.target.isContentEditable) return; // 输入框内不响应
var k = e.key.toLowerCase();
if (k === 'd') { cycleSpeed(); } // D: 切换倍速
else if (k === 's') { setSpeed(1, false); toast('恢复 1x'); } // S: 恢复 1 倍
else if (k === 'f') { setSpeed(3, true); toast('3x'); } // F: 直接 3 倍
else if (k === 'g') { skip(10); } // G: 快进 10 秒
else if (k === 'h') { skip(-10); } // H: 快退 10 秒
else if (k === 'e') { skipToEnd(); } // E: 直接跳到结尾前 1 秒
});
// 监听新出现的视频(SPA 页面/懒加载)
var mo = new MutationObserver(function () {
setSpeed(SPEEDS[idx], false);
});
mo.observe(document.documentElement, { childList: true, subtree: true });
})();