真白萌新站阅读插件

去掉内联字体样式,删除四个以上换行。

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

// ==UserScript==
// @name         真白萌新站阅读插件
// @namespace    mashiro_me
// @version      0.4.3
// @description  去掉内联字体样式,删除四个以上换行。
// @description  添加返回顶部按钮。
// @author       MikaRyu
// @match        https://masiro.me/admin/novelReading*
// @license      BSD
// @icon         https://www.google.com/s2/favicons?domain=masiro.me
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    //追加返回顶部按钮
    AddResetPositinonButton();

    //最大保留换行数
    var maxBreakLines = 4;
    //纯文本模式Falg(改为true使用纯文本模式)
    var textModFlag = false;

    //小说内容Box
    var textBox;
    var baseBox = document.getElementsByClassName("box-body nvl-content")[0];

    if (textModFlag){

        //复制纯文本用Box
        textBox = baseBox.parentNode.insertBefore(baseBox.cloneNode(false), baseBox);

        //文本内容复制
        window.rowCount = 0;
        FormartNodesAsText(textBox, baseBox);


    }else{

        //复制原内容Box
        textBox = baseBox.parentNode.insertBefore(baseBox.cloneNode(true), baseBox);

        //删除空行
        DeleteEmptyRows(textBox)

        //删除字体、字体大小、字体颜色
        DeleteFontStyles(textBox);

    }

    //删除【maxBreakLines】个以上的换行
    DeleteMultiBrs(textBox, maxBreakLines);

    //原内容Box隐藏
    baseBox.style.display = "none";

})();

function AddResetPositinonButton(){

    var parent = document.getElementById("app");

    var icon = document.createElement("i");
    icon.setAttribute("class", "fa fa-chevron-up");
    icon.setAttribute("style", "margin-top: 10px;margin-left: 10px;");

    var button = document.createElement("div");
    button.appendChild(icon);
    button.setAttribute("id", "AddIn_RpButton");

    var styleList =
        "position: fixed; bottom: 50px;" +
        "width: 36px; height: 36px;"+
        "border-radius: 3px; border: 1px solid; border-color: #E6E6E6;"+
        "background-color: white;"+
        "cursor: pointer;";

    button.setAttribute("style", styleList);
    button.onclick = function() { document.documentElement.scrollTop = 0; };

    parent.appendChild(button);
    KeepButtonPosition("AddIn_RpButton");

    window.addEventListener("resize", function(){ KeepButtonPosition("AddIn_RpButton"); });

}

function KeepButtonPosition(buttonId){

    var button = document.getElementById(buttonId);
    var marginWidth = document.getElementsByClassName("content")[0].offsetLeft -
        document.getElementById("app").offsetLeft;

    if ( marginWidth > 136 ){
        button.style.right = (marginWidth - 86) + "px";
        if(button.style.opacity != 1){
            button.style.opacity = 1;
        }
    }else{
        if (button.style.right != "50px"){
            button.style.right = "50px";
        }
        if (marginWidth < 71){
            if(button.style.opacity != 0.5){
                button.style.opacity = 0.5;
            }
        }else{
            if(button.style.opacity != 1){
                button.style.opacity = 1;
            }
        }
    }
}

function FormartNodesAsText(base, parent){

    var childs = parent.childNodes;
    var orgLth = childs.length;
    var tagName, innerText, br;

    for(var i = 0; i < orgLth; i++){

        tagName = childs[i].localName;

        if (typeof(tagName) == "undefined"){

            innerText = childs[i].data;

            if (/^(&nbsp;|\s)*$/.test(innerText)){
                continue;
            }

            var regx = /^(&nbsp;|\s)*([「『【[\[]{1}).*([」』】]\]]){1}(&nbsp;|\s)*$/;

            if (regx.test(innerText)) {

                if (((window.rowCount > 1) && (base.childNodes[window.rowCount - 2].localName != "br")) ||
                    ((window.rowCount == 1) && (base.childNodes[0].localName != "br")) ||
                    (window.rowCount == 0)){
                    base.appendChild(document.createElement("br"));
                    window.rowCount += 1;
                }

                base.appendChild(childs[i].cloneNode(false));
                window.rowCount += 1;

                if (((i + 1) == orgLth) || ( childs[i+1].localName != "br" )){
                    base.appendChild(document.createElement("br"));
                    window.rowCount += 1;

                }

            }else{

                base.appendChild(childs[i].cloneNode(false));
                window.rowCount += 1;

            }

            if ((i + 1) < orgLth){
                if( childs[i+1].localName != "br" ){
                    base.appendChild(document.createElement("br"));
                    window.rowCount += 1;
                }
            }else{
                base.appendChild(document.createElement("br"));
                window.rowCount += 1;
            }

        }else if (tagName == "br" || tagName == "img"){

            base.appendChild(childs[i].cloneNode(false));
            window.rowCount += 1;

        }else if(tagName == "ruby"){

            base.replaceChild(childs[i].cloneNode(true),
                              base.childNodes[window.rowCount - 1]);

        }else{

            if (childs[i].hasChildNodes()){

                FormartNodesAsText(base, childs[i]);

            }

        }
    }

}

function DeleteFontStyles(parent){

    var childs = parent.children;
    var style;

    for(var i = 0; i < childs.length; i++){

        style = childs[i].style;

        if (typeof(style) != "undefined"){
            childs[i].style.fontFamily = null;
            childs[i].style.fontSize = null;
            childs[i].style.color = null;
        }

        if (childs[i].hasChildNodes()){
            DeleteFontStyles(childs[i]);
        }

    }

}

function DeleteEmptyRows(parent){

    var childs = parent.childNodes;
    var tagName, innerText;

    for(var i = 0; i < childs.length; i++){

        tagName = childs[i].localName;

        if (tagName == "br" ||
            tagName == "img"){
            continue;
        }

        innerText = childs[i].innerHTML;
        if (typeof(innerText) == "undefined"){
            innerText = childs[i].data;
        }

        if ((/^(&nbsp;|\s)*$/g.test(innerText)) ||
            ((! childs[i].hasChildNodes()) && typeof(tagName) != "undefined")){

            parent.removeChild(childs[i]);
            i -= 1;
            continue;

        }

        DeleteEmptyRows(childs[i]);

    }
}

function DeleteMultiBrs(parent, num){

    var j = 0;
    var childs = parent.childNodes;
    var tagName;

    for(var i = 0; i < childs.length; i++){

        tagName = childs[i].localName;

        if (tagName == "br"){

            if( j == num ){
                parent.removeChild(childs[i]);
                i -= 1;
            }else{
                j += 1;
            }
            continue;
        }

        j = 0;
        if (childs[i].hasChildNodes()){
            DeleteMultiBrs(childs[i], num);
        }

    }

}