Last active
November 28, 2022 09:17
-
-
Save patrickroberts/b859f19354c0ac6b66d1130c4951f1c5 to your computer and use it in GitHub Desktop.
YouTube Subscription Sorter
This file contains 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
// ==UserScript== | |
// @name YouTube Subscription Sorter | |
// @version 0.3 | |
// @author Patrick Roberts | |
// @match https://www.youtube.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var masthead = document.querySelector('#masthead'); | |
var guide = document.querySelector('#guide-inner-content'); | |
if (masthead && masthead.onGuideToggleHover_ && !main()) { | |
var observer = new MutationObserver(main); | |
observer.observe(guide, { childList: true, subtree: true }); | |
masthead.onGuideToggleHover_(); | |
} | |
function main() { | |
var section = document.querySelectorAll('#items')[1]; | |
if (!section) return false; | |
var showMore = section.querySelector('ytd-guide-collapsible-entry-renderer'); | |
if (!showMore || !showMore.fillRemainingListItems) return false; | |
showMore.fillRemainingListItems(); | |
if (observer && observer.disconnect) observer.disconnect(); | |
var subscriptions = Array.from(section.querySelectorAll('ytd-guide-entry-renderer:not([id])')); | |
subscriptions.pop(); // Browse channels | |
var parents = subscriptions.map(function(subscription) { | |
return subscription.parentElement; | |
}); | |
sort(subscriptions, function(subscription) { | |
return subscription.querySelector('a').title; | |
}, new Intl.Collator().compare).forEach(function(subscription, index, array) { | |
if (index === 0 || parents[index - 1] !== parents[index]) { | |
parents[index].insertBefore(subscription, parents[index].firstElementChild); | |
} else { | |
parents[index].insertBefore(subscription, array[index - 1].nextElementSibling); | |
} | |
}); | |
return true; | |
function sort(array, key, compare) { | |
array.map(function(value) { | |
return { key: key(value), value: value }; | |
}).sort(function(a, b) { | |
return compare(a.key, b.key); | |
}).forEach(function(entry, index) { | |
array[index] = entry.value; | |
}); | |
return array; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment