Kuroi_mato_O / huggingface sorting by date

// ==UserScript==
// @name         huggingface sorting by date
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  abomination for personal usage
// @author       Kuroi_Mato_O
// @match        https://huggingface.co/KuroiMatoO/loras/tree/main
// @license      MIT
// @icon         https://www.google.com/s2/favicons?sz=64&domain=huggingface.co
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    (new MutationObserver(check)).observe(document, {childList: true, subtree: true});

    function check(changes, observer) {
        if(document.querySelector('.mb-8')) {
            observer.disconnect();
            //ThE sLeEp FeAtuRe YeAhHh
            function sleep(ms) {
                return new Promise(resolve => setTimeout(resolve, ms));
            }
            async function wait() {
                await sleep(2000);
                //create array for date and time
                function sort(){
                    let time = document.querySelectorAll('.text-gray-400 time');
                    const timeArr = [];
                    for (let i = 0, element; (element = time[i]); i++) {
                        timeArr.push(element.dateTime);
                    }
                    //reverse array
                    let sortedArr = timeArr.sort();
                    let reversedArr = sortedArr.reverse();
                    //not gonna work without a grid
                    let li = document.querySelectorAll('.mb-8 li');
                    let container = document.querySelector('.mb-8');
                    container.style = 'display: grid;';
                    //pick a date-time from time array then find same date-time in reversed array and get its index
                    //this index is a new position of li element
                    for (let i = 0; i < reversedArr.length; i++){
                        li[i].style.order = reversedArr.indexOf(time[i].dateTime)
                    }
                }
                sort();
                let loadButton = document.querySelectorAll('.flex-grow.items-center')[1];
                loadButton.onclick = wait();
                sort();
            }
            wait();
        }}
})();