MJZ-BT之家小助手

替换下载地址为真实地址,可以自动滚动至最新BT处,可以自动隐藏介绍,可以隐藏置顶帖子 。

目前為 2022-11-07 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         MJZ-BT之家小助手
// @namespace    https://github.com/maijz128
// @version      0.6
// @description  替换下载地址为真实地址,可以自动滚动至最新BT处,可以自动隐藏介绍,可以隐藏置顶帖子 。
// @author       MaiJZ
// @match        *://*.btbtt.co/*
// @match        *://*.btbtt.me/*
// @match        *://*.btbtt.net/*
// @match        *://*.btbtt.pw/*
// @match        *://*.btbtt.la/*
// @match        *://*.btbtt.org/*
// @match        *://*.btbtt15.com/*
// @match        *://*.btbtt11.com/*
// @match        *://*.btbtt12.com/*
// @match        *://*.btbtt13.com/*
// @require      https://greasyfork.org/scripts/39499-go-to-top/code/Go%20to%20Top.js
// @grant        none
// ==/UserScript==

const strContainerID = "btbtt-helper";
var css = "";


(function () {
    'use strict';

    // BT下载页面
    if (matchURL("/attach-dialog")) {
        autoClickDownload();
    }
    // 帖子页面
    else if (matchURL("/thread-index-fid")) {
        ThreadPage();
    }
    // 帖子浏览页面
    else if (matchURL("/forum-index-fid")) {
        ToggleTopThread();
    }
    // 搜索页面
    else if (matchURL("/search-index-keyword")) {
        css += "width: 1024px !important;";
    }

    HighlightUserName();

    css += "#threadtype a {font-family: 微软雅黑; text-shadow: 0 0 1.15px #fcf6ecd9, 0 0 1px #7b7b7b, 0 0 0.75px #65625e57;}";
    css += "#wrapper2 > div.width.border{display: none !important;}";

    addStyle(css);
})();


// BT下载页面 ////////////////////////////////////////////////////////////

function autoClickDownload() {
    const AUTO_CLOSE_TIME = 800;

    var inter = setInterval(function () {
        var elA = document.querySelector("#body > div > dl dd a");
        if (elA) {
            console.log(elA);
            clearInterval(inter);

            window.location.href = elA.getAttribute("href");

            setTimeout(function () {
                window.close();
            }, AUTO_CLOSE_TIME);
        }
    }, 50);
}

// 帖子页面 ////////////////////////////////////////////////////////////////

function ThreadPage() {
    addContainer();
    hideIntroduction();
    scrollToBT();
    replaceLink();

    // 添加容器
    function addContainer() {
        var strContainer = '<div id="' + strContainerID + '" style="font-size: medium;"></div> ';
        $('.post_td:first').prepend(strContainer);

        var style = "#" + strContainerID + " span {";
        style += "margin-right: 10px;";
        style += "}";
        addStyle(style);
    }

    // start 隐藏介绍
    function hideIntroduction() {
        var elContainer = $('#' + strContainerID);

        // 添加toggle按钮
        {
            var fun_toggleIntroduction = '<script>function toggleIntroduction(){';
            fun_toggleIntroduction += '$(".post_td .post:first").toggle();';
            fun_toggleIntroduction += '}</script>';

            $('head:first').append(fun_toggleIntroduction);

            var strButton = '<button onclick="toggleIntroduction()" ' +
                'style="width:100%;height:30px;">隐藏/显示介绍</button>';

            elContainer.prepend(strButton);
        }

        // 添加选项按钮:是否自动隐藏介绍
        {
            var fun_toggleAutoHideIntroduction = '<script>function toggleAutoHideIntroduction(){';
            fun_toggleAutoHideIntroduction += 'localStorage.isHideIntroduction = $("#cb-HideIntroduction").attr("checked");';
            fun_toggleAutoHideIntroduction += '}</script>';
            $('head:first').append(fun_toggleAutoHideIntroduction);

            var strCheckbox_HideIntroduction = '<span><input type="checkbox" id="cb-HideIntroduction"';
            strCheckbox_HideIntroduction += ' onchange="toggleAutoHideIntroduction()">';
            strCheckbox_HideIntroduction += '自动隐藏介绍<span>';
            elContainer.append(strCheckbox_HideIntroduction);

        }



        var isHideIntroduction = localStorage.isHideIntroduction || "false";
        if (isHideIntroduction === "true") {
            // 隐藏介绍
            $('.post_td:first .post').hide();

            $("#cb-HideIntroduction").attr("checked", true);
        }

    }

    // 滚动至最新的bt
    function scrollToBT() {
        var elContainer = $('#' + strContainerID);

        // 添加选项按钮:是否自动滚动
        {
            var fun_toggleAutoScrollToBT = '<script>function toggleAutoScrollToBT(){';
            fun_toggleAutoScrollToBT += 'localStorage.isScrollToBT = $("#cb-ScrollToBT").attr("checked");';
            fun_toggleAutoScrollToBT += '}</script>';
            $('head:first').append(fun_toggleAutoScrollToBT);

            var strCheckbox_HideIntroduction = '<span><input type="checkbox" id="cb-ScrollToBT"';
            strCheckbox_HideIntroduction += ' onchange="toggleAutoScrollToBT()">';
            strCheckbox_HideIntroduction += '自动滚动至最新发布的BT<span>';
            elContainer.append(strCheckbox_HideIntroduction);

        }

        var isScrollToBT = localStorage.isScrollToBT || "false";
        if (isScrollToBT === "true") {
            $("#cb-ScrollToBT").attr("checked", true);

            $('html, body').animate({
                scrollTop: $(".post td a:last").offset().top - 100
            }, 1000);
        }

    }

    // 替换下载链接:下载网页->直接地址
    function replaceLink() {
        // 替换前:attach-dialog-fid-950-aid-4323928.htm
        // 替换后:attach-download-fid-950-aid-4323928.htm


        const QUERY = "#body table td.post_td div.post div.attachlist";
        var el_attachlist_list = document.querySelectorAll(QUERY);
        console.assert(el_attachlist_list.length > 0, "没找到BT列表");

        el_attachlist_list.forEach(function (el_attachlist) {

            var elA_list = el_attachlist.querySelectorAll("a");
            if (elA_list) {
                for (var i = 0; i < elA_list.length; i++) {
                    const itemA = elA_list[i];
                    const href = itemA.getAttribute("href");

                    const str1 = "attach-dialog-fid";
                    const str2 = "attach-download-fid";

                    const newHref = href.replace(str1, str2);
                    itemA.setAttribute("href", newHref);
                }
            }
        });
    }

    

    $(".post .attachlist").each(function(){
        // var btnDownloadAllBT = '<a href="#" onclick="downloadAllBT(this)">下载所有BT</a>';
        var btnDownloadAllBT = document.createElement("button");
        btnDownloadAllBT.textContent = "下载所有BT";
        btnDownloadAllBT.onclick = function(){
            // console.log(this);
            downloadAllBT(this);
        };
        
        $(this).append(btnDownloadAllBT);
    });

    

}
var downloadAllBT = function(e){
    var attachlist = $(e).parent();
    var alist = attachlist.find("a");
    alist.each(function(){
        var epHref = $(this).attr("href");
        var epName = $(this).text();
        console.log(epName);
        console.log(epHref);
        // $(this).click();
        // var url = window.location.host + "/" + epHref;
        var url =  epHref;
        setTimeout(function () {
            window.open(url);
        }, 500);
    });
};
window.downloadAllBT= downloadAllBT;
document.downloadAllBT = downloadAllBT;



