Last active
October 30, 2024 16:38
-
-
Save pongo/694a23becd6817ff6f8ff4be1198c36e to your computer and use it in GitHub Desktop.
Fast forward and rewind hotkeys userscript for VK and Boosty. Key D: fast forward 1 second. Key A: rewind 1 second. Alt+C: copy current timecode.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Video hotkeys for VK, Rutube, Boosty - fast forward and rewind | |
// @namespace pongo | |
// @version 2024-10-30 | |
// @description Adds hotkeys for fast forwarding (key D) and rewinding (key A) videos by 1 second on VK and Boosty. Press alt+c for copy current timecode. | |
// @author pongo | |
// @match https://vk.com/video-* | |
// @match https://boosty.to/*/posts/* | |
// @match https://rutube.ru/video/* | |
// @grant GM_setClipboard | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const { isVideoTarget, getVideo } = initSite(); | |
window.addEventListener('keydown', function(e) { | |
//console.log(e); | |
switch (e.code) { | |
case "KeyD": | |
if (!isVideoTarget(e)) return; | |
e.preventDefault(); | |
seek(1, getVideo(e)); | |
return; | |
case "KeyA": | |
if (!isVideoTarget(e)) return; | |
e.preventDefault(); | |
seek(-1, getVideo(e)); | |
return; | |
case "KeyC": | |
if (!isVideoTarget(e)) return; | |
if (!e.altKey) return; | |
e.preventDefault(); | |
copyCurrentTime(getVideo(e)); | |
return; | |
} | |
}); | |
function seek(seconds, video) { | |
const time = getTime(video); | |
video.currentTime = time + seconds; | |
} | |
function copyCurrentTime(video) { | |
const time = getTime(video); | |
const timecode = new Date(time * 1000).toISOString().slice(11, 19); // https://stackoverflow.com/a/25279340/136559 | |
GM_setClipboard(timecode, "text"); | |
} | |
function getTime(video) { | |
return parseInt(video.currentTime, 10); | |
} | |
function initSite() { | |
switch (location.hostname) { | |
case 'vk.com': | |
return { | |
isVideoTarget(e) { | |
return e.target.classList.contains('videoplayer'); | |
}, | |
getVideo() { | |
return document.querySelector('#video_player video'); | |
} | |
}; | |
case 'boosty.to': | |
return { | |
isVideoTarget(e) { | |
return e.target.className === 'shadow-root-container'; | |
}, | |
getVideo(e = undefined) { | |
const target = e?.target ?? document.querySelector('vk-video-player .shadow-root-container'); | |
return target.shadowRoot.querySelector('video'); | |
} | |
}; | |
case 'rutube.ru': | |
return { | |
isVideoTarget(e) { | |
return e.target.className.includes('player') || e.target.querySelector('video') != null; | |
}, | |
getVideo() { | |
return document.querySelector('.video-player video'); | |
} | |
}; | |
default: | |
alert(`[hotkey-video] hostname "${location.hostname}" not found`); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment