Skip to content

Instantly share code, notes, and snippets.

@przemoc
Last active December 11, 2024 21:01
Show Gist options
  • Save przemoc/533366f71db422652ee8fe5044250470 to your computer and use it in GitHub Desktop.
Save przemoc/533366f71db422652ee8fe5044250470 to your computer and use it in GitHub Desktop.
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 (and even old ones can be shuffled), you will need to rerun bookmarklet again.

Tested in Chrome (106), Edge (106), Firefox (106).

Installation

  • Chrome: In bookmarks bar select Add page... from context menu, you'll get new window.
  • Edge: In Bookmarks Toolbar select Manage favorites from context menu, in Favorites page click Add favorite, you'll get new window.
  • Firefox: In favorites bar select New Bookmark... from context menu, you'll get new window.

Put whatever name you like, use the following javascript: oneliner as URL, and save.

Usage

Click bookmark when you're on videos tabs in YouTube, i.e. following kind of URLs:

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

YouTube: Sort videos by length

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;});ctn="YTD-CONTINUATION-ITEM-RENDERER";i=document.querySelector("#primary #items");if(i&&i.childElementCount){c=i.lastElementChild;if(c.tagName==ctn){c.remove();}else{c=null;}l.forEach(function(o){i.appendChild(o.a.parentElement.parentElement.parentElement);});if(c)i.appendChild(c);}else{i=document.querySelector("#primary #contents");c=i.lastElementChild;if(c.tagName==ctn){c.remove();}else{c=null;}l.forEach(function(o){i.appendChild(o.a.parentElement.parentElement.parentElement.parentElement.parentElement);});if(c)i.appendChild(c);}})()

(If you prefer descending order, then change a.l-b.l into b.l-a.l.)

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

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.

EDIT (2022-10-24): During Sunday I realized it was not working on my Firefox on W11, seemingly it was seeing different HTML than other browsers were getting (my Firefox on W7 was not affected), so I played with it a bit and fixed it. Side effect: it works now on Chromium-based browsers too.

@melodromacy
Copy link

Doesn't seem to work? It just breaks and doesn't actually sort anything unless I'm not waiting long enough, but this playlist is only ~200 videos.

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