Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bookmarklet for YouTube: Sort videos by length in videos tab

Bookmarklet for YouTube: Sort videos by length in videos tab

On channels in videos tab you can sort videos by:

  • Most popular
  • Date added (oldest)
  • Date added (newest)

But you cannot sort by length. So I decided to create a bookmarklet to do it (in ascending order).

It doesn't do any requests on its own, it just reorders already shown items on the videos tab. So if you'll scroll the page and YT will load more videos, newly added ones won't be sorted, you will need to rerun bookmarklet again.

Tested in Firefox (69). Unfortunately in Chrome (76) it triggers reloading list of videos, but I haven't investigated why and how to fix it.

In your Bookmarks Toolbar select New Bookmark... from context menu, and in New Bookmark window input whatever name you like (I suggest: YouTube: Sort videos by length) and use following string as location (it's a one long one-liner):

javascript:(function(){l=[].map.call(document.querySelectorAll("span.ytd-thumbnail-overlay-time-status-renderer"),function(e){l=e.innerHTML.trim().split(":").map(function(t){return parseInt(t)}).reduce(function(p,c){return p*60+c});return{a:e.parentElement.parentElement.parentElement,l:l}}).sort(function(a,b){return a.l-b.l});i=document.querySelector("#primary #items");l.forEach(function(o){i.appendChild(o.a.parentElement.parentElement.parentElement)})})()

Warning: It is only meant to be used on videos tabs, i.e. following kind of URLs:

  • https://www.youtube.com/channel/CHANNEL/videos
  • https://www.youtube.com/user/USER/videos

Hope it will serve you well (until YT will change how the videos page is generated).

If you know browser extensions or add-ons enhancing YT sorting features, then please share it.

EDIT (2019-09-08): I was notified that recently the original bookmarklet code was no longer working, so I fixed it.

@przemoc

This comment has been minimized.

Copy link
Owner Author

@przemoc przemoc commented Sep 8, 2019

I created this gist, because my post was removed in other place after I updated it. But later it came out it wasn't intentional (it was just an automatic bot action). Anyway, I'll continue having copy of it here.

I forgot I had a dedicated gist for bookmarklet code alone: yt-sort-videos-by-length.js. I updated it too.

@iam-Shashank

This comment has been minimized.

Copy link

@iam-Shashank iam-Shashank commented Feb 24, 2020

Nice work. I know it's possible via a chrome extension, but would it be possible to display Total Playlist Length via a bookmarklet like this? It would also have to account for scrolling(for long playlists)? I use an extension for this at the moment, generally use it to calculate total length for a lecture series (when studying one day before college exams !)

@habdulrauf

This comment has been minimized.

Copy link

@habdulrauf habdulrauf commented Jun 26, 2020

I was looking for such solution since long. Thank you @przemoc.
It is working fine on FireFox Browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.