B站动态、旧版播放页显示评论图片

B站动态、旧版播放页显示评论图片。

目前为 2023-03-23 提交的版本。查看 最新版本

// ==UserScript==
// @name         B站动态、旧版播放页显示评论图片
// @namespace    http://tampermonkey.net/
// @version      1.00
// @description  B站动态、旧版播放页显示评论图片。
// @author       CZX Fuckerman
// @match        *://*.bilibili.com/*
// @exclude      *://member.bilibili.com*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @run-at       document-body
// @license      GPL
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    Function.prototype.clone = function() {
    var cloneObj = this;
    if(this.__isClone) {
      cloneObj = this.__clonedFrom;
    }

    var temp = function() { return cloneObj.apply(this, arguments); };
    for(var key in this) {
        temp[key] = this[key];
    }

    temp.__isClone = true;
    temp.__clonedFrom = cloneObj;

    return temp;
};

    hackEle(document.querySelector("head"), "insertBefore", hack)
    hackEle(document.querySelector("head"), "appendChild", hack)


    function hackEle(ele, func, callback){
        const ori = ele[func];
        ele[func] = function(...args){
            //console.log(this, ...args)
            return callback(ori.bind(this), ...args)
        }
    };

    function injectbbComment(){
       const bbComment = window.bbComment;
        var oldFun = bbComment.prototype._createListCon.clone();
        bbComment.prototype._createListCon = function (item, i, pos) {
            var res = oldFun.apply(this, [item, i, pos]);
            if(typeof(item.content.pictures) != "undefined" || item.content.pictures != null){
                var domRes = $.parseHTML(res);
                $.each(item.content.pictures, function(index, item) {
                    $(domRes).children("p.text").append("<br/><img class=\"custom-comment-picture\" src=\"" + item.img_src + "\" onclick=\"window.open('" + item.img_src + "')\"></img>");
                });
                res = $(domRes).prop('outerHTML');
            }
            return res;
        };

    };

    let styleDom = document.createElement("style");
    styleDom.innerText = ".custom-comment-picture{width:auto!important;height:auto!important;max-width:100%!important;max-height:100%!important;cursor:pointer!important}";
    document.head.appendChild(styleDom);

    function hack(origin, ...args){
        const [ele, target] = [...args];
        if( ele.src && ~ele.src.indexOf("/x/v2/reply")){
            // 确定是评论类型,执行额外流程
            injectbbComment();
        }
        let res = origin(...args);
        return res;
    }

})();