Kuroi_mato_O / hf toolkit for my repo

// ==UserScript==
// @name         hf toolkit for my repo
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  straight from hell
// @author       Kuroi_Mato_O
// @require      https://cdnjs.cloudflare.com/ajax/libs/tinysort/3.2.5/tinysort.min.js
// @match        https://huggingface.co/KuroiMatoO/loras/tree/main
// @icon         https://www.google.com/s2/favicons?sz=64&domain=huggingface.co
// @license      MIT
// @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(3000);
                // https://cdnjs.cloudflare.com/ajax/libs/tinysort/3.2.5/tinysort.min.js
                tinysort.defaults.order = 'desc';
                tinysort('.mb-8 li',{selector:'time',attr:'datetime'});
                let loadButton = document.querySelectorAll('.flex-grow.items-center')[1];
                if(loadButton){
                    loadButton.click();
                    wait();
                    tinysort('.mb-8 li',{selector:'time',attr:'datetime'});
                    document.querySelector('li:has(a[href="/KuroiMatoO/loras/resolve/main/.gitattributes"])').remove();
                }
            }
            wait();
        }
        //create links window
    const linkContainer = document.body.appendChild(document.createElement('div'));
    linkContainer.id = 'linkContainer';

    const linkTextarea = linkContainer.appendChild(document.createElement('textarea'));
    const loraText = '%cd /content/stable-diffusion-webui/extensions/sd-webui-additional-networks/models/lora\n';
    linkTextarea.value = loraText;

    const buttonContainer = linkContainer.appendChild(document.createElement('div'));
    buttonContainer.id = 'buttonContainer';

    const leftButton = buttonContainer.appendChild(document.createElement('button'));
    leftButton.innerHTML = '♻';
    leftButton.classList.add('linkButtons');

    const linksPageNumber = buttonContainer.appendChild(document.createElement('div'));
    linksPageNumber.id = 'linksPageNumber';

    const rightButton = buttonContainer.appendChild(document.createElement('button'));
    rightButton.innerHTML = '⏩';
    rightButton.classList.add('linkButtons');


    //styling
    linkContainer.style = 'position: fixed; background: #111827;  width: 500px;  height: 250px;  top: 10px;  left: 20px;display: flex; justify-content: center; flex-wrap: wrap; border: 2px solid #323f50';
    linkTextarea.style = 'width: 100%; height: 80%; align-self: flex-start; resize: none; color: black; caret-color: black!important;';
    buttonContainer.style = 'width: 50%; height: 10%; display: flex; justify-content: space-between; background: #323f50; border-radius: 10px;';
    leftButton.style = 'width: 40%;';
    rightButton.style = 'width: 40%;';
    linksPageNumber.style = 'background: white; width: 10%; text-align: center; align-self: center; color: black; border-radius:50%;';

    //all rows
    let totalRows;
    let totalRowsLi;
    //get the number of rows with the same commit href
    let lastCommit;
    let totalNum = 0;
    let n = 0;
    let myHrefs;
    let href;
    function commitCheck(){
        totalRows = document.querySelectorAll('.mb-8 li:has(time)').length;
        lastCommit = document.querySelectorAll('.mb-8 li a.col-span-4[href*="/KuroiMatoO/loras/commit"]');
        console.log('n is ' + n)
        lastCommit[n].scrollIntoView();
        if (totalNum < totalRows - 5){
            while (lastCommit[totalNum].href === lastCommit[n].href) {
                totalNum++;
                console.log('total number is ' + totalNum);
            }
        } else {
            console.log(myHrefs[n]);
            linkTextarea.value = loraText;
            href = myHrefs[n].href;
            linkTextarea.value += `!wget ${href}\n`;
            linksPageNumber.innerHTML = totalNum;
        }
        wget();
        linksPageNumber.innerHTML = totalNum;

        console.log('total number is ' + totalNum);
        console.log('n is ' + n)
    }

    //write all hrefs of the same commit inside textarea

    function wget(){
        totalRowsLi = document.querySelectorAll('.mb-8 li:has(time)');
        myHrefs = document.querySelectorAll(".mb-8 li:has(time) a[title='Download file']");
        console.log('inside wget n ' + n);
        console.log('inside wget totalNum ' + totalNum);
        for (let i = 0; i < totalRows; i++) {
        totalRowsLi[i].style = 'background: rgb(17 24 39);'
        }
        for (n; n < totalNum; n++) {
            href = myHrefs[n].href;
            totalRowsLi[n].style = 'background: rgb(19 31 56);'
            linkTextarea.value += `!wget ${href}\n`;
        }
        console.log('inside wget n2 ' + n);
        console.log('inside wget totalNum2 ' + totalNum);
    }

    //next commit
    function rightClick(){
        if (totalNum == 0){
            linkTextarea.value = loraText;
            commitCheck();
        }
        else if (totalNum < totalRows - 1){
            linkTextarea.value = loraText;
            commitCheck();
            } else {
                linkTextarea.value = loraText;
                href = myHrefs[n].href;
                linkTextarea.value += `!wget ${href}\n`;
                linksPageNumber.innerHTML = totalNum;
                console.log('eblan')
            }
    }

    //reset
    function leftClick(){
        n = 0;
        totalNum = 0;
        linkTextarea.value = '';
    }

    rightButton.addEventListener("click", rightClick);
    leftButton.addEventListener("click", leftClick);
    }
})();