YouTube - Save to playlist menu sorted alphabetically

Save to playlist menu sorted alphabetically

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube - Save to playlist menu sorted alphabetically
// @namespace    DoniaCometa.YouTube.SaveToPlaylistAlphabetically
// @license      MIT
// @version      1.01
// @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() {
    let menuAddToPlaylists = getMenuAddToPlaylists();
    if (menuAddToPlaylists == null) {
        return null;
    }
    return menuAddToPlaylists.parentNode.parentNode;
}
function getMenuAddToPlaylistsIsVisible() {
    menuAddToPlaylistsVisibilityParent = getMenuAddToPlaylistsVisibilityParent();
    if (menuAddToPlaylistsVisibilityParent == null) {
        return false;
    }
    return window.getComputedStyle(menuAddToPlaylistsVisibilityParent).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() {
    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);
})();