TJUPT helper

my personal script to enchance TJUPT experience

目前为 2022-12-18 提交的版本。查看 最新版本

// ==UserScript==
// @name         TJUPT helper
// @namespace    https://blog.miigon.net
// @version      0.1
// @license      GPL2
// @description  my personal script to enchance TJUPT experience
// @author       Miigon
// @match        https://tjupt.org/*
// @match        https://byr.pt/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant    GM_setClipboard
// @run-at      document-idle
// ==/UserScript==

/*
    VERSION 0.1:
      - supports tjupt and byrpt.
      - torrent page shows torrent downloader/uploader ratio.
      - clicking the downup ratio will copy the torrent link.
      - automatically loads next page of torrents and show them
        on current page, effectively doubling the page size.
*/

let link = window.location.href;

/* global $ */
console.log("$ = ", $);

function remove_space(str) { return str.replace(/\s/g, ""); }

function parse_torrent_table(context) {
    let torrents_table = $(".torrents > tbody > tr:gt(0)", context);
    // parse torrent table
    let torrents = []
    for(let i = 0; i < torrents_table.length; i++) {
        let tr = torrents_table[i]; // have to do this to support byr.pt
        let c = tr.children
        let torrent = {
            name: $(".torrentname a", c[1])[0].title,
            link: $(".torrentname a", c[1])[0].href,
            comments: Number(c[2].innerText),
            time: remove_space(c[3].innerText),
            size: remove_space(c[4].innerText),
            uploader: Number(c[5].innerText),
            downloader: Number(c[6].innerText),
            completes: Number(c[7].innerText),
            author: c[8].innerText,
            html_node: tr,
        }
        torrent.torrent_id = torrent.link.match(/id=(\d+)/)[1];
        torrent.downup_ratio = torrent.downloader / torrent.uploader;
        torrents.push(torrent);
    }
    return torrents;
}

function get_torrent_header(context) {
    return $(".torrents > tbody > tr:eq(0)", context);
}

function modify_torrent_header(context) {
    $("td:eq(6)", get_torrent_header(context)).after("<td class='colhead'><img src='https://tjupt.org/pic/s_dl.gif'/></td>");
}

function temporary_inner_text_override(elem, newtext) {
    let oldtext = elem.innerText;
    elem.innerText = newtext;
    setTimeout(()=>{elem.innerText = oldtext;},1000);
}

let passkey_parameter_str = null;
function find_passkey_parameter() {
    if(passkey_parameter_str != null) return passkey_parameter_str;
    if(link.indexOf("tjupt.org") != -1){
        let passkey = $("link[title='Latest Torrents']")[0].href.match(/passkey=(.*)/)[1];
        passkey_parameter_str = "&passkey=" + passkey;
    } else {
        passkey_parameter_str = "";
    }
    return passkey_parameter_str;
}

function modify_torrent_table(torrents, context) {
    // process & modify torrent table
    // downup_ratio
    for(let t of torrents) {
        let duratio_percent = Math.round(t.downup_ratio*100);
        if(duratio_percent == Infinity) {
            duratio_percent = "Inf";
        }
        let duratio_precent_node = $("<td class='rowfollow'><b><a href='#'>" + duratio_percent + "%</a></b></td>")
        duratio_precent_node[0].onclick = function(){
            // copy download link or download torrent file
            let passkey_parameter = find_passkey_parameter();
            let download_link = link.replace(/\/torrents.php.*/, "/download.php?id=" + t.torrent_id + passkey_parameter);
            if(passkey_parameter != "") {
                // has passkey, probably wants copy so the link can be used in a bt client.
                GM_setClipboard(download_link);
                temporary_inner_text_override($("a",duratio_precent_node)[0], "Copied!");
            } else {
                // does not have passkey, probably should download in browser, since the website might be using cookie to authenticate.
                $("body").after($("<iframe src='" + download_link + "'/ hidden>"));
            }
            return false;

        }

        $("td:nth-child(7)", t.html_node).after(duratio_precent_node);

    }
    return torrents;
}

function sort_torrent_table(torrents) {
    torrents.sort((a,b) => b.downup_ratio - a.downup_ratio);
}

function rebuild_torrent_table(torrents, context) {
    let new_tbody = $("<tbody/>");
    new_tbody.append(get_torrent_header(context));
    for(let t of torrents) {
        new_tbody.append(t.html_node);
    }
    $(".torrents > tbody", context).remove();
    $(".torrents", context).append(new_tbody);
}

console.log("link", link)
if(link.indexOf("/torrents.php") != -1) {
    if(link.indexOf("#subpage") == -1) { // parent page
        let torrents = parse_torrent_table(document);
        modify_torrent_header(document); // only need to do once
        modify_torrent_table(torrents, document);
        sort_torrent_table(torrents);
        rebuild_torrent_table(torrents, document);

        let page_regex = /[?&]page=(\d+)/
        let page_match = link.match(page_regex)
        let current_page = 0
        let next_page_link = "";
        if(page_match != null) {
            current_page = Number(page_match[1]);
            next_page_link = link.replace(page_match[0], page_match[0][0]/* ? or & */ + "page=" + String(current_page + 1))
        } else {
            next_page_link = (link.indexOf("?") == -1 ? "?" : "&") + "page=" + String(current_page + 1)
        }
        console.log("current_page", current_page);
        console.log("next_page_link", next_page_link);

        // try to load torrent table of next page and merge it to the current page
        let f = $("<iframe src='" + next_page_link + "#subpage'/ hidden>");
        $("body").after(f);
        // listen for subpage_ready message.
        window.addEventListener("message", function(event) {
            if(event.data == "subpage_ready") {
                let childDocument = f[0].contentDocument;
                let cts = parse_torrent_table(childDocument);
                modify_torrent_table(cts, childDocument)
                torrents.push(...cts);
                sort_torrent_table(torrents);
                rebuild_torrent_table(torrents, document);
            }
        });
    } else { // subpage
        parent.postMessage("subpage_ready", "*");
    }
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址