哔哩哔哩站内链接信息显示 BilibiliLinksInfos

替换bilibili页面的视频链接为视频名,专栏链接为专栏文章名

As of 2020-03-24. See the latest version.

// ==UserScript==
// @name         哔哩哔哩站内链接信息显示 BilibiliLinksInfos
// @namespace    https://www.ckylin.site/
// @version      0.6
// @description  替换bilibili页面的视频链接为视频名,专栏链接为专栏文章名
// @author       CKylinMC
// @include      *t.bilibili.com/*
// @include      *www.bilibili.com/video*
// @include      *www.bilibili.com/read*
// @license      GPLv3 License
// @grant        none
// ==/UserScript==

(function() {
function CKBilibiliLinksUtil(dom) {
    const that = this;
    this.dom = dom;
    this.doGetInfos = function () {
        if (this.dom instanceof HTMLElement) {
            if ((this.dom.href.indexOf("https://www.bilibili.com/video/") == 0 || this.dom.href.indexOf("//www.bilibili.com/video/") == 0) && !this.dom.hasAttribute("data-blblinfo")) {
                this.dom.setAttribute("data-blblinfo", true);
                this.dom.innerHTML += " (正在获取信息...)";
                setTimeout(() => {
                    fetch(this.dom.href).then(res => res.text(), failReason => {
                        this.dom.innerHTML.replace(" (正在获取信息...)", "(信息获取失败)");
                    })
                        .then(res => {
                            if (res.indexOf("视频去哪了呢?_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili") >= 0) {
                                that.dom.innerHTML = that.dom.innerHTML.replace(" (正在获取信息...)", "(视频不存在或已删除)");
                                return;
                            }
                            var parta = res.split("\"title\":\"");
                            if (parta.length >= 2) {
                                var partb = parta[1].split("\",\"pubdate");
                                that.dom.innerHTML = "[视频] " + unescape(partb[0].replace(/\\/g,"%"));
                            }
                        });
                }, 100);
            }
            else if ((this.dom.href.indexOf("https://www.bilibili.com/read/") == 0 || this.dom.href.indexOf("//www.bilibili.com/read/") == 0) && !this.dom.hasAttribute("data-blblinfo")) {
                this.dom.setAttribute("data-blblinfo", true);
                this.dom.innerHTML += " (正在获取信息...)";
                setTimeout(() => {
                    fetch(this.dom.href).then(res => res.text(), failReason => {
                        this.dom.innerHTML.replace(" (正在获取信息...)", "(信息获取失败)");
                    })
                        .then(res => {
                            if (res.indexOf("<div class=\"error-txt\">页面不存在或已被删除</div>") >= 0) {
                                that.dom.innerHTML = that.dom.innerHTML.replace(" (正在获取信息...)", "(专栏不存在或已删除)");
                                return;
                            }
                            var parta = res.split("<meta name=\"name\" content=\"");
                            if (parta.length >= 2) {
                                var partb = parta[1].split("\">");
                                that.dom.innerHTML = "[专栏] " + unescape(partb[0].replace(/\\/g,"%"));
                            }
                        });
                }, 100);
            }
            /*else if ((this.dom.href.indexOf("https://live.bilibili.com/") == 0 || this.dom.href.indexOf("//live.bilibili.com/") == 0) && !this.dom.hasAttribute("data-blblinfo")) {
                console.log(this.dom);
                this.dom.setAttribute("data-blblinfo", true);
                this.dom.innerHTML += " (正在获取信息...)";
                setTimeout(() => {
                    fetch(this.dom.href).then(res => res.text())
                        .then(res => {
                            var parta = res.split("title id=\"link-app-title\">");
                            if (parta.length >= 2) {
                                var partb = parta[1].split(" - 哔哩哔哩直播,二次元弹幕直播平台");
                                that.dom.innerHTML = "[直播间] " + partb[0];
                            }
                        });
                }, 100);
            }*/
        }
    }
}

function doUpdateAllLinksHook() {
    document.querySelectorAll("a").forEach((e, i) => {
        e.onmouseover = e => {
            (new CKBilibiliLinksUtil(e.target)).doGetInfos();
        }
    });
}
document.addEventListener("DOMSubtreeModified", function (e) {
    doUpdateAllLinksHook();
});
doUpdateAllLinksHook();
})();