Greasy Fork

Greasy Fork is available in English.

B站视频封面查看

在播放窗口下方添加查看封面的按钮

当前为 2020-11-03 提交的版本,查看 最新版本

// ==UserScript==
// @name         B站视频封面查看
// @namespace    limgmk/bilibili-cover
// @version      0.0.9
// @description  在播放窗口下方添加查看封面的按钮
// @author       Limgmk
// @include     http*://www.bilibili.com/video/av*
// @include     http*://www.bilibili.com/video/BV*
// @run-at      document-end
// ==/UserScript==

(function () {
    addRes();
    waitLoadComplete();
})();

function waitLoadComplete() {
    if (document.readyState == "complete") {
        setTimeout(addNode, 3000);
    } else {
        setTimeout(waitLoadComplete, 100);
    }
}

function addRes() {
    var head = getElementByXpath("//head");
    var iconLink = document.createElement("link");
    iconLink.setAttribute("rel", "stylesheet");
    iconLink.setAttribute("type", "text/css");
    var href = "https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css";
    iconLink.setAttribute("href", href);
    head.insertBefore(iconLink, null);
}

function addNode() {
    var share = getElementByXpath('//span[@title="分享"]');
    var ops = getElementByXpath('//div[@class="ops"]');
    var re = new RegExp("[0-9].*");
    if (re.exec(share.innerText) && ops) {

        var showCover = document.createElement("span");
        showCover.setAttribute("id", "show-cover");
        showCover.setAttribute("style", "color: #505050;");

        var text = `<i class="fa fa-picture-o" style="font-size: 24px; padding-top: 1px"></i>封面`;
        showCover.innerHTML = text;

        showCover.addEventListener('click', function (e) {
            var cover = getElementByXpath('//meta[@itemprop="image" and @data-vue-meta="true"]');
            if (cover) {
                var coverURL = cover.getAttribute("content");
                coverURL = coverURL.replace(/http[s]?\:/, '');
                window.open(coverURL, '_blank');
            } else {
                alert("不好意思, 找不到封面链接 (゚´Д`゚)゚");
            }
        });

        showCover.addEventListener('mouseover', function (e) {
            showCover.setAttribute("style", "color: #00A1D6;");
        });

        showCover.addEventListener('mouseout', function (e) {
            showCover.setAttribute("style", "color: #505050;");
        });

        share.setAttribute("style", "width: 92px;");

        ops.insertBefore(showCover, null);
    } else {
        setTimeout(addNode, 100);
    }
}

function getElementByXpath(xpath) {
    return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}