Last active
June 29, 2018 21:27
-
-
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
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
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(":"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)