// ==UserScript== // @name 视频左右键快进快退 // @namespace http://tampermonkey.net/ // @version 1.0.1 // @description 仅保留全局键盘左右键快进/快退(按住连续),跨iframe自动生效 // @author You // @match http://*/* // @match https://*/* // @allFrames true // @run-at document-idle // ==/UserScript== /* 【参考:[全功能智能视频倍速控制器 (全局键盘生效版) ](https://scriptcat.org/zh-CN/script-show-page/7545 )】 */ (function () { 'use strict'; // 防止重复注入 if (window.__SEEK_ONLY_INJECTED__) return; window.__SEEK_ONLY_INJECTED__ = true; // ========================================== // 配置 // ========================================== const CONFIG = { SEEK_STEP: 3, // 快进/快退秒数 SEEK_INTERVAL: 500, // 按住方向键时的重复间隔(毫秒) EDGE_PADDING: 0.5, // 直播/可寻址边界的保护距离 MIN_VIDEO_WIDTH: 320, // 小于该尺寸的预览/缩略图视频忽略 MIN_VIDEO_HEIGHT: 180 }; // ========================================== // 全屏元素 // ========================================== function getFullscreenElement() { let fs = document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement; while (fs && fs.shadowRoot && fs.shadowRoot.fullscreenElement) { fs = fs.shadowRoot.fullscreenElement; } return fs; } // ========================================== // 视频可见性 + 尺寸过滤 // ========================================== function isVisibleVideo(v) { if (!v) return false; const rect = v.getBoundingClientRect(); if (rect.width <= CONFIG.MIN_VIDEO_WIDTH || rect.height <= CONFIG.MIN_VIDEO_HEIGHT) { return false; } const style = window.getComputedStyle(v); return ( style.visibility !== 'hidden' && style.display !== 'none' && style.opacity !== '0' ); } // ========================================== // 取面积最大的视频 // ========================================== function getLargestVideo(videos) { return videos.reduce((largest, current) => { const l = largest ? largest.getBoundingClientRect() : null; const c = current.getBoundingClientRect(); const lArea = l ? l.width * l.height : 0; const cArea = c.width * c.height; return cArea > lArea ? current : largest; }, null); } // ========================================== // 获取当前活动视频 // ========================================== function getActiveVideo() { // 1. 全屏优先 const fsElement = getFullscreenElement(); if (fsElement) { if (fsElement.tagName === 'VIDEO') return fsElement; const fsVideos = Array.from(fsElement.querySelectorAll('video')).filter(isVisibleVideo); if (fsVideos.length > 0) { return fsVideos.find(v => !v.paused) || getLargestVideo(fsVideos); } } // 2. 页面中的视频 const allVideos = Array.from(document.querySelectorAll('video')).filter(isVisibleVideo); if (allVideos.length === 0) return null; const playingVideos = allVideos.filter(v => !v.paused && v.readyState >= 2); if (playingVideos.length > 0) { return getLargestVideo(playingVideos); } // 3. 都没在播放:取离视口中心最近的 const viewportCenterY = window.innerHeight / 2; let closestVideo = null; let minDistance = Infinity; allVideos.forEach(video => { const rect = video.getBoundingClientRect(); const videoCenterY = rect.top + rect.height / 2; const distance = Math.abs(videoCenterY - viewportCenterY); if (distance < minDistance) { minDistance = distance; closestVideo = video; } }); return closestVideo; } // ========================================== // Seek // ========================================== function seekVideo(seconds) { const video = getActiveVideo(); if (!video) return; let target = video.currentTime + seconds; if (video.seekable && video.seekable.length > 0) { const minTime = video.seekable.start(0); const maxTime = video.seekable.end(video.seekable.length - 1) - CONFIG.EDGE_PADDING; if (maxTime > minTime) { target = Math.max(minTime, Math.min(maxTime, target)); } else { target = Math.max(minTime, target); } } else if (video.duration && isFinite(video.duration)) { target = Math.max(0, Math.min(video.duration, target)); } else { target = Math.max(0, target); } /* try { video.currentTime = target; } catch (e) { }*/ // 解决快进时音画不同步的问题 try { video.currentTime = target; // Edge 不支持 fastSeek,用帧回调在渲染后做缓冲区重置 if (typeof video.requestVideoFrameCallback === 'function') { video.requestVideoFrameCallback(() => { try { const cur = video.currentTime; video.currentTime = cur + 0.01; video.currentTime = cur; } catch (e) { } }); } else { // 极低版本 Edge 的兜底 setTimeout(() => { try { const cur = video.currentTime; video.currentTime = cur + 0.01; video.currentTime = cur; } catch (e) { } }, 16); } } catch (e) { } } // ========================================== // 按住方向键连续 Seek // ========================================== let keySeekTimer = null; let activeKeyCode = null; function stopKeySeek() { if (keySeekTimer) { clearInterval(keySeekTimer); keySeekTimer = null; } activeKeyCode = null; } // ========================================== // 全局键盘控制 // ========================================== window.addEventListener('keydown', e => { // 输入框内不触发 const activeEl = document.activeElement; const activeTag = activeEl ? activeEl.tagName : ''; if (['INPUT', 'TEXTAREA'].includes(activeTag) || activeEl?.isContentEditable) { return; } const code = e.code; if (code !== 'ArrowLeft' && code !== 'ArrowRight') return; e.preventDefault(); e.stopPropagation(); // 忽略系统按键重复,由定时器接管连续快进 if (e.repeat) return; const delta = code === 'ArrowLeft' ? -CONFIG.SEEK_STEP : CONFIG.SEEK_STEP; seekVideo(delta); if (activeKeyCode === code) return; stopKeySeek(); activeKeyCode = code; keySeekTimer = setInterval(() => { seekVideo(delta); }, CONFIG.SEEK_INTERVAL); }, true); // 按键松开 / 失焦 停止连续 Seek window.addEventListener('keyup', e => { if (e.code === activeKeyCode) { stopKeySeek(); } }, true); window.addEventListener('blur', stopKeySeek); // 页面离开清理 window.addEventListener('beforeunload', stopKeySeek); })();