Skip to content

Instantly share code, notes, and snippets.

@theodrosyimer
Last active February 29, 2024 23:54
Show Gist options
  • Save theodrosyimer/0e0d411ff604930351f366e84c2b4695 to your computer and use it in GitHub Desktop.
Save theodrosyimer/0e0d411ff604930351f366e84c2b4695 to your computer and use it in GitHub Desktop.
get-youtube-timestamps
console.log(getYoutubeTimestamps())
// works for videos less than 100 hours (should be enough, but easily extensible)
function getYoutubeTimestamps({withMilliseconds = true} = {}) {
if ($('#right-arrow')) {
$('#right-arrow').click()
}
let chaptersTimestampList = [...$$('[slot="extra-content"] #scroll-container #endpoint > #details:not([hidden]) #time')].map(item => ({
timestamp: item.textContent,
title: item.previousElementSibling.textContent
}))
if (!chaptersTimestampList) {
chaptersTimestampList = [...$$('#scroll-container > #items #details:not([hidden]) > #time')].map(item => ({
timestamp: item.textContent,
title: item.previousElementSibling.textContent
}))
}
return chaptersTimestampList.map(item => {
const timestampSplitted = item.timestamp.split(':')
let hours, minutes, seconds = ''
if (timestampSplitted.length === 2) {
[minutes, seconds] = timestampSplitted
hours = '00'
}
if (timestampSplitted.length === 3) {
[hours = '00', minutes, seconds] = timestampSplitted
}
minutes = `0${minutes}`.slice(-2)
hours = hours !== '00' ? `0${hours}`.slice(-2) : hours
let timestamp = `${hours}:${minutes}:${seconds}`
if (withMilliseconds) {
timestamp = `${timestamp}.000`
}
return `${timestamp} ${item.title}`
}).join('\n')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment