Sort QBittorrent Queue order

Sort your QBitorrent queue Size, Name, Date or any other available table sorting.

2023-12-31 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name         Sort QBittorrent Queue order
// @namespace    http://tampermonkey.net/
// @version      2023-12-31
// @description  Sort your QBitorrent queue Size, Name, Date or any other available table sorting.
// @author       Is not important
// @match        http://localhost:8080/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=undefined.localhost
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create a button element
    var button = document.createElement("button");

    // Set button attributes and styles
    button.innerHTML = "<b>Sort Queue order</b> <br /> As your current ordered table";
    button.style.position = "fixed";
    button.style.right = "10px"; // Adjust the number of pixels from the right
    button.style.top = "20%"; // Vertical alignment in the middle
    button.style.transform = "translateY(-50%)";
    button.style.border = "2px solid red";
    button.style.backgroundColor = "orange";
    button.style.padding = "30px";
    button.style.cursor = "pointer";

    // Add click event listener
    button.addEventListener("click", sortTable);

    // Append the button to the body
    document.body.appendChild(button);


    // Function to sort the queue order by the current sorted table. Be aware that the sorting uses the bottom of the queue, so if let's say you have 100 torrents in multiple categories, and you want to sort just e.g. 40 torrents in a category
    // their queue order will be 41, 42, 43, ..., 100, if you want to move them at the top of the queue just select all of them and click the "Move to the top of the queue" Button  in the Qbittorrent top toolbar.
    function sortTable(){
        // creates  a sorting status div element
        var sortingNotification = document.createElement("div");
        sortingNotification.innerHTML = "<b>Sorting in progress...</b>";
        sortingNotification.style.position = "fixed";
        sortingNotification.style.right = "10px"; // Adjust the number of pixels from the right
        sortingNotification.style.top = "30%"; // Vertical alignment in the middle
        sortingNotification.style.transform = "translateY(-50%)";
        sortingNotification.style.color = "black";
        sortingNotification.style.backgroundColor = "yellow";
        sortingNotification.style.padding = "20px 58px 20px 58px";

        // Append status div to the body
        document.body.appendChild(sortingNotification);
        // Gets current table rows
        var tableRows = document.querySelectorAll("#torrentsTableDiv > table > tbody > tr");
        // Loops over each row
        tableRows.forEach(function(row, index, array){
            // Qbittorrent move to bottom of queue button
            const orderButton = document.querySelector("#bottomPrioButton");

            setTimeout(function(){

                row.click();
                orderButton.click();

                if(index === array.length - 1){
                sortingNotification.style.padding = "20px 99px 20px 99px";
                sortingNotification.innerHTML = "<b>DONE!</b>";
                sortingNotification.style.backgroundColor = "lime";
            }

            },index * 1000);


        });
    }
})();