Calculate qBittorrent selected torrents size

Calculates setected torrents size and displays a total in the toolbar

当前为 2024-01-03 提交的版本,查看 最新版本

// ==UserScript==
// @name         Calculate qBittorrent selected torrents size
// @namespace    http://tampermonkey.net/
// @version      v0.2
// @description  Calculates setected torrents size and displays a total in the toolbar
// @author       me
// @match        http://localhost:8080/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=undefined.localhost
// @grant        none
// @license      MIT

// ==/UserScript==
(function() {
    'use strict';

var trElement = document.querySelector("#desktopFooter > table > tbody > tr")

var selectedSizeTotalElement = document.createElement("td");
selectedSizeTotalElement.id = "selectedSizeTotal";
selectedSizeTotalElement.textContent = "Selected Torrents Total: 0.00";

var statusBarSeparatorElement = document.createElement("td");
statusBarSeparatorElement.className = "statusBarSeparator";

// Insert the new elements before the existing ones
trElement.insertBefore(selectedSizeTotalElement, trElement.firstElementChild);
trElement.insertBefore(statusBarSeparatorElement, trElement.firstElementChild.nextSibling);


var previousSelectedRows = [];
var totalSpanId = 'totalSpan';

function calculateTotal() {
    var kib = 0;
    var mib = 0;
    var gib = 0;
    var total = "";

    var selectedRows = document.querySelectorAll("tr.torrentsTableContextMenuTarget.selected");

    // Check if selectedRows array is bigger than 0
    if (selectedRows.length > 0) {
        // Check if selectedRows has changed
        if (!arraysAreEqual(previousSelectedRows, selectedRows)) {
            selectedRows.forEach(function (row) {
                var tSize = row.childNodes[3].innerHTML.split(" ");

                if (tSize[1] == "KiB") {
                    kib += parseFloat(tSize[0]);
                } else if (tSize[1] == "MiB") {
                    mib += parseFloat(tSize[0]);
                } else if (tSize[1] == "GiB") {
                    gib += parseFloat(tSize[0]);
                }
            });

            var localTotal = kib + mib * 1000 + gib * 1000000;

            // Check if total has changed
            if (localTotal.toFixed(2) !== total) {
                if (localTotal < 1000) {
                    total = localTotal.toFixed(2) + " KiB";
                } else if (localTotal < 1000000) {
                    total = (localTotal / 1000).toFixed(2) + " MiB";
                } else if (localTotal < 1000000000) {
                    total = (localTotal / 1000000).toFixed(2) + " GiB";
                } else {
                    total = (localTotal / 1000000000).toFixed(2) + " TiB";
                }

                
                var selectedSizeTotal = document.getElementById("selectedSizeTotal");
                selectedSizeTotal.innerHTML = "Selected Torrents Total: " + total;

            }

            // Update previousSelectedRows
            previousSelectedRows = Array.from(selectedRows);
        }
    }
}

// Run the function every 1 second
var intervalId = setInterval(calculateTotal, 1000);

// Helper function to compare two arrays
function arraysAreEqual(array1, array2) {
    return array1.length === array2.length && array1.every((value, index) => value === array2[index]);
}

})();