Forked from liaohuqiu/gist:4ee77b9b03afcdecc80252252378d367
Last active
February 15, 2017 11:31
-
-
Save shelvacu/d6621fb90248846f64c8a2a765076d5d to your computer and use it in GitHub Desktop.
Find out the full duration time of a YouTube playlist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = document.getElementsByClassName('pl-video-time'); | |
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 ) % 24; | |
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]; | |
if(!item) return; //deal with private videos and such | |
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment