Skip to content

Instantly share code, notes, and snippets.

Created June 29, 2016 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/689d1004cb058e498922c5a61574c9d3 to your computer and use it in GitHub Desktop.
Save anonymous/689d1004cb058e498922c5a61574c9d3 to your computer and use it in GitHub Desktop.
Youtube_Playlist_Total_Time_Count
// Get Timestamps From the Document as Array
var stamps = document.querySelectorAll('.timestamp span');
// Initialize variables
var minutes = 0, hours = 0, seconds = 0;
// Loop throgh stamps array
// For each if statement inside the loop
// We are adding the corresponding time
// in seconds, minutes and hours to our variables
for(var i=0;i<stamps.length;i++){
// Get the time as text //
var content = stamps[i].textContent;
// split or devide the content string into array
// in other words, spit the content string by ":"
var t = content.split(":");
// if the time has hour
if(t.length == 3){
// time has [hours, minutes, seconds]
hours += Number.parseInt(t[0]);
minutes += Number.parseInt(t[1]);
seconds += Number.parseInt(t[2]);
}else if(t.length == 2){
// time has [minutes, seconds]
minutes += Number.parseInt(t[0]);
seconds += Number.parseInt(t[1]);
}else if(t.length == 1){
// time has only [seconds]
seconds += Number.parseInt(t[0]);
}else{
// Something went wrong // Or it's not a time //
hours = minutes = seconds = 0;
break;
}
}
// Call "myConvert" function //
myConvert(hours, minutes, seconds);
// Define "myConvert" function
function myConvert(h,m,s){
if(h == m && m == s && s == 0){
return "Wrong Time Found";
}
// Convert seconds to minutes and add it to minutes
m += Number.parseInt(s/60);
// Decrease the seconds to maximum 60 //
s %= 60;
// Convert minutes to hours and add it to hours
h += Number.parseInt(m/60);
// Decrease the minutes to maximum 60 //
m %= 60;
// return the full string of total time //
return "Hours: "+h +" || Minutes: "+m + " || Seconds: " + s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment