xxhk.org

学习骇客

油猴脚本:通过数字键快速控制视频进度

2024 / 8 / 21

前提知识:油猴脚本的用法

用法

数字键 1-9 对应视频进度 10%-90%,预览视频时再也不用鼠标移动到进度条后拖放了。

左右箭头快进,快进 60s,快退30s。

上下箭头调节播放速度。

// ==UserScript== // @name Video Progress Controller // @namespace http://tampermonkey.net/ // @version 0.1 // @description Control video progress and speed with keyboard shortcuts // @author You // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; document.addEventListener('keydown', function(e) { const video = document.querySelector('video'); if (!video) return; switch (e.key) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': video.currentTime = video.duration * (Number(e.key) * 0.1); break; case 'ArrowRight': video.currentTime += 60; break; case 'ArrowLeft': video.currentTime -= 30; break; case '+': video.playbackRate += 0.15; showFloatMessage(`Speed: ${video.playbackRate.toFixed(2)}x`); break; case '-': if (video.playbackRate > 0.15) { video.playbackRate -= 0.15; showFloatMessage(`Speed: ${video.playbackRate.toFixed(2)}x`); } break; case '0': video.playbackRate = 1.0; video.play(); showFloatMessage('Speed: 1.00x'); break; } }); function showFloatMessage(message) { const floatMessage = document.createElement('div'); floatMessage.innerText = message; floatMessage.style.position = 'fixed'; floatMessage.style.bottom = '20px'; floatMessage.style.right = '20px'; floatMessage.style.padding = '10px'; floatMessage.style.backgroundColor = 'rgba(0, 0, 0, 0.75)'; floatMessage.style.color = 'white'; floatMessage.style.borderRadius = '5px'; floatMessage.style.zIndex = '9999'; document.body.appendChild(floatMessage); setTimeout(() => { floatMessage.remove(); }, 2000); } })();