博客阅读美化(去广告|自动全文|简洁|<CSDN|51脚本|SegmentFault>)

去广告;布局及格式优化;优化内容推荐展示样式;图片居中;删除空段落;|CSDN|自动阅读全文;去除首行缩进;支持MathJax;|51脚本|SegmentFault|。(修改自新版CSDN阅读体验提升)

当前为 2018-03-28 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         博客阅读美化(去广告|自动全文|简洁|<CSDN|51脚本|SegmentFault>)
// @namespace    http://www.csdn.net/
// @icon         https://favicon.yandex.net/favicon/csdn.net
// @version      1.2.9-20180328
// @description  去广告;布局及格式优化;优化内容推荐展示样式;图片居中;删除空段落;|CSDN|自动阅读全文;去除首行缩进;支持MathJax;|51脚本|SegmentFault|。(修改自新版CSDN阅读体验提升)
// @author       zhuzhuyule
// @license      GPL version 3
// @match        http://blog.csdn.net/*
// @match        https://blog.csdn.net/*
// @match        http://www.jb51.net/*
// @match        https://www.jb51.net/*
// @match        https://segmentfault.com/*
// @grant        none
// @run-at       document-body
// ==/UserScript==
(function () {
    'use strict';
    //初始化一个 document.ready 事件
    (function () {
        var ie = !!(window.attachEvent && !window.opera);
        var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);
        var fn = [];
        var run = function () {
            for (var i = 0; i < fn.length; i++) fn[i]();
        };
        var d = document;
        d.ready = function (f) {
            if (!ie && !wk && d.addEventListener)
                return d.addEventListener('DOMContentLoaded', f, false);
            if (fn.push(f) > 1) return;
            if (ie)
                (function () {
                    try {
                        d.documentElement.doScroll('left');
                        run();
                    }
                    catch (err) {
                        setTimeout(arguments.callee, 0);
                    }
                })();
            else if (wk)
                var t = setInterval(function () {
                    if (/^(loaded|complete)$/.test(d.readyState)) {
                        clearInterval(t);
                        run();
                    }
                }, 0);
        };
    })();

    //------------------------------------------------------------------------------------------
    // 文档载入成功后开始执行
    try {
        if ('blog.csdn.net' == location.host) {
            dealCSDN();
        } else if ('www.jb51.net' == location.host) {
            dealJb51();
        } else if ('segmentfault.com' == location.host) {
            dealSegmentFault();
        }

        window.addEventListener('scroll', () => {
            if (getScrollTop() + getWindowHeight() > getScrollHeight() - 10) {
                document.querySelectorAll('dl[class=""]').forEach(function (item) {
                    item.parentElement.removeChild(item);
                });

                document.querySelectorAll('iframe').forEach(function (item) {
                    if ((item.src && item.src.indexOf('pos.baidu.com') > 0)) {
                        item.parentElement.removeChild(item);
                    }
                });
            }
        });
    } catch (e) {
        console.log(e);
    }
    //------------------------------------------------------------------------------------------
    //页面处理过程
    /**
     * 处理 CSDN广告及布局
     * blog.csdn.net
     */
    function dealCSDN() {
        var s = '';
        //公共部分
        s += 'h1 { "text-align": "center"; }';
        s += '.article_content p { margin:1em 0; "line-height": "1.75em";"color":"#2F2F2F"}';
        s += '.pulllog-box { display:none !important; }';

        //旧版本
        s += '#homepageArticles ,#layerd,#nav_show_top_stop, .QRcodebg1';
        s += '{ display: none !important; }';
        s += '#com-quick-reply,#com-quick-collect,#com-d-top-a { min-width: 30px !important; width: 31px !important;}';
        s += '#com-quick-reply:hover,#com-quick-collect:hover,#com-d-top-a:hover { width: 90px !important;}';

        //新版本
        s += '.fixRight,.extension_other,.fixRight_box,.persion_article,.yd_a_d_feed_cla';
        s += '{ display: none !important;}';
        s += '.article_content,.article_content p,.article_content p span:not([class^=MathJax] span):not([class^=MathJax]):not([id^=MathJax] span):not([id^=MathJax]) {"font-family": "Microsoft YaHei,Consolas";"font-size": "15px" }';
        s += '.article_content p .MathJax { "font-size": "16px"; }';
        s += '.article_content p img{  "display": "block";"margin": "0 auto" }';

        //修改原来烦人的样式
        s += contentEndStyle('article');
        s += recomendStyle();
        addStyle(style);
        document.ready(function () {
            //自动展开全文
            var btnMore = document.querySelector('.readall_box a.btn.read_more_btn');
            if (btnMore)
                btnMore.click();

            //优化推荐列表显示效果
            modifyRecomendWeak('.recommend_list');

            //删除空白段落
            var paragraph = document.querySelectorAll('.article_content p');
            paragraph.forEach(function (item) {
                if (!item.innerText.trim() && !item.querySelector('img,a'))
                    item.parentElement.removeChild(item);
            });

            document.querySelectorAll('iframe').forEach(function (item) {
                if ((item.src && item.src.indexOf('pos.baidu.com') > 0)) {
                    item.parentElement.removeChild(item);
                }
            });

            setTimeout(function () {
                var hideArr = [];
                document.querySelectorAll('div[id]').forEach(function (item) {
                    if (item.id.startsWith('BAIDU_SSP'))
                        hideArr.push('#' + item.id);
                });

                document.querySelectorAll('dl[class=""]').forEach(function (item) {
                    item.parentElement.removeChild(item);
                });

                document.querySelectorAll('iframe').forEach(function (item) {
                    if ((item.src && item.src.indexOf('pos.baidu.com') > 0)) {
                        if (item.parentElement.id.length > 0)
                            hideArr.push('#' + item.parentElement.id);
                        else
                            item.parentElement.style.display = 'none !important;';
                    }
                });
                if (hideArr.length > 0)
                    hiddenItems(hideArr);
            }, 200);
        });
    }

    /**
     * 处理 脚本之家广告及布局
     * www.jb51.net
     */
    function dealJb51() {
        var hideArr = [];
        hideArr.push('.main.mb10.clearfix');
        hideArr.push('.header .wrap');
        hideArr.push('.main.clearfix.bgf .lbd.clearfix');
        hideArr.push('#content>.jb51ewm');
        hideArr.push('.lbd.clearfix');
        hideArr.push('.article-right>.r300.clearfix.mt10');
        //hideArr.push('');

        var style = hiddenStyle(hideArr);
        style += contentEndStyle('#content');
        style += recomendStyle();
        addStyle(style);

        document.ready(function () {
            //优化推荐列表显示效果
            modifyRecomendWeak('.xgcomm.clearfix');

            //删除空白段落
            var paragraph = document.querySelectorAll('#content p');
            paragraph.forEach(function (item) {
                if (!item.innerText.trim() && !item.querySelector('img,a'))
                    item.parentElement.removeChild(item);
            });

            document.querySelectorAll('iframe').forEach(function (item) {
                if ((item.src && item.src.indexOf('pos.baidu.com') > 0)) {
                    item.parentElement.removeChild(item);
                }
            });

            setTimeout(function () {
                var hideArr = [];
                document.querySelectorAll('div[id]').forEach(function (item) {
                    if (item.id.startsWith('BAIDU_SSP'))
                        hideArr.push('#' + item.id);
                });
                document.querySelectorAll('iframe').forEach(function (item) {
                    if ((item.src && item.src.indexOf('pos.baidu.com') > 0)) {
                        if (item.parentElement.id.length > 0)
                            hideArr.push('#' + item.parentElement.id);
                        else
                            item.parentElement.style.display = 'none !important;';
                    }
                });
                if (hideArr.length > 0)
                    hiddenItems(hideArr);
            }, 200);
        });
    }

    /**
     * 处理 思否 广告
     * segmentfault.com
     */
    function dealSegmentFault() {
        var hideArr = [];
        hideArr.push('.mt20.ad-detail-mm');
        hideArr.push('.side .mb25');
        //hideArr.push('');

        var style = hiddenStyle(hideArr);
        addStyle(style);
    }

    //------------------------------------------------------------------------------------------
    //辅助方法
    function getScrollTop() {
        var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
        if (document.body) {
            bodyScrollTop = document.body.scrollTop;
        }
        if (document.documentElement) {
            documentScrollTop = document.documentElement.scrollTop;
        }
        scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
        return scrollTop;
    }

    //文档的总高度
    function getScrollHeight() {
        var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
        if (document.body) {
            bodyScrollHeight = document.body.scrollHeight;
        }
        if (document.documentElement) {
            documentScrollHeight = document.documentElement.scrollHeight;
        }
        scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
        return scrollHeight;
    }

    function getWindowHeight() {
        var windowHeight = 0;
        if (document.compatMode == "CSS1Compat") {
            windowHeight = document.documentElement.clientHeight;
        } else {
            windowHeight = document.body.clientHeight;
        }
        return windowHeight;
    }

    function hiddenStyle(items) {
        return (items.toString() + ' {display: none !important; }');
    }

    function contentEndStyle(selector) {
        return selector + '::after{content:"— The End —";color:#e0e0e0;text-align:center;width:100%;line-height:60px;margin-top:-40px;display:block;font-family:Monotype Corsiva,Blackadder ITC,Brush Script MT,Bradley Hand ITC,Bell MT;font-size:1.6em;padding-bottom:.6em;}';
    }

    function recomendStyle() {
        var s = '*{transition: opacity 1s, width 1s;}';
        s += '.recomend_weak { height: 180px !important; overflow-y: hidden !important; opacity:.2; }';
        s += '.recomend_show { height:auto !important;  overflow-y !important: visible;opacity:.2; }';
        s += '.recomend_weak:hover { opacity:1; }';
        s += '.recomend_show:hover { opacity:1; }';
        return s;
    }

    function addStyle(s) {
        //添加样式到 Head 中
        var head = document.head || document.getElementsByTagName('head')[0];
        if (head) {
            var style = document.createElement("style");
            style.type = "text/css";
            style.appendChild(document.createTextNode(s));
            head.appendChild(style);
        }
    }

    //优化推荐列表显示效果
    function modifyRecomendWeak(selector) {
        var item = document.querySelector(selector);
        if (item) {
            var func = function () {
                item.removeEventListener('mousemove', func);
                this.classList.add('recomend_show');
                this.classList.remove('recomend_weak');
            };
            item.classList.add('recomend_weak');
            item.addEventListener('mousemove', func);
        }
    }
})();