Skip to content

Instantly share code, notes, and snippets.

@TSedlar
Last active November 28, 2022 09:19
Show Gist options
  • Save TSedlar/f7a3242e68dea734aeefb39906e27b39 to your computer and use it in GitHub Desktop.
Save TSedlar/f7a3242e68dea734aeefb39906e27b39 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name YouTube Sidebar Playlist Sorter (A-Z)
// @version 1.0.0
// @author Tyler Sedlar
// @description Sorts the YouTube sidebar playlists alphabetically
// @match http://*.youtube.com/
// @match https://*.youtube.com/
// @match http://*.youtube.com/*
// @match https://*.youtube.com/*
// ==/UserScript==
function ytText(element) {
return element.getElementsByTagName('a')[0].title
}
function sortChildren(parent, comparator) {
if (parent) {
Array.prototype.slice.call(parent.children)
.map(function(x) {
return parent.removeChild(x)
})
.sort(function(x, y) {
return comparator(x, y)
})
.forEach(function(x) {
parent.appendChild(x)
})
}
}
var menu = document.querySelector("button[aria-label=Guide]")
if (menu) {
menu.onclick = function() {
setTimeout(function() {
var btn = document.querySelector('a[title="Show more"]')
if (btn) {
btn.click()
sortChildren(document.querySelector('#expandable-items'), function(x, y) {
return ytText(x).localeCompare(ytText(y))
})
}
}, 1500)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment