本人喜欢看一些油管上的播客,某句没听清楚的时候需要拉进度条,但是默认跳跃五秒有点多了。
这个好像是油猴论坛找的脚本忘记出处了,但是有点小bug,第一次加载视频不会生效,给优化了一下
// ==UserScript==
// @name YouTube Skip Time Customizer Fixed
// @namespace http://tampermonkey.net/
// @version 2.2
// @description Customize YouTube skip time by arrow key and block Numpad0~Numpad9 function
// @author TrainingDummy1
// @match https://www.youtube.com/*
// @run-at document-start
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
if (window.__ytSkipTimeCustomizerInstalled) return;
window.__ytSkipTimeCustomizerInstalled = true;
const step = 3;
function isTargetPage() {
return location.pathname === '/watch' || location.pathname.startsWith('/shorts/');
}
function getVideo() {
return (
document.querySelector('video.html5-main-video') ||
[...document.querySelectorAll('video')].find(v => Number.isFinite(v.duration)) ||
document.querySelector('video')
);
}
function isTypingTarget(target) {
if (!(target instanceof Element)) return false;
const tag = target.tagName;
return (
tag === 'INPUT' ||
tag === 'TEXTAREA' ||
target.isContentEditable ||
target.closest('[contenteditable="true"]') ||
target.closest('[role="textbox"]') ||
target.id === 'contenteditable-root'
);
}
function stopEvent(event) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
}
function seekVideo(direction) {
const video = getVideo();
if (!video || !Number.isFinite(video.duration) || !Number.isFinite(video.currentTime)) {
console.warn('没有找到可用的视频元素');
return;
}
const targetTime = Math.max(
0,
Math.min(video.duration, video.currentTime + direction * step)
);
let retries = 0;
const maxRetries = 5;
const tolerance = 0.15;
function accurateSeek() {
video.currentTime = targetTime;
if (
Math.abs(video.currentTime - targetTime) < tolerance ||
retries >= maxRetries
) {
return;
}
retries++;
setTimeout(accurateSeek, 100);
}
accurateSeek();
}
function onKeyDown(event) {
if (!isTargetPage()) return;
if (isTypingTarget(event.target)) return;
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
stopEvent(event);
const direction = event.key === 'ArrowRight' ? 1 : -1;
seekVideo(direction);
return;
}
if (/^Numpad\d$/.test(event.code)) {
stopEvent(event);
}
}
window.addEventListener('keydown', onKeyDown, {
capture: true,
passive: false
});
console.log(`YouTube 方向键 ${step} 秒快进/快退脚本已启用`);
})();
![]()
此处数字为自定义快进秒数
1 个帖子 - 1 位参与者