b站回溯首页推荐换一换

b站推荐换一换添加撤回按钮

// ==UserScript==
// @name         b站回溯首页推荐换一换
// @namespace    qwq0
// @version      0.3
// @description  b站推荐换一换添加撤回按钮
// @author       qwq0
// @match        https://www.bilibili.com/
// @match        https://www.bilibili.com/?*
// @run-at       document-start
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        none
// ==/UserScript==

(async function ()
{
    'use strict';

    if (location.pathname != "/")
        return;

    let history = [];
    let nowIndex = -1;

    let clickdButton = false;
    let clickdBacktrackButton = false;

    let oldFetch = window.fetch.bind(window);
    window.fetch = async (...param) =>
    {
        // console.log("test");
        if (
            (clickdButton || clickdBacktrackButton) &&
            typeof (param[0]) == "string" &&
            (
                param[0].startsWith("https://api.bilibili.com/x/web-interface/wbi/index/top/feed/rcmd") ||
                param[0].startsWith("https://api.bilibili.com/x/web-interface/index/top/feed/rcmd") ||
                param[0].startsWith("//api.bilibili.com/x/web-interface/wbi/index/top/feed/rcmd") ||
                param[0].startsWith("//api.bilibili.com/x/web-interface/index/top/feed/rcmd")
            )
        )
        {
            if (clickdButton)
            {
                clickdButton = false;

                if (nowIndex < history.length - 1)
                {
                    nowIndex++;
                    if (nowIndex >= 1)
                        backtrackButton.style.display = "block";
                    return history[nowIndex].clone();
                }
                else
                {
                    /**
                     * @type {Response}
                     */
                    let response = await oldFetch(...param);
                    history.push(response.clone());
                    nowIndex++;
                    if (nowIndex >= 1)
                        backtrackButton.style.display = "block";
                    // console.log(response.clone());
                    return response;
                }
            }
            else
            {
                clickdBacktrackButton = false;

                nowIndex--;
                if (nowIndex < 0)
                    nowIndex = 0;

                if (nowIndex == 0)
                    backtrackButton.style.display = "none";

                return history[nowIndex].clone();
            }
        }
        return oldFetch(...param);
    };

    function parseHtmlString(htmlString)
    {
        let wrapper = document.createElement("div");
        wrapper.innerHTML = htmlString;
        return wrapper.firstChild;
    }

    let buttonRoll = await new Promise(resolve =>
    {
        let intervalId = setInterval(() =>
        {
            let buttonRoll = document.querySelectorAll(".feed-roll-btn")[0];
            if (buttonRoll != undefined)
            {
                resolve(buttonRoll);
                clearInterval(intervalId);
            }
        }, 150);
    });

    /*
    let OldXMLHttpRequest = window.XMLHttpRequest;

    window.XMLHttpRequest = function (...param)
    {
        console.log("new XMLHttpRequest", param);
        let target = new OldXMLHttpRequest(...param);
        let send = target.send.bind(target);
        let open = target.open.bind(target);
        target.send = (...param) =>
        {
            console.log("XMLHttpRequest send", param);
            send(...param);
        };
        target.open = (...param) =>
        {
            console.log("XMLHttpRequest open", param);
            open(...param);
        };
        return target;
    };
    */

    let refreshButton = buttonRoll.children[0];
    let backtrackButton = parseHtmlString(`<button style="margin-top: 10px;" class="primary-btn roll-btn"><span>回溯</span></button>`);
    buttonRoll.appendChild(backtrackButton);
    backtrackButton.style.display = "none";

    refreshButton.addEventListener("click", () =>
    {
        if (!clickdBacktrackButton)
            clickdButton = true;
    }, true);
    backtrackButton.addEventListener("click", () =>
    {
        clickdBacktrackButton = true;
        refreshButton.click();
    });
})();