真白萌新站阅读插件

去除字体样式,添加返回顶部按钮等。

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

// ==UserScript==
// @name         真白萌新站阅读插件
// @namespace    mashiro_me
// @version      0.4.8
// @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 = 4;
    //纯文本模式Flag(改为true使用纯文本模式)
    var textModFlag = false;

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

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

    //允许点击任意区域隐藏目录
    LetAnyClickHideChapter();

    if (textModFlag){

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

        //文本内容复制
        FormartNodesAsText(textBox, baseBox);

    }else{

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

        //删除空行
        DeleteEmptyRows(textBox);

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

    }

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

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

})();

function AddButtonGroup(){

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

    //插件按钮组
    var icon,button;
    var buttonBox = document.createElement("div");
    var styleList =
        "position: fixed; bottom: 50px;" +
        "width: 36px; height: 240px;";

    buttonBox.setAttribute("style", styleList);
    buttonBox.setAttribute("id", "AddIn_ButtonGroup");


    //原内容/编辑后内容切换按钮
    icon = document.createElement("i");
    icon.setAttribute("class", "fa fa-refresh");
    icon.setAttribute("style", "margin-top: 11px;margin-left: 11px;");
    button = document.createElement("div");
    button.appendChild(icon);

    styleList =
        "bottom: 200px; left: 0px; position: absolute; " +
        "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() {

        var contents = document.getElementsByClassName("box-body nvl-content");
        if (contents.length < 2) { return; }

        var toHide, toShow, toHideBlockHeight, originPosition, positionCoeff;
        if (contents[0].style.display == "block"){
            toHide = contents[0];
            toShow = contents[1];
        }else{
            toHide = contents[1];
            toShow = contents[0];
        }

        toHideBlockHeight = toHide.offsetHeight;
        originPosition = document.documentElement.scrollTop;
        positionCoeff = (originPosition - toHide.offsetTop) /
            toHideBlockHeight;

        toHide.style.display = "none";
        toShow.style.display = "block";

        if (positionCoeff > 1 ){
            document.documentElement.scrollTop = originPosition +
                ( toShow.offsetHeight - toHideBlockHeight );
        }
        else if (positionCoeff > 0 ){
            document.documentElement.scrollTop = ( positionCoeff * toShow.offsetHeight ) +
                toShow.offsetTop;
        }

    };
    buttonBox.appendChild(button);


    //目录按钮
    icon = document.createElement("i");
    icon.setAttribute("class", "fa fa-list");
    icon.setAttribute("style", "margin-top: 11px;margin-left: 10px;");
    button = document.createElement("div");
    button.appendChild(icon);

    styleList =
        "bottom: 160px; left: 0px; position: absolute; " +
        "width: 36px; height: 36px;"+
        "border-radius: 3px; border: 1px solid; border-color: #E6E6E6;"+
        "background-color: white;"+
        "cursor: pointer;";

    button.setAttribute("style", styleList);
    button.setAttribute("class", "list-btn chapter-list");
    button.onclick = function () {
        var chapter = document.getElementsByClassName("chapter-nav")[0];
        if (chapter.offsetWidth == 0){
            var baseWidth = document.body.clientWidth;
            var titleBox = chapter.children[1];
            var bookMark = chapter.getElementsByClassName("marked")[0];

            if (baseWidth > 1000) {
                titleBox.style.width = ((baseWidth * 0.3) - 5) + "px";
                chapter.style.width = "30%";
            }else{
                titleBox.style.width = ((baseWidth * 0.6) - 5) + "px";
                chapter.style.width = "60%";
            }
            chapter.scrollTop = bookMark.offsetTop - bookMark.offsetHeight;
            ResetTitleBoxWidth();
        }
    };
    buttonBox.appendChild(button);


    //直达评论区按钮
    icon = document.createElement("i");
    icon.setAttribute("class", "fa fa-comments-o");
    icon.setAttribute("style", "margin-top: 11px;margin-left: 10px;");
    button = document.createElement("div");
    button.appendChild(icon);
    window.AddIn_OP1 = -1;

    styleList =
        "bottom: 120px; left: 0px; position: absolute; " +
        "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 () {
        if (window.AddIn_OP1 < 0) {
            window.AddIn_OP1 = document.documentElement.scrollTop;
            document.documentElement.scrollTop =
                document.getElementsByClassName("col-md-12")[1].offsetTop;
        }else{
            document.documentElement.scrollTop = window.AddIn_OP1;
            window.AddIn_OP1 = -1;
        }
    };
    buttonBox.appendChild(button);


    //返回顶部按钮
    icon = document.createElement("i");
    icon.setAttribute("class", "fa fa-chevron-up");
    icon.setAttribute("style", "margin-top: 10px; margin-left: 10px;");
    button = document.createElement("div");
    button.appendChild(icon);
    window.AddIn_OP2 = -1;

    styleList =
        "bottom: 0px; left: 0px; position: absolute; " +
        "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() {
        if (window.AddIn_OP2 < 0) {
            window.AddIn_OP2 = document.documentElement.scrollTop;
            document.documentElement.scrollTop = 0;
        }else{
            document.documentElement.scrollTop = window.AddIn_OP2;
            window.AddIn_OP2 = -1;
        }
    };
    buttonBox.appendChild(button);


    //追加按钮组,重设位置
    parent.appendChild(buttonBox);
    KeepButtonPosition("AddIn_ButtonGroup");
    window.addEventListener("resize", function(){ KeepButtonPosition("AddIn_ButtonGroup"); });

}

function LetAnyClickHideChapter(){

    //鼠标在目录中时设定为利用中
    var chapter = document.getElementsByClassName("chapter-nav")[0];
    chapter.addEventListener("mouseenter", function(event){
        AddClass(document.getElementsByClassName("chapter-nav")[0], "AddIn_Using");
    });
    chapter.addEventListener("mouseleave", function(event){
        RemoveClass(document.getElementsByClassName("chapter-nav")[0], "AddIn_Using");
    });

    //文档全体添加隐藏目录事件
    document.addEventListener("click", function(event){
        var chapter = document.getElementsByClassName("chapter-nav")[0];
        if ((! /( AddIn_Using|AddIn_Using )/.test(chapter.className)) &&
            (chapter.offsetWidth > 0)){

            var baseWidth = document.body.clientWidth;
            var titleBox = chapter.children[1];
            if (baseWidth > 1000) {
                titleBox.style.width = ((baseWidth * 0.3) - 5) + "px";
            }else{
                titleBox.style.width = ((baseWidth * 0.6) - 5) + "px";
            }

            chapter.style.width = "0px";
            ResetTitleBoxWidth();
        }
    });

}

function ResetTitleBoxWidth(){
    window.AddIn_Reset && clearTimeout(window.AddIn_Reset);
    window.AddIn_Reset =
    setTimeout(function(){
        document.getElementsByClassName("chapter-nav")[0].children[1].style.width = "100%"
        window.AddIn_Reset = null;
    }, 600);
}

function AddClass(item, className){

    var reg = new RegExp("( " + className + "|" + className + " )");
    var allClasses = item.className;
    if (! reg.test(allClasses)){
        item.setAttribute("class", allClasses + " " + className);
    }

}

function RemoveClass(item, className){

    var reg = new RegExp("( " + className + "|" + className + " )");
    var allClasses = item.className;
    if (reg.test(allClasses)){
        item.setAttribute("class", allClasses.replace(reg, ""));
    }

}

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.4){
                button.style.opacity = 0.4;
            }
        }else{
            if(button.style.opacity != 1){
                button.style.opacity = 1;
            }
        }
    }
}

function FormartNodesAsText(base, parent){

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


        if (childs[i].nodeType == 3){

            var innerText = childs[i].data;
            if (/^(&nbsp;|\s)*$/.test(innerText)){
                continue;
            }

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

        }else{

            var tagName = childs[i].localName;

            if (tagName == "br" || tagName == "img" || tagName == "ruby"){

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

            }else{

                if (( tagName == "p" ) &&
                    ( typeof(childs[i].style) != "undefined" ) &&
                    ( typeof(childs[i].style.textIndent) != "undefined" )){
                    base.appendChild(document.createElement("br"));
                }

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

                    FormartNodesAsText(base, childs[i]);

                }
                if (tagName != "span"){
                    base.appendChild(document.createElement("br"));
                }
            }
        }
    }
}

function DeleteFontStyles(parent){

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

        if (typeof(childs[i].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);
        }

    }

}