nicoseiga res checker

add a simple responce function to niconico seiga's comment area.

目前为 2018-09-08 提交的版本。查看 最新版本

// ==UserScript==
// @name            nicoseiga res checker
// @namespace       nicoseigareschecker
// @description     add a simple responce function to niconico seiga's comment area.
// @author          sotoba
// @match           http://seiga.nicovideo.jp/seiga/im*
// @version         1.0.0.20180908
// @license         MIT License
// @grant           none
// ==/UserScript==
//---------------------------------------------------------------------------------------

(function () {
    'use strict';

    class commentList {
        constructor(list) {
            this.list = list;
        }

        setCommentList(list) {
            this.list = list;
        }

        getCommentList() {
            return this.mylist;
        }
    }

    const resType = {
        // type:"↑"
        arrow: Symbol("arrow"),
        // type:">" and ">"
        anchor: Symbol("anchor")
    }

    class shortCommentList extends commentList {
        getRefList() {
            let refList = []
            const innerElement = this.list.getElementsByClassName("comment_info");
            const firstElementID = innerElement[0].getElementsByClassName("id")[0].children[0].innerHTML


            let i = 0
            for (const elem of innerElement) {
                const comment = elem.getElementsByClassName("text")[0].innerHTML
                const refData = this.checkText(comment)
                if (refData !== null) {
                    const idString = elem.getElementsByClassName("id")[0].children[0].innerHTML
                    refData.element = elem
                    refData.id = idString
                    if (refData.type == resType.arrow && i - Number(refData.target) < 0) {
                        refData.refHistory = true
                    } else if (refData.type == resType.anchor && Number(refData.target)< Number(firstElementID) ) {
                        refData.refHistory = true
                    } else {
                        refData.refHistory = false
                    }

                    refList.push(refData)

                    const resString = comment.substring(0, refData.length)
                    const rest = comment.substring(refData.length)

                    const typeString = refData.type === resType.arrow ? "arrow" : "anchor"
                    const refHistoryString = refData.refHistory ? "true" : "false"
                    elem.getElementsByClassName("text")[0].innerHTML = "<span class=res-check type=" + typeString + " target=" + refData.target + " refHistory=" + refHistoryString + ">" + resString + "</span>" + rest
                }
                i++;
            }

            return refList
        }

        checkText(str) {
            if (str.startsWith("↑")) {
                // pattern:↑↑↑
                const regResult = str.match(/^(↑+)([  ×]*)([0-90-9]*).+/)
                if (regResult[1].length > 0 && regResult[3].length > 0) {
                    // pattern :↑5,↑3
                    const target = regResult[3].replace(/[0-9]/g, s => String.fromCharCode(s.charCodeAt(0) - 0xFEE0))

                    const result = {
                        type: resType.arrow,
                        target: target,
                        length: regResult[1].length + regResult[2].length + regResult[3].length
                    }
                    return result
                } else if (regResult[1].length > 0) {
                    const result = {
                        type: resType.arrow,
                        target: regResult[1].length,
                        length: regResult[1].length
                    }
                    return result
                }
            } else if (str.startsWith("&gt;") || str.startsWith(">")) {
                // pattern: >12345,>> 12345、>12345...
                const regResult = str.match(/^(&gt;)+([  ]*)([0-90-9]*).+/)
                const regResult2 = str.match(/^(>)+([  ]*)([0-90-9]*).+/)
                if (regResult[1].length > 0) {
                    const target = regResult[3].replace(/[0-9]/g, s => String.fromCharCode(s.charCodeAt(0) - 0xFEE0))
                    const result = {
                        type: resType.anchor,
                        target: target,
                        length: regResult[1].length + regResult[2].length + regResult[3].length
                    }
                    return result
                }else if(regResult2[1].length > 0) {
                    const target = regResult2[3].replace(/[0-9]/g, s => String.fromCharCode(s.charCodeAt(0) - 0xFEE0))
                    const result = {
                        type: resType.anchor,
                        target: target,
                        length: regResult2[1].length + regResult2[2].length + regResult2[3].length
                    }
                    return result
                }
            }
            return null;
        }

        isReferHistory(refList) {
            for (const ref of refList) {
                if (ref.refHistory) {
                    return true;
                }
            }
            return false;
        }
    }

    class allCommentList extends commentList {

    }

    class Page {
        constructor() {
            this.URL = document.URL
            this.illustID = document.getElementsByClassName("illust_tag_container")[0].getAttribute("data-target_id");
        }
    }

    class Utils {
        getShortCommentList() {
            const listElement = document.getElementById("comment_list");
            return new shortCommentList(listElement)
        }

        colorizeCommentList(shortCommentList) {
            const resList = document.getElementsByClassName("res-check")
            let colorList = ["#F5B090", "#FCD7A1", "#FFF9B1", "#A5D4AD", "#A2D7D4"]
            for (const res of resList) {
                const color = colorList.shift()
                res.setAttribute("style", "background-color:" + color + ";font-size:bold;");
                if (res.getAttribute("refHistory") === "false") {
                    const commentItemNode = res.parentNode.parentNode.parentNode;
                    const num = Number(res.getAttribute("target"))
                    if (res.getAttribute("type") === "arrow") {
                        let previousNode;
                        previousNode = commentItemNode.previousElementSibling
                        for (let i = 0; i < num - 1; i++) {
                            previousNode = previousNode.previousElementSibling
                        }

                        previousNode.getElementsByClassName("text")[0].setAttribute("style", "background-color:" + color + ";");
                    } else if (res.getAttribute("type") === "anchor") {
                        //for (const comment of shortCommentList) {
                       // const innerElement = this.list.getElementsByClassName("comment_info");
                        //const firstElementID = innerElement[0].getElementsByClassName("id")[0].children[0].innerHTML

                        //const innerlement =shortCommentList.getElementsByClassName("id")
                        const innerElement =document.getElementById("comment_list").getElementsByClassName("id")
                        for (const elem of innerElement) {
                            const id = elem.children[0].innerHTML

                            if(res.getAttribute("target")===id){
                                const commentItemNode = elem.parentNode.getElementsByClassName("text")[0]
                                commentItemNode.setAttribute("style", "background-color:" + color + ";");

                            }
                        }
                    }
                }
            }
        }

        getAllCommentList() {

        }

        setPopup() {

        }

    }

    window.onload = () => {
        const utils = new Utils();
        const page = new Page();
        const shortCommentList = utils.getShortCommentList();

        const refList = shortCommentList.getRefList()

        if (refList.size !== 0) {

            // if (shortCommentList.isReferHistory(refList)) {
            //  const allCommentList = utils.getAllCommentList(page);
            // utils.setPopup(page,shortCommentList,allCommentList);
            //  }else{
            utils.colorizeCommentList(shortCommentList);
            //  }
        }


    }
})(); //function

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址