Skip to content

Instantly share code, notes, and snippets.

@abhijit-hota
Last active July 15, 2021 13:18
Show Gist options
  • Save abhijit-hota/b59fc0ea4e41f8aab580300f529a993a to your computer and use it in GitHub Desktop.
Save abhijit-hota/b59fc0ea4e41f8aab580300f529a993a to your computer and use it in GitHub Desktop.
Output total time of a playlist
/*
Note: This works for URLs like the following:
https://www.youtube.com/playlist?list=PLDV1Zeh2NRsAsbafOroUBnNV8fhZa7P4u
https://www.youtube.com/watch?v=oQQO_n57SB0&list=PLDV1Zeh2NRsAsbafOroUBnNV8fhZa7P4u&index=6
*/
const extra = window.location.pathname === "/watch" ? "panel-" : "";
const tag = `ytd-playlist-${extra}video-renderer`;
const playlistPanel = Array.from(document.getElementsByTagName(tag));
const timeStrings = playlistPanel.map((video) => {
const timestamp = video.getElementsByTagName("ytd-thumbnail-overlay-time-status-renderer")[0];
const value = timestamp.children[1].innerText;
return value;
});
const timeInSeconds = timeStrings.reduce((total, curr) => {
const [minutes, seconds] = curr.split(":");
return parseInt(minutes * 60) + parseInt(seconds) + total;
}, 0);
console.log("Time in seconds:", timeInSeconds);
console.log(`${Math.round(timeInSeconds / 3600)}h ${Math.round(timeInSeconds / 60) % 60}m ${timeInSeconds % 60}s`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment