TBD: Sands of Seed Time for TorrentBD

Sorts your torrent download history based on seed time and lets you download all visible torrents at once.

// ==UserScript==
// @name         TBD: Sands of Seed Time for TorrentBD
// @namespace    https://naeembolchhi.github.io/
// @version      0.4
// @description  Sorts your torrent download history based on seed time and lets you download all visible torrents at once.
// @author       NaeemBolchhi
// @license      GPL-3.0-or-later
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAAAXNSR0IB2cksfwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAMO5JREFUeJztnQd4VMfV953EKXaSN8n35f2SvMkbY4JbSGztCkwx2ICxcXDBBRAdTLPpHQzYFIMB0w0GDFqtGkK9S6sGSAgVigAhelEXogiLou0C5ptZaYXKlru37Jm7mvM8vwcQ0mpm7v2faWfOPPEEkEWMf+G3MaNffD56zEv/jB/zQreYsc/3ih37wgD85/uYITFjnhsVM+b58dGjn5sSNabTjOgxnebjry2KHt1pGWZF1Mh/rMWsixzRcVPk8I6bI0Y8u93C8Gd3EsKHPfNDuI+F3WFD/96Az98Dw3ye2YcJxV+PxySE+3TQRAzrcJAQPqxDLv7zCP6co/Hj/3k6eYpXfso05QFMdiN5mKONnMScaqQQc74VlzHFrahqxXVMTSvu2UGHMXLD26SZ3sWcPL1rPf77I/w1xA/vR+QzyGeRz+T++5VaO3W4a6O+1TbapXW7XbLRvoXN2v9Es+eS0+x57cdkYNIxSY3EY6IbCceENhLQiBqzm5A6Tbkd/7kFsxHzDW6DlSnTlUvw1xemTFXOxl+bjpmcMk3xKf5zlGaqcmjqNO+PNdMU72IG4O/ti+mdMt37VYx36gxFZ8xLqVO9n02bonxGM7XL/0ua0vWPCZO6/hxKh26zmNHPfxzh02FfxPAO1Vh4D7EgEW1g8aOECZ2RZqqCp2AYDN7UpjQ4sC3YWbwErVfRLHpUx9G4Zy2BFrczYse9wITPoIksPLJQQOuXt6WO8n4qcvizqdDC5kLCxM7QD5vBsMUDzIbMqZ1/Bq1nlyx+/At/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EbFpydiYFWNvQr/YDMdELR9Zum/TYp8VEefYUJ+Z+Bb63apB0as/y2VRhfRAovcil42MCfh+Dbtym5l7zDcyTRm+duZ3iYveewAtgPZK/OL3jZGrJi7/wS/gGej3gVk7NZVK9WTE+vkjo1Z/XqiZ2QNcFJ6OZvZrKHLFmIy9m78cvOb7QDbMZ0aP7Yo4+FLs6s9nxywffTdlmje4WDyG6d4o6uux16K+mTR1l0r1F+jnzIyZU1Or1d1ivxoxN+briXdSpneBF5HcwG0WtXJMddyywTP9dm3sDP08mTHjbXtCknvGLhkyJXjuoPzEGd3ZISQ7xM3s/TBg/uDsiCWDJ+/Y7f8i9HNjxkx0m/5t0G82fDn3g6B5H23dN+fd29Cig8Ub7Z37QTVm9YalcwfNXuv7NPTzYcbMrbZ65cq/71w03idw3sdbA+Z9ci15uueeTkya3hUFzRtUtW/uu+u+/2KSz4oVq/4G3f7MmFFla7+Y/7vvlkx5J2z2O5N8F4yMwo6hJkmGToGUee/c92/5zx8cgXv4Seu/nDtg0TfbfgPdvsyYyc42fzXn1xuXzuntu2DUqKiZfefgkUJ88NwPKuJm9qqHFnr0rD71gfM/qVQtGJYQOmfgvJ1fTBi1etnS3gvW7GIptZgxk9pmrPb77y9Wbeq+9qvFg/YsHDU2bmbvz/bNGbhJPd8nTr1g2JGAeYOvBs8dVBM+e4A+fPbbZizYB7Ezez9KmNHjkQbPwTXTuyDy99iZrz8k/xc++y0THn3o8M/cCpg/+DL+jDz1gqHRYXPe+RZ/3+SAeUPGbVo668OVK1b0/HrF8j9C15+ZMPv/c7PqJRPONJEAAAAASUVORK5CYII=
// @match        https://*.torrentbd.com/download-history.php*
// @match        https://*.torrentbd.net/download-history.php*
// @match        https://*.torrentbd.org/download-history.php*
// @match        https://*.torrentbd.me/download-history.php*
// @require      https://cdnjs.cloudflare.com/ajax/libs/localforage/1.10.0/localforage.min.js
// @run-at       document-idle
// @grant        none
// ==/UserScript==

(function() {
// Inline style for iFrames
const frameStyle = "display: block;position: fixed;top: -1000px;left: -1000px;height: 1px;width: 1px;opacity: 0;z-index: -9001;overflow: hidden";

// Setting and receiving JSON data from local storage.
function setJSON(key, data) {
    if (typeof data != "string") {data = JSON.stringify(data);}
    localStorage.setItem(key, data);
}
function getJSON(key) {
    return JSON.parse(localStorage.getItem(key));
}

// Create and add to localJSON
function pushLocal(key, val) {
    if (!localStorage.hisTable) {
        setJSON("hisTable",{});
    }
    let tempJSON = getJSON("hisTable");
    //tempJSON["page" + key] = encodeURIComponent(val);
    if (!tempJSON["page" + key]) {
        tempJSON["page" + key] = "";
    }
    tempJSON["page" + key] = tempJSON["page" + key] + val;
    setJSON("hisTable",tempJSON);
}

// Scrape variables from the URL
function urlVar(url) {
    let vars = {};
    url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}

// Create a new element
function elemake(tag, innr, attr) {
    let element = document.createElement(tag);
    if (innr) {element.innerHTML = innr;}
    if (!attr) {return element;}

    for (let x = 0; x < attr.key.length; x++) {
        element.setAttribute(attr.key[x], attr.val[x]);
    }
    return element;
}

// Spawn new iFrames
function spawnFrame(url) {
    let frame = document.createElement("iframe");
    frame.id = "spawn_" + urlVar(url).page;
    frame.src = url + "&iframe";
    frame.setAttribute("style",frameStyle);
    document.body.appendChild(frame);
}

// Compress html
function getSmall(str) {
    return str.replace('<tr><td><a href="torrents-details.php?id=',"৳1")
              .replace('" target="_blank">',"৳2")
              .replace('</a><div class="margin-t-5" style="font-size: .9em;">Uploaded by <a href="account-details.php?id=',"৳3")
              .replace('" target="_blank"><span class="tbdrank',"৳4")
              .replace(window.location.origin + "/images/","৳5")
              .replace('</span></a></div></td><td class="dl-btn-td center-align"><a href="download.php?id=',"৳6")
              .replace('"><i class="material-icons small">file_download</i></a></td><td>',"৳7")
              .replace('</td><td class="completed-td">',"৳8")
              .replace('</td><td>',"৳9")
              .replace('</td><td class="active-status-td">',"৳A")
              .replace('<span class="text-gray" title="No">⚫</span></td></tr>',"৳B")
              .replace('<i class="material-icons green-text" title="Yes">check</i></td></tr>',"৳C");
}

// Decompress html
function getLarge(str) {
    return str.replace("৳1",'<tr><td><a href="torrents-details.php?id=')
              .replace("৳2",'" target="_blank">')
              .replace("৳3",'</a><div class="margin-t-5" style="font-size: .9em;">Uploaded by <a href="account-details.php?id=')
              .replace("৳4",'" target="_blank"><span class="tbdrank')
              .replace("৳5",window.location.origin + "/images/")
              .replace("৳6",'</span></a></div></td><td class="dl-btn-td center-align"><a href="download.php?id=')
              .replace("৳7",'"><i class="material-icons small">file_download</i></a></td><td>')
              .replace("৳8",'</td><td class="completed-td">')
              .replace("৳9",'</td><td>')
              .replace("৳A",'</td><td class="active-status-td">')
              .replace("৳B",'<span class="text-gray" title="No">⚫</span></td></tr>')
              .replace("৳C",'<i class="material-icons green-text" title="Yes">check</i></td></tr>');
}

// Base URL
let hisURL = window.location.origin + window.location.pathname + "?id=" + urlVar(document.querySelector("#left-block-container a.accc-btn[href*='account-details']").href).id;

// Divide tasks based on URL
if (window.location.href.match(/iframe/)) {
    /* inside iframe */
    // Run when page finishes loading
    function onReady() {
        if (document.readyState !== "complete") {return;}
        let tbody = document.querySelector("#middle-block table > tbody");
        tbody.innerHTML = tbody.innerHTML.replace(/\n/g,"").replace(/\s\s/g,"").replace(/\s\s/g,"").replace(/>\s*</g,"><");

        let trall = document.querySelectorAll("#middle-block table > tbody > tr"),
            stime = document.querySelectorAll("#middle-block table > tbody > tr > td:nth-child(5)"),
            arrayAlpha;

        localforage.getItem("arrayData").then(function(value) {
            if (value === null) {
                arrayAlpha = [];
            } else {
                arrayAlpha = value;
            }
        }).then(function() {
            for (let x = 0; x < stime.length; x++) {
                let torData = getSmall(trall[x].outerHTML),
                    torTime = eval(stime[x].innerText.replace(/([0-9]*)y/i,"($1*12*30*24)").replace(/([0-9]*)mo/i,"($1*30*24)").replace(/([0-9]*)d/i,"($1*24)").replace(/([0-9]*)h/i,"$1").replace(/([0-9]*)m/i,"($1/60)").replace(/\s/g,"+").replace("-","0"));

                arrayAlpha.push({"X":torData,"Y":torTime.toString()});
            }
            finalize();
        });

        function finalize() {
            localforage.setItem("arrayData", arrayAlpha).then(function() {
                window.parent.postMessage("down_hist_spawn_" + urlVar(window.location.href).page);
            });
        }
    };
    onReady();

    // Listen for readystate change
    document.addEventListener("readystatechange", onReady);
} else {
    /* outside iframe */
    // Clear button is search is on
    if (window.location.search.match(/torrent/)) {
        document.querySelector("#middle-block form button[type='submit']").parentNode.appendChild(elemake("button",'Reset<i class="material-icons right">clear_all</i>',{"key":["id","class","style","type"],"val":["hisReset","btn light-blue darken-3","margin-left: 6px","button"]}));
        document.getElementById("hisReset").addEventListener("click",function() {
            window.location.href = hisURL;
        });
        return;
    }

    // Page Count
    let pages = document.querySelectorAll(".pagination li a[href*='page']"),
        lastpage = parseInt(urlVar(pages[pages.length-1].href).page),
        dialText = "An unknown error occured. Please report to the dedicated thread.",
        redoBtn = "", proSate = "";

    if (localStorage.hisTable && !JSON.parse(localStorage.hisTable).pageSorted) {
        dialText = 'Incomplete data from a previous session has been discovered. As such, the process needs to be restarted.<br><br><b class="red-text">This action cannot be paused.</b><br><br>Do you wish to proceed?';
        proSate = "incomplete";
    }
    if (localStorage.hisTable && JSON.parse(localStorage.hisTable).pageSorted) {
        dialText = 'Complete data from a previous session has been discovered. As such, you can sort your torrents right away.<br><br><b class="red-text">You can also wipe the saved data and redo the process.</b><br><br>What would you like to do?';
        redoBtn = '<div class="swal-button-container"><button class="swal-button swal-button--redo">Redo</button></div>';
        proSate = "complete";
    }
    if (!localStorage.hisTable) {
        // Add page count to localJSON
        pushLocal("Count", lastpage);
        dialText = 'This will save your torrent download history in the browser and sort them based on seed time. As such, it may take some time to complete.<br><br><b class="red-text">This action cannot be paused.</b><br><br>Do you wish to proceed?';
        proSate = "blank";
    }

    // Generate new button
    document.querySelector("#middle-block form button[type='submit']").parentNode.appendChild(elemake("button",'Sort Torrents<i class="material-icons right">access_time</i>',{"key":["id","class","style","type"],"val":["hisBtn","btn light-blue darken-3","margin-left: 6px","button"]}));
    document.getElementById("hisBtn").addEventListener("click",createDial);

    // functions for creating and deleting the dialogue box
    function createDial() {
        document.body.appendChild(elemake("div",`<div class="swal-modal" role="dialog" aria-modal="true"><div class="swal-text">${dialText}</div><div class="swal-footer"><div class="swal-button-container"><button class="swal-button swal-button--cancel">Cancel</button></div>${redoBtn}<div class="swal-button-container"><button class="swal-button swal-button--confirm">Proceed</button></div></div></div>`,{"key":["id","class","tabindex"],"val":["hisDial","swal-overlay swal-overlay--show-modal","-1"]}));
        document.querySelector("#hisDial .swal-button--cancel").addEventListener("click",deleteDial);
        document.querySelector("#hisDial .swal-button--confirm").addEventListener("click",followDial);
        try {
            document.querySelector("#hisDial .swal-button--redo").addEventListener("click",wipeData);
        } catch(e) {}
    }
    function deleteDial() {
        document.getElementById("hisDial").remove();
    }

    // Wipe saved data and redo the process
    function wipeData() {
        localforage.removeItem("arrayData").then(function() {
            localStorage.removeItem("hisTable");
            saveData();
        });
    }

    // Forwarding function for the proceed button
    function followDial() {
        if (proSate === "blank") {
            saveData();
        }
        if (proSate === "incomplete") {
            wipeData();
        }
        if (proSate === "complete") {
            updateDial('All torrents are already saved in the browser. Please wait as the table is updated.<br><br><b class="red-text">Starting update...</b>');
            setTimeout(doExtend, 1000);
        }
    }

    // Function for updating dialogue text
    function updateDial(key) {
        document.querySelector("#hisDial .swal-footer").innerHTML = '<div class="swal-button-container"><button class="swal-button swal-button--cancel">Cancel</button></div>';
        document.querySelector("#hisDial .swal-button--cancel").addEventListener("click",function() {
            deleteDial();
            window.location.reload(true);
        });

        document.querySelector("#hisDial .swal-text").innerHTML = key;
    }

    // Function for updating dialogue text **final version**
    function finalDial(key) {
        document.querySelector("#hisDial .swal-footer").innerHTML = '<div class="swal-button-container"><button class="swal-button swal-button--confirm">Exit</button></div>';
        document.querySelector("#hisDial .swal-button--confirm").addEventListener("click",deleteDial);

        document.querySelector("#hisDial .swal-text").innerHTML = key;
    }

    // Make Download All button
    function downBtn() {
        document.querySelector("#middle-block form button[type='submit']").parentNode.appendChild(elemake("button",'Download Torrents<i class="material-icons right">file_download</i>',{"key":["id","class","style","type"],"val":["hisDown","btn light-blue darken-3","margin-left: 6px","button"]}));
        document.getElementById("hisDown").addEventListener("click",downAll);
    }

    // Download one torrent
    function downOne(url, name) {
        fetch(url)
          .then(response => response.blob())
          .then(blob => {
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement("a");
            a.href = url;
            a.download = "[TorrentBD]" + name + ".torrent";
            document.body.appendChild(a);
            a.click();
            a.remove();
            window.URL.revokeObjectURL(url);
          })
          .catch(error => {
            console.error('Error:', error);
          });
      }

    // Download All Visible Torrents
    function downAll() {
        let torLinks = document.querySelectorAll("#middle-block table tbody a[href*='torrents-details']");

        for (let x = 0; x < torLinks.length; x++) {
            let torId = urlVar(torLinks[x].href).id,
                torName = torLinks[x].innerText,
                torLink = window.location.origin + "/download.php?id=" + torId;

            downOne(torLink, torName);
        }
    }

    // Sort and update localJSON based on sort data
    function doSort() {
        let arrayOne;

        localforage.getItem("arrayData").then(function(value) {
            arrayOne = value.sort(function(a, b) {return parseFloat(a.Y) - parseFloat(b.Y);});
            finalize();
        });

        function finalize() {
            localforage.setItem("arrayData", arrayOne).then(function() {
                pushLocal("Sorted", arrayOne.length.toString());

                finalDial('<b class="orange-text">Please wait...</b><br><br>All torrents have been sorted and saved locally. The torrents table will be updated shortly.');
                setTimeout(doExtend, 1000);
            });
        }
    }

    // Update torrent list from saved data
    function doExtend() {
        // Update pagination
        let pagi = document.querySelector("#middle-block .pagination-block"),
            torrentCount = parseInt(getJSON("hisTable").pageSorted),
            eachPage = 30,
            totalPages, currentPage;

        if (parseInt(torrentCount / eachPage) < (torrentCount / eachPage)) {
            totalPages = parseInt(torrentCount / eachPage) + 1;
        } else {
            totalPages = parseInt(torrentCount / eachPage);
        }

        pagi.innerHTML = `
        <style>
            .hisPagination {display: flex;align-items: center;justify-content: center;}
            .hisLi {display: flex;align-items: center;justify-content: center;}
            #hisSelect {display: block;height: 23.6333px;min-width: 31px;padding: 0 5px;outline: 0;border: 0;background: var(--main-bg);cursor: pointer;color: var(--body-color);}
        </style>
        <ul class="pagination hisPagination">
            <li><a class="waves-effect" title="First"><i class="material-icons">first_page</i></a></li>
            <li><a class="waves-effect" title="Previous"><i class="material-icons">chevron_left</i></a></li>
            <li class="hisLi"><select id="hisSelect" title="Jump to">${hisOptions()}</select></li>
            <li><a class="waves-effect" title="Next"><i class="material-icons">chevron_right</i></a></li>
            <li><a class="waves-effect" title="Last"><i class="material-icons">last_page</i></a></li>
        </ul>`;

        // Make enough pages in the options list
        function hisOptions() {
            let print = "";

            for (let x = 1; x < (totalPages + 1); x++) {
                print = print + `<option value="${x}">${x}</option>`;
            }

            return print;
        }

        // Load torrents for intended pages
        function loadTorrents(page) {
            let print = "",
                start = ((page * eachPage) - eachPage),
                end = (page * eachPage);

            if (end > torrentCount) {
                end = torrentCount;
            }

            localforage.getItem("arrayData").then(function(value) {
                for (let x = start; x < end; x++) {
                    print = print + getLarge(value[x].X);
                }
            }).then(function() {
                document.querySelector("#middle-block table > tbody").innerHTML = print;
                document.getElementById("hisSelect").value = page;
                currentPage = page;
                try {
                    finalDial('<b class="green-text">Success!</b><br><br>Torrents in your download history are now sorted based on seed time. You may safely exit this dialogue.');
                } catch(e) {}
            });
        }
        loadTorrents(1);

        // Event listeners to make the pagination functional
        let pagiNode = document.querySelector("#middle-block .pagination-block .hisPagination");
        pagiNode.children[0].addEventListener("click",function(){loadTorrents(1)});
        pagiNode.children[1].addEventListener("click",function(){loadTorrents(currentPage - 1)});
        pagiNode.children[2].children[0].addEventListener("change",function(){loadTorrents(pagiNode.children[2].children[0].value)});
        pagiNode.children[3].addEventListener("click",function(){loadTorrents(currentPage + 1)});
        pagiNode.children[4].addEventListener("click",function(){loadTorrents(totalPages)});

        try {
            document.querySelector("#hisBtn").remove();
        } catch(e) {}
        downBtn();
    }

    // Main process for saving table data
    function saveData() {
        // Spawn an iframe
        spawnFrame(hisURL + "&page=1");
        updateDial(`<b class="orange-text">Progress: 1 of ${lastpage}</b><br><br>This will take some time to finish depending on your internet speed. Please wait patiently.`);

        // Set event listener for iframe messages
        window.addEventListener("message", function(event) {
            if (!event.data.match(/down_hist_spawn_/)) {return;}
            let frameId = event.data.replace(/down_hist_/,""),
                pageNo = parseInt(frameId.replace(/spawn_/,""));
            document.getElementById(frameId).remove();
            if (pageNo >= lastpage) {
                pushLocal("Complete", lastpage.toString());

                updateDial('<b class="green-text">Progress complete!</b><br><br>All torrents have been successfully saved in the browser. Please wait for the torrents to be sorted.');
                setTimeout(doSort,1000);
            } else {
                spawnFrame(hisURL + "&page=" + (pageNo + 1));
                document.querySelector("#hisDial .swal-text .orange-text").innerText = `Progress: ${(pageNo + 1)} of ${lastpage}`;
            }
        });
    }
}
})();