Greasy Fork

Greasy Fork is available in English.

显示nga头像

在正常模式和lite模式下无视内容长度显示用户头像

当前为 2019-01-31 提交的版本,查看 最新版本

// ==UserScript==
// @name         显示nga头像
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  在正常模式和lite模式下无视内容长度显示用户头像
// @author       wfel
// @match        *://bbs.ngacn.cc/read.php*
// @match        *://bbs.nga.cn/read.php*
// @match        *://nga.178.com/read.php*
// @grant        none
// ==/UserScript==

(function() {
    function createElementFromHTML(htmlString) {
        var div = document.createElement('div');
        div.innerHTML = htmlString;

        // Change this to div.childNodes to support multiple top-level nodes
        return div.firstChild;
    }
    
    function fix(____nextChilds, lite) {
        var ____uindex = 0;
        var ____uinfo = {};
        var ____udata = {};
        var avatarElem;
        // 是否显示图像由cLen(uinfo.cLength)和udata.vsmall共同控制
        // cLength由contentC.innerHTML.length控制(页面未渲染前的字符数量)或者由后端计算(一般都由后端计算)
        // cLength在js_read.js的320行被重写,长度变为渲染后contentC的直接孩子结点中的文本节点内的文本总长度(算法有误)
        // vsmall:屏幕大小,继承自commonui.postArg.def
        // lite在js_read.js的330行被重写,根据内容长度和postnum决定lite的值(postnum是否>20与是否显示头像似乎无关)
        // bug是cLength的计算方法导致的

        for(var iter = 0; iter < ____nextChilds.length; ++iter)
        {
            if(____nextChilds[iter].previousElementSibling
               && ____nextChilds[iter].previousElementSibling.tagName.toLowerCase() == 'img') {
                console.log(`next of ${iter} is img`);
                continue;
            }
            console.log(`find ${iter}`);
            ____uindex = parseInt(____nextChilds[iter].parentNode.id.toString().slice(10), 10);
            ____udata = commonui.postArg.data[____uindex];
            ____uinfo = commonui.userInfo.users[____udata.pAid];
            console.log('avatar is ' + ____uinfo.avatar);
            var avatarHTML = commonui.posterInfo.avatar(____uindex, lite, ____uinfo.avatar, ____uinfo.buffs, ____udata ? ____udata.atItem :null, ____udata.pAid);
            avatarHTML.trim();
            //console.log('elem is ' + avatarElem);
            if(avatarHTML) {
                avatarElem = createElementFromHTML(avatarHTML);
                avatarElem = ____nextChilds[iter].parentNode.insertBefore(avatarElem, ____nextChilds[iter]);
                if(lite & 1)
                {
                    ____nextChilds[iter].parentNode.insertBefore(createElementFromHTML('&nbsp;'), avatarElem.nextSibling);
                }
            }
        }
    }
    
    var nextChilds = document.querySelectorAll('td.c1 > span.posterinfo > .stat');
    var lite = 2;
    if(nextChilds.length == 0) {
        nextChilds = document.querySelectorAll('td.c2 > span.posterInfoLine > .author');
        lite |= 1;
    }
    fix(nextChilds, lite);
})();