// 帖子浏览页面:隐藏置顶帖子 ////////////////////////////////////////////////////////

function ToggleTopThread() {
    const TITLE_SELECTOR = "#body div.header td.subject";

    addToggleTopThreadButton();
    addToggleTopThreadContainerFunction();
    addCheckbox();


    function addToggleTopThreadButton() {
        var header_td = document.querySelector(TITLE_SELECTOR);
        if (header_td) {
            var strButton = '<button onclick="toggleTopThreadContainer()" ' +
                'style="font-size: x-small; font-weight:bold;">显示/隐藏 置顶帖子</button>';

            $(header_td).append(strButton);
        }
    }

    function addToggleTopThreadContainerFunction() {
        var toggleTopThreadFun = '<script> function toggleTopThreadContainer() {';
        toggleTopThreadFun += '    $("#threadlist > .bg2").prevAll().toggle();';
        toggleTopThreadFun += '} </script>';
        $('head:first').prepend(toggleTopThreadFun);
    }

    // 添加选项按钮:是否自动隐藏置顶帖子
    function addCheckbox() {
        var fun_toggleAutoHideTopThread = '<script>function toggleAutoHideTopThread(){';
        fun_toggleAutoHideTopThread += 'localStorage.isHideTopThread = $("#cb-HideTopThread").attr("checked");';
        fun_toggleAutoHideTopThread += '}</script>';
        $('head:first').append(fun_toggleAutoHideTopThread);

        var strCheckbox_HideTopThread = '<span style="margin-left: 5px; font-weight:bold;">';
        strCheckbox_HideTopThread += '<input type="checkbox" id="cb-HideTopThread"';
        strCheckbox_HideTopThread += ' onchange="toggleAutoHideTopThread()">';
        strCheckbox_HideTopThread += '自动隐藏置顶帖子<span>';

        $(TITLE_SELECTOR).append(strCheckbox_HideTopThread);

    }


    var isHideTopThread = localStorage.isHideTopThread || "false";
    if (isHideTopThread === "true") {
        // 隐藏置顶帖子
        $("#threadlist > .bg2").prevAll().hide();

        $("#cb-HideTopThread").attr("checked", true);
    }
}

function HighlightUserName(){
    var SuperModerators = ["david19710102", "Eddie wang"];
    $("td.username a").each(function(){
        var name = $(this).text();
        if (SuperModerators.includes(name)) {
            $(this).parent().removeClass("grey");
            $(this).parent().addClass("purple");
        }
    });
}


// Util //////////////////////////////////////////////////////

function matchURL(url) {
    const URL = window.location.href;
    return URL.indexOf(url) > -1;
}

function addStyle(styleContent) {
    var elStyle = document.createElement("style");
    elStyle.innerHTML = styleContent;
    document.head.appendChild(elStyle);
}