Skip to content

Instantly share code, notes, and snippets.

@zidan-p
Last active June 11, 2024 08:59
Show Gist options
  • Select an option

  • Save zidan-p/a4378fca0ecc3632c2e6c72aeec7b91e to your computer and use it in GitHub Desktop.

Select an option

Save zidan-p/a4378fca0ecc3632c2e6c72aeec7b91e to your computer and use it in GitHub Desktop.
get sum of video length from playlist on youtube
function getTotalPlaylistDuration(){
const elements = document.querySelectorAll("#contents > ytd-playlist-video-renderer .badge-shape-wiz__text")
/**
* @type {{second: number, minute: number, hour: number}[]}
*/
const container = [];
elements.forEach(item =>{
const str = item.innerHTML.split(".")
const secondPos = str.length - 1;
const minutePos = str.length - 2;
const hourPos = str.lengt - 2;
const second = secondPos >= 0 ? str[secondPos] : 0;
const minute = minutePos >= 0 ? str[minutePos] : 0;
const hour = hourPos >= 0 ? str[hourPos] : 0;
container.push({
second: Number(second),
minute: Number(minute),
hour: Number(hour),
})
})
const result = container.reduce((prev, current) => {
return {
second: prev.second + current.second,
minute: prev.minute + current.minute,
hour: prev.hour + current.hour
}
}, {second: 0, minute: 0, hour: 0});
let hour = result.hour;
let minute = result.minute;
let minuteExcess = 0;
let second = result.second;
let secondExcess = 0;
secondExcess = second % 60;
minute = Math.floor(second / 60) + minute;
second = secondExcess;
minuteExcess = minute % 60;
hour = hour + Math.floor(minute / 60);
minute = minuteExcess;
console.log(hour + " : " + minute + " : " + second);
}
getTotalPlaylistDuration();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment