Bilibili 账号已注销修正

修正Bilibili 账户已注销的主页链接,修改为 https://www.bilibili.com/list/$UID

// ==UserScript==
// @name         Bilibili 账号已注销修正
// @name:zh-CN   Bilibili 账号已注销修正
// @namespace    http://tampermonkey.net/
// @version      2.3.1
// @license      MIT
// @description  修正Bilibili 账户已注销的主页链接,修改为 https://www.bilibili.com/list/$UID
// @description:zh-CN  修正Bilibili 账户已注销的主页链接,修改为 https://www.bilibili.com/list/$UID
// @author       Kaesinol
// @match        https://*.bilibili.com/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

// https://github.com/the1812/Bilibili-Evolved/discussions/4804#discussioncomment-10513931
(function () {
    "use strict";
    function processLinks() {
        const rules = {
            "space.bilibili.com/\\d+/favlist": {
                query: "div.bili-video-card__subtitle a",
            },
            "bilibili.com/(video|list)/": {
                type: "intercept",
                query: ".up-detail-top a",
            },
            "search.bilibili.com": {
                query: ".bili-video-card__info--owner",
            },
            "www.bilibili.com/opus/\\d+": {
                type: "override",
                query: ".opus-module-author:not([data-processed])",
            },
        };

        // 遍历执行
        Object.entries(rules).forEach(([host, { query, type }]) => {
            if (RegExp(host).test(location.href)) {
                document
                    .querySelectorAll(query)
                    .forEach((el) => handleElement(el, type));
            }
        });
    }
    function base64(n) {
        n = BigInt(n); // 保证是 BigInt
        const chars =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
        let res = "";
        while (n > 0n) {
            res = chars[Number(n % 64n)] + res;
            n /= 64n;
        }
        return res || "A";
    }
    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }
    function handleElement(tag, type = "normal") {
        let text = null;
        let regular = true;
        if (
            ["space.bilibili.com", "search.bilibili.com", "bilibili.com/opus"].some(
                (prefix) => (location.hostname + location.pathname).includes(prefix)
            )
        ) {
            text = tag.textContent.split(" ").filter((s) => s.trim() !== "")[0];
            regular = false;
        } else {
            text = tag.text;
        }
        if (text.trim() === "账号已注销") {
            const match = type == "override" ? true : tag.href.match(/\/(\d+)\??/);
            tag.style.fontStyle = "italic";
            if (match && type == "override") {
                // 劫持 window.open 获取 UID
                let uid = null;
                const _open = window.open;
                window.open = (url, t) =>
                    (uid = url.match(/space\.bilibili\.com\/(\d+)/)[1]);
                tag.querySelector(".opus-module-author__avatar").click();
                sleep(200);
                window.open = _open;
                tag.addEventListener(
                    "click",
                    (e) => {
                        e.preventDefault();
                        e.stopPropagation();
                        e.stopImmediatePropagation();
                        console.log(uid);
                        if (uid) _open(`https://www.bilibili.com/list/${uid}`, "_blank");
                    },
                    { capture: true }
                );
                tag.querySelector(".opus-module-author__name").textContent =
                    "账号已注销" + base64(uid);
                tag.setAttribute("data-processed", "true");
                return;
            }
            if (regular) tag.text += base64(match[1]);
            else
                tag.text = tag.text.replace(
                    "账号已注销",
                    "账号已注销" + base64(match[1])
                );
            if (match && type == "normal") {
                tag.href = `https://www.bilibili.com/list/${match[1]}`;
            } else if (match && type == "intercept") {
                tag.addEventListener("click", function (event) {
                    event.preventDefault(); // 阻止默认跳转行为
                    window.open(`https://www.bilibili.com/list/${match[1]}`, "_blank");
                });
            }
        }
    }
    function processCommentRenderers(elements) {
        elements.forEach((renderer) => {
            const bili = renderer.shadowRoot.querySelector("bili-comment-renderer");
            const userInfo = bili.shadowRoot.querySelector("bili-comment-user-info");
            const user = userInfo.shadowRoot.querySelector("#user-name a");
            if (user) handleElement(user);

            const replies = renderer.shadowRoot.querySelector(
                "bili-comment-replies-renderer"
            );
            if (!replies.shadowRoot.textContent.trim()) {
                renderer.setAttribute("data-processed", "true");
                return;
            }

            const replyNodes = replies.shadowRoot.querySelectorAll(
                "bili-comment-reply-renderer"
            );
            replyNodes.forEach((reply) => {
                const rUser = reply.shadowRoot
                    .querySelector("bili-comment-user-info")
                    .shadowRoot.querySelector("#user-name a");
                if (rUser) handleElement(rUser);
            });
        });
    }
    function processComments() {
        const startElement = document.querySelector("bili-comments");
        if (startElement && startElement.shadowRoot) {
            const allElements = startElement.shadowRoot.querySelectorAll(
                "bili-comment-thread-renderer:not([data-processed])"
            );
            processCommentRenderers(allElements);
        }
    }
    function tick() {
        processComments();
        processLinks();
    }

    setInterval(tick, 3000);
})();