Greasy Fork

Greasy Fork is available in English.

哔哩哔哩(B站|Bilibili)收藏夹Fix(多网站跳转)

修复 哔哩哔哩(www.bilibili.com) 失效的收藏。

当前为 2024-11-24 提交的版本,查看 最新版本

// ==UserScript==
// @name         哔哩哔哩(B站|Bilibili)收藏夹Fix(多网站跳转)
// @namespace    http://tampermonkey.net/
// @version      1.2.1
// @description  修复 哔哩哔哩(www.bilibili.com) 失效的收藏。
// @author       Mr.Po
// @match        https://space.bilibili.com/*
// @connect      api.bilibili.com
// @grant        GM_xmlhttpRequest
// @grant        GM_openInTab
// ==/UserScript==

/*jshint esversion: 8 */
(function () {
    'use strict';
    const favlistRegex = /https:\/\/space\.bilibili\.com\/\d+\/favlist.*/;
    function handleFavorites() {
        if (!favlistRegex.test(window.location.href)) {
            return;
        }
        const lisAll = document.querySelectorAll("ul.fav-video-list li.small-item");
        lisAll.forEach(function (li) {
            const anchors = li.querySelectorAll("a");
            const title = anchors[0].getAttribute("title");
            if (title) {
                anchors[1].textContent = title;
            }
        });
        const lisDisabled = Array.from(document.querySelectorAll("ul.fav-video-list li.small-item.disabled")).filter(el => !el.classList.contains("handled"));
        const lisNormal = Array.from(document.querySelectorAll("ul.fav-video-list li.small-item")).filter(el => !el.classList.contains("disabled") && !el.classList.contains("handled"));
        if (lisDisabled.length === 0 && lisNormal.length === 0) {
            return;
        }
        lisDisabled.forEach(function (li) {
            li.classList.add("handled");
            li.classList.remove("disabled");
        });
        let flag = false;
        for (let i = 0; i < lisDisabled.length; i++) {
            const anchors = lisDisabled[i].querySelectorAll("a");
            const title = anchors[0].getAttribute("title");
            if (!title) {
                flag = true;
                break;
            }
        }
        if (flag) {
            const fidRegex = window.location.href.match(/fid=(\d+)/);
            let fid;
            if (fidRegex) {
                fid = fidRegex[1];
            } else {
                fid = document.querySelector("div.fav-item.cur").getAttribute("fid");
            }
            const pn = document.querySelector("ul.be-pager li.be-pager-item.be-pager-item-active").textContent;
            GM_xmlhttpRequest({
                method: "GET",
                url: `https://api.bilibili.com/x/v3/fav/resource/list?media_id=${fid}&pn=${pn}&ps=20&keyword=&order=mtime&type=0&tid=0&platform=web`,
                responseType: "json",
                onload: function (response) {
                    const json = response.response;
                    const medias = json.data.medias;
                    lisDisabled.forEach(function (li) {
                        const ul = li.querySelector(".be-dropdown-menu");
                        const lastChild = ul.lastElementChild;
                        if (!lastChild.classList.contains("added")) {
                            lastChild.classList.add("be-dropdown-item-delimiter");
                            const bv = li.getAttribute("data-aid");
                            const mediaF = medias.filter(function (it) {
                                if (it.bvid == bv) {
                                    return it;
                                }
                            });
                            const media = mediaF[0];
                            const anchors = li.querySelectorAll("a");
                            const content = `${media.intro} `;
                            anchors[0].setAttribute("title", content);
                            anchors[1].textContent = content;
                            const liJump = document.createElement("li");
                            liJump.className = "be-dropdown-item added";
                            liJump.textContent = "■■■■jump■■■■";
                            liJump.addEventListener("click", function () {
                                GM_openInTab(`https://www.biliplus.com/video/${bv}`, { active: true, insert: false, setParent: true });
                                GM_openInTab(`https://xbeibeix.com/video/${bv}`, { insert: false, setParent: true });
                                GM_openInTab(`https://www.jijidown.com/video/${bv}`, { insert: false, setParent: true });
                            });
                            ul.appendChild(liJump);
                            const liSpace = document.createElement("li");
                            liSpace.className = "be-dropdown-item added";
                            liSpace.textContent = "space";
                            liSpace.addEventListener("click", function () {
                                GM_openInTab(`https://space.bilibili.com/${media.upper.mid}`, { active: true, insert: true, setParent: true });
                            });
                            ul.appendChild(liSpace);
                        }
                    });
                }
            });
        }
        lisNormal.forEach(function (li) {
            li.classList.add("handled");
            const ul = li.querySelector(".be-dropdown-menu");
            const lastChild = ul.lastElementChild;
            if (!lastChild.classList.contains("added")) {
                lastChild.classList.add("be-dropdown-item-delimiter");
                const bv = li.getAttribute("data-aid");
                const liJump = document.createElement("li");
                liJump.className = "be-dropdown-item added";
                liJump.textContent = "■■■■jump■■■■";
                liJump.addEventListener("click", function () {
                    GM_openInTab(`https://www.biliplus.com/video/${bv}`, { insert: false, setParent: true });
                    GM_openInTab(`https://xbeibeix.com/video/${bv}`, { insert: false, setParent: true });
                    GM_openInTab(`https://www.jijidown.com/video/${bv}`, { insert: false, setParent: true });
                });
                ul.appendChild(liJump);
                const srcCover = li.querySelector("picture img").getAttribute("src");
                const urlCover = srcCover.match(/\/\/([^@]*)@/);
                const liCover = document.createElement("li");
                liCover.className = "be-dropdown-item added";
                liCover.textContent = "cover";
                liCover.addEventListener("click", function () {
                    GM_openInTab(`https://${urlCover[1]}`, { active: true, insert: true, setParent: true });
                });
                ul.appendChild(liCover);
            }
        });
    }
    setInterval(handleFavorites, 1000);
})();