Last active
September 2, 2023 17:06
-
-
Save paolosimone/41343ed7915db11fa26d898c8d0ffa8e to your computer and use it in GitHub Desktop.
This file contains 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
// ==UserScript== | |
// @name Youtube Playlist Total Time | |
// @namespace https://www.youtube.com/ | |
// @version 0.3 | |
// @description Shows cumulative time of videos in a Youtube playlist | |
// @author Paolo Simone | |
// @match https://www.youtube.com/playlist* | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
// credits: https://gist.github.com/liaohuqiu/4ee77b9b03afcdecc80252252378d367 | |
(function() { | |
function toSeconds(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 format(totalSec) { | |
let days = parseInt(totalSec / 3600 / 24); | |
let hours = parseInt(totalSec / 3600) % 24; | |
let minutes = parseInt(totalSec / 60) % 60; | |
let seconds = totalSec % 60; | |
return (days > 0 ? (days + "d ") : "") + | |
(hours > 0 ? (hours + "h ") : "") + | |
(minutes > 0 ? (minutes + "m ") : "") + | |
seconds + "s "; | |
} | |
function computeTotalTime(items) { | |
let totalSeconds = 0; | |
for (let i = 0; i < items.length; i++) { | |
totalSeconds += toSeconds(items[i].innerText); | |
} | |
return format(totalSeconds); | |
} | |
function showTotalTime(lastLoaded) { | |
let timeLabels = document.querySelectorAll('span.ytd-thumbnail-overlay-time-status-renderer'); | |
let fullyLoaded = timeLabels.length > 0 && timeLabels.length === lastLoaded; | |
if (!fullyLoaded) { | |
setTimeout(() => showTotalTime(timeLabels.length), 200); | |
return; | |
} | |
let timeSpan = document.createElement("span"); | |
timeSpan.innerText = ` • ${computeTotalTime(timeLabels)}`; | |
let stats = document.querySelectorAll('.metadata-stats > yt-formatted-string')[0]; | |
stats.appendChild(timeSpan); | |
} | |
window.addEventListener('load', () => showTotalTime(0), false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment