Created
July 3, 2019 03:07
-
-
Save cesarandreu/0efd7f8ee8c4e4360239fedb2d5a18eb to your computer and use it in GitHub Desktop.
Sort the visible list of videos by views
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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())
.