Skip to content

Instantly share code, notes, and snippets.

@cesarhilario
Forked from fredericogg/playlist_time.js
Last active November 21, 2023 14:31
Show Gist options
  • Save cesarhilario/28ec17d630f04fc3a3c915cfc56c0452 to your computer and use it in GitHub Desktop.
Save cesarhilario/28ec17d630f04fc3a3c915cfc56c0452 to your computer and use it in GitHub Desktop.
Calcula o tempo total de uma playlist no Youtube. É só colar no console na página da playlist. Update para a nova interface do Youtube
(function () {
const timeContainer = document.querySelectorAll(
".ytd-thumbnail-overlay-time-status-renderer"
);
let timeSeconds = 0;
for (let i = 0; i < timeContainer.length; i++) {
const timeStr = timeContainer[i].innerText;
if (!/\d?\d:\d{2}(:\d{2})?/g.test(timeStr)) continue;
const timeParts = timeStr.split(":");
let seconds;
if (timeParts.length === 3) {
seconds =
parseInt(timeParts[0]) * 60 * 60 +
parseInt(timeParts[1]) * 60 +
parseInt(timeParts[2]);
} else {
seconds = parseInt(timeParts[0]) * 60 + parseInt(timeParts[1]);
}
timeSeconds += seconds;
}
const renderNumber = (number) =>
number <= 9 ? `0${Math.round(number)}` : Math.round(number);
console.log(timeSeconds);
const hours = timeSeconds >= 60 * 60 ? timeSeconds / 60 / 60 : 0;
const minutes = timeSeconds >= 60 ? (timeSeconds / 60) % 60 : 0;
const seconds = timeSeconds % 60;
const result = `${renderNumber(hours)}:${renderNumber(
minutes
)}:${renderNumber(seconds)}`;
alert(result);
})();
@cesarhilario
Copy link
Author

Atualizado novamente!

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