Skip to content

Instantly share code, notes, and snippets.

@rigred
Created June 5, 2020 11:38
Show Gist options
  • Save rigred/4efa4310343d72cfdd58ec896bd41deb to your computer and use it in GitHub Desktop.
Save rigred/4efa4310343d72cfdd58ec896bd41deb to your computer and use it in GitHub Desktop.
Get playtime of a youtube playlist
var list = document.getElementsByClassName('ytd-thumbnail-overlay-time-status-renderer');
var time = 0;
function toS(hms) {
var a = hms.split(':');
while (a.length < 3) {
a.unshift(0);
}
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
return seconds;
}
function toHMS(totalSec) {
var hours = parseInt( totalSec / 3600 );
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;
var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
return result;
}
function add(item) {
/*item = item.getElementsByClassName('timestamp')[0];
item = item.getElementsByTagName('SPAN')[0]; */
var timeString = (item.innerText || item.textContent);
time += toS(timeString);
}
for (var i = 0; i < list.length; i++) {
var item = list[i];
add(item);
}
console.log("The full duration time is: " + toHMS(time));
@rigred
Copy link
Author

rigred commented Jun 5, 2020

One Liner to paste into Browser Console:

var list=document.getElementsByClassName("ytd-thumbnail-overlay-time-status-renderer"),time=0;function toS(t){for(var e=t.split(":");e.length<3;)e.unshift(0);return 60*+e[0]*60+60*+e[1]+ +e[2]}function toHMS(t){var e=parseInt(t/3600),i=parseInt(t/60)%60,n=t%60;return(e<10?"0"+e:e)+":"+(i<10?"0"+i:i)+":"+(n<10?"0"+n:n)}function add(t){var e=t.innerText||t.textContent;time+=toS(e)}for(var i=0;i<list.length;i++){var item=list[i];add(item)}console.log("The full duration time is: "+toHMS(time));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment