Skip to content

Instantly share code, notes, and snippets.

@schoettker
Last active April 13, 2020 17:30
Show Gist options
  • Select an option

  • Save schoettker/e349af0a20ebb861e26851d564048536 to your computer and use it in GitHub Desktop.

Select an option

Save schoettker/e349af0a20ebb861e26851d564048536 to your computer and use it in GitHub Desktop.
Log the total length of a youtube playlist by pasting into console on a playlist overview page
const durationContainers = Array.from(document.querySelectorAll("#primary #contents .ytd-thumbnail-overlay-time-status-renderer"));
var durations = durationContainers.map(__span => __span.innerText).map(duration => {
if (duration) {
const fragments = duration.split(":").reverse();
const fragmentsToSeconds = fragments.reduce((acc, current, idx) => acc + parseInt(current) * Math.pow(60, idx), 0);
return fragmentsToSeconds
}
else {
return 0;
}
});
const totalSeconds = durations.reduce((acc, current) => acc + current, 0)
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor(totalSeconds / 60) % 60;
const seconds = totalSeconds % 60;
const time = `${String(hours).padStart(2, 0)}h:${String(minutes).padStart(2,0)}m:${String(seconds).padStart(2,0)}s`
console.log(time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment