Sort confluence "My Spaces" alphabetically

Alphabetically Sort confluence "My Spaces"

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Sort confluence "My Spaces" alphabetically
// @namespace    http://tampermonkey.net/
// @version      1.01
// @description  Alphabetically Sort confluence "My Spaces"
// @author       You
// @match        https://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @require      https://code.jquery.com/jquery-3.6.3.min.js
// @license      MIT
// ==/UserScript==

/* global jQuery */

function capitalizeFirstLetter(val) {
    return val.charAt(0).toUpperCase() + val.toLowerCase().slice(1);
}

jQuery(function(){
    'use strict';

    setTimeout(() => {
        let mySpaces = {};
        let list = jQuery('#sidebar-spaces').find('ul:first');

        list.find('li').each(function(){
            let val = jQuery(this).html();
            let name = capitalizeFirstLetter(jQuery(this).find('.aui-nav-item-label').html());
            mySpaces[name] = val;
        });

        const mySpacesOrdered = Object.keys(mySpaces).sort().reduce(
            (obj, key) => {
                obj[key] = mySpaces[key];
                return obj;
            },
            {}
        );

        list.html('');
        for (const [name, value] of Object.entries(mySpacesOrdered)) {
            jQuery('<li class="item" />').append(value).appendTo(list);
        }
    }, "500");

});