Greasy Fork

Greasy Fork is available in English.

真白萌新站阅读体验优化

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

当前为 2021-09-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         真白萌新站阅读体验优化
// @namespace    mashiro_me
// @version      0.3
// @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';

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

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

    if (textModFlag){

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

        //文本内容复制
        FormartNodesAsText(textBox, baseBox);
        //原内容节点删除
        baseBox.parentNode.removeChild(baseBox);

    }else{

        //删除空行
        DeleteEmptyRows(baseBox)

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

    }

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

})();

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;
            }

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

                base.appendChild(document.createElement("br"));
                base.appendChild(childs[i].cloneNode(false));

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

            }else{

                base.appendChild(childs[i].cloneNode(false));

            }

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

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

            base.appendChild(childs[i].cloneNode(false));


        }else{

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

                FormartNodesAsText(base, childs[i]);

            }

        }
    }

}

function DeleteFontStyles(parent){

    var childs = parent.childNodes;
    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);
        }

    }

}