Skip to content

Instantly share code, notes, and snippets.

@Mathspy
Last active June 29, 2018 21:27
Show Gist options
  • Save Mathspy/aa18c0a3c170b17dd5378c033a6ab39e to your computer and use it in GitHub Desktop.
Save Mathspy/aa18c0a3c170b17dd5378c033a6ab39e to your computer and use it in GitHub Desktop.
Calculates the time remaining until one finishes a playlist if they have been watching videos linearly in it
Array.from(document.querySelectorAll("ytd-playlist-video-renderer"))
.slice(121) //Changes this number to the number of the last video you watched
.map(x => x.querySelector(".ytd-thumbnail-overlay-time-status-renderer").textContent.trim())
.reduce((acc, video) => {
let [h, m, s] = video.split(":").map(x => parseInt(x));
if (!s) {s = m; m = h; h = 0;}
acc[0] += h;
acc[1] += m;
acc[2] += s;
return acc;
}, [0, 0, 0])
.reduceRight((acc, clock, i) => {
if (i > 0) {
acc[i-1] = Math.floor((clock + acc[i]) / 60);
acc[i] = (clock + acc[i]) % 60;
} else {
acc[i] += clock;
}
return acc;
}, [0, 0, 0])
.map(x => x.toString())
.map(x => x.length >= 2 ? x : "0" + x)
.join(":");
@Mathspy
Copy link
Author

Mathspy commented Jun 18, 2018

There's probably a more elegant way to write reduceRight's function (even if that way doesn't use reduceRight completely or changes the way the previous reduce works)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment