YouTube - Save to playlist menu sorted alphabetically

Save to playlist menu sorted alphabetically

Stan na 25-08-2022. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         YouTube - Save to playlist menu sorted alphabetically
// @namespace    DoniaCometa.YouTube.SaveToPlaylistAlphabetically
// @license      MIT
// @version      1.0
// @description  Save to playlist menu sorted alphabetically
// @author       DoniaCometa
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @match        http*://*.youtube.com/*
// ==/UserScript==
/************************************************************************/
function getMenuAddToPlaylists() {
    return document.getElementById("playlists");
}
function getMenuAddToPlaylistsVisibilityParent() {
    return getMenuAddToPlaylists().parentNode.parentNode;
}
function getMenuAddToPlaylistsIsVisible() {
    return window.getComputedStyle(getMenuAddToPlaylistsVisibilityParent()).display === "block";
}
function stringLocaleCompare(a, b) {
    // for sorting string with emojis icons/emojis and keeping them on top
    // https://stackoverflow.com/questions/59589337/in-javascript-sorting-strings-with-numbers-and-special-characters
    return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
}
function sortMenuAddToPlaylists() {
    function getPlaylistTitle(playlistElement) {
        return playlistElement.children[0].children[1].children[0].children[0].children[0].title.toLowerCase();
    }
    let playlists = getMenuAddToPlaylists();
    let sorted = true;
    while(sorted) {
        sorted = false;
        for (let i = 1; i < playlists.children.length - 1; i++) {
            let a = playlists.children[i];
            let b = playlists.children[i + 1];
            if (stringLocaleCompare(getPlaylistTitle(a), getPlaylistTitle(b)) > 0) {
                playlists.insertBefore(b, a);
                sorted = true;
            }
        }
    }
}
function canInit() {
    return getMenuAddToPlaylistsIsVisible();
}
function init() {
    console.log("init");
    /*
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutationRecord) {
            if (getMenuAddToPlaylistsIsVisible()) {
                sortMenuAddToPlaylists();
            }
        });
    });
    observer.observe(getMenuAddToPlaylistsVisibilityParent(), { attributes : true, attributeFilter : ['style'] });
    sortMenuAddToPlaylists();
    */
    sortMenuAddToPlaylists();
    let intervalId = window.setInterval(function(){
        if (getMenuAddToPlaylistsIsVisible()) {
            sortMenuAddToPlaylists();
        }
    }, 1);
}
/************************************************************************/
(function() {
    'use strict';
    // Your code here...
    let intervalId = window.setInterval(function(){
        if (canInit()) {
            init();
            clearInterval(intervalId);
        }
    }, 100);
})();