Skip to content

Instantly share code, notes, and snippets.

@cesarandreu
Created July 3, 2019 03:07
Show Gist options
  • Save cesarandreu/0efd7f8ee8c4e4360239fedb2d5a18eb to your computer and use it in GitHub Desktop.
Save cesarandreu/0efd7f8ee8c4e4360239fedb2d5a18eb to your computer and use it in GitHub Desktop.
Sort the visible list of videos by views
function youtubeSort () {
const channel = document.querySelector('#channel-title').textContent
const videos = [...document.querySelectorAll('#items > ytd-grid-video-renderer')]
.map(item => {
const metadataLine = item.querySelector('#details #meta #metadata-line')
const metadataViews = metadataLine.querySelector(':nth-child(1)').textContent
const metadataDate = metadataLine.querySelector(':nth-child(2)').textContent
const videoTitle = item.querySelector('#details #meta #video-title')
const href = videoTitle.href
const title = videoTitle.textContent
const humanizedDate = videoTitle.getAttribute('aria-label')
.replace(`${title} by ${channel} `, '')
.split(' ').slice(0, -2).join(' ')
const humanizedViews = videoTitle.getAttribute('aria-label')
.replace(`${title} by ${channel} `, '')
.match(/([0-9,]+) views/)[1]
const views = Number.parseInt(humanizedViews.replace(',', ''), 10)
return {
metadataViews,
metadataDate,
href,
title,
humanizedDate,
humanizedViews,
views,
}
})
.sort((a, b) => {
return b.views - a.views
})
return videos
}
@cesarandreu
Copy link
Author

Go to a channel's video list and scroll down until you've loaded enough videos. Then copy the above function and run copy(youtubeSort()).

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