Last active
June 20, 2022 02:05
-
-
Save gervasiocaj/4fd8186b34cd6f06ce03e73da6e9a643 to your computer and use it in GitHub Desktop.
Youtube snippet utils to build timestamp lists
This file contains 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
(function () { | |
const ytPlayer = document.getElementById("movie_player"); | |
const viewCount = document.querySelector(".ytd-video-view-count-renderer"); | |
const oldBtns = document.querySelectorAll("[data-timestamputils='true']"); | |
oldBtns.forEach(function (el) { el.remove(); }); | |
function buildButton(name, onClickFn) { | |
const btn = document.createElement("button"); | |
btn.setAttribute("data-timestamputils", true); | |
btn.innerText = name; | |
btn.onclick = onClickFn; | |
viewCount.insertAdjacentElement("afterend", btn); | |
} | |
// copy current timestamp to clipboard | |
buildButton("copytime", function () { | |
let sec = parseInt(ytPlayer.getCurrentTime()); | |
let min = (sec / 60); | |
let hr = (min / 60); | |
sec = parseInt(sec % 60).toString().padStart(2, "0"); | |
min = parseInt(min % 60).toString().padStart(2, "0"); | |
hr = parseInt(hr ).toString().padStart(2, "0"); | |
// txt formatting: [hh:]mm:ss | |
const txt = (hr == "00" ? "" : hr + ":") + min + ":" + sec; | |
navigator.clipboard.writeText(txt); | |
}); | |
// plays at 1x speed, goes back 3 secs | |
buildButton("whoopskip", function () { | |
// doesn't always work // ytPlayer.setPlaybackRate(1); | |
document.querySelector("video").playbackRate = 1; | |
const curr = ytPlayer.getCurrentTime(); | |
ytPlayer.seekTo(curr - 3); | |
ytPlayer.playVideo(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment