// ==UserScript==
// @name         ҳ澫
// @namespace    https://greasyfork.org/zh-CN/scripts/23687
// @namespace    https://coding.net/u/BackRunner/p/GreaseMonkey-JS/git
// @version      2.7.15
// @description  õɾűȫȥֹ漰ģ飬ȫָ֧ҳ棬¼
// @author       BackRunner
// @include      *://tieba.baidu.com/*
// @include      *://dq.tieba.com/*
// @include      *://jump2.bdimg.com/*
// @include      *://jump.bdimg.com/*
// @exclude      *://tieba.baidu.com/f/fdir*
// @run-at       document-body
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js
// @license      MIT
// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_info
// ==/UserScript==

// ====  Tips =====
// ʹTamperMonkeyûԽűļλΪdocument-startøЧֲЧȴ뻹ԭ
// ҪעѡҪصģ飬вصģע͵һд
// Ҫע͵ڵ
// ޸ĵĴڸµʱ򱻸ǣ޸ĵ
// 
// ==== Donate ====
// ñűÿһ㻨
// donate@backrunner.top (֧)
// === ־ ===
// 2018.12.26 - 2.7.15
// ޸Chrome°汾ϵļ⡣
// 2018.10.21 - 2.7.14
// һض
// 2018.10.13 - 2.7.13
// jQueryCDNű
// ޸һЩ
// ================


(function() {
    //=========================
    //        
    //=========================
    var sleepTime;
    var sleepTimeWhenPageTurn;
    var reverseSleepTime;
    var postprocess;
    var homePageProcess;
    var groupPageProcess;
    var noLoginProcess;
    var reverse;
    var homeProcess;
    var isHeadimg;
    var checkrate;
    var delayScriptRunTimes;
    var isRedirect;
    var displayLive;
    var displaySign;
    var autoCloseGuide;
    //ű̶
    var isCtrlPanelOn;
    //ײϢdiv
    var foot;
    var homePageMatch = RegExp("(http|https):\/\/tieba.baidu.com\/(#*$)|(http|https):\/\/tieba.baidu.com\/(index\.html#*$)");
    //=====================
    //ݿ޸ģ޸
    //=====================
    var startTime = new Date().getTime();
    var finishTime = -1;
    //=========================
    //        ִ
    //=========================
    //̨Ϣ
    console.warn('ҳ澫 by BackRunner: ʼ');
    //ʼ
    initialize();
    console.warn('ҳ澫 by BackRunner: ');
    //ض
    if (isRedirect){
        redirect();
    }
    //css޸
    appendCSS();
    //Ϣ
    updateAlert();
    //¼
    if (noLoginProcess){
        noLogin();
    }
    if (postprocess){
        //Խűִ
        $(document).ready(function(){
            //ؼʱ
            finishTime = new Date().getTime() - startTime;
            console.log('ҳ澫 by BackRunner: ҳʱ: ' + finishTime);
            console.warn('ҳ澫 by BackRunner: ʼִжԽű');
            //
            try{
                console.warn('ҳ澫 by BackRunner: ڴ');
                if(isCtrlPanelOn){
                    createControlPanel();
                }
            } catch(e){
                console.error(e);
            }
            //ײϢ
            try{
                createFooterLayer();
                addFinishTimeToFooter();
            } catch(e){
                console.error(e);
            }
            //ҳ
            addListenerToNav();
            //бҳ
            if (window.location.search.indexOf("kw=")!= -1 && window.location.href.indexOf("search")==-1){
                addListenerToList();
                adinListClean();
                disableForumCard();
                //Զָ *20180509
                closeGuide();
                //ر *20181013
                removePopupModal();
            } else {
                if (window.location.href.indexOf("tieba.baidu.com/p/") != -1){
                    addListenerToPage();
                    tpointADClean();
                    adinPageClean();
                    reverseorder();
                } else {
                    if (window.location.href.indexOf("tieba.baidu.com/group") != -1){
                        console.warn('ҳ澫 by BackRunner: ǰλȺҳ棬ִȺҳԽű');
                    } else {
                        if (homePageMatch.test(window.location.href)){
                            bindCleanIndexADEvent();
                        } else {
                            console.warn('ҳ澫 by BackRunner: ҳδӳٽű');
                        }
                    }
                }
            }
        });
        //ӳִ
        setTimeout(function(){
            var interval = setInterval(check,sleepTime * checkrate);
            var n=0;
            function check(){
                var times = n+1;
                console.warn('ҳ澫 by BackRunner: ӳٽűִе ' + times + ' ');
                //
                try{
                    console.warn('ҳ澫 by BackRunner: ڴ');
                    if(isCtrlPanelOn){
                        createControlPanel();
                    }
                } catch(e){
                    console.error(e);
                }
                //ײϢ
                try{
                    createFooterLayer();
                } catch(e){
                    console.error(e);
                }
                //ҳ
                addListenerToNav();
                //бҳ
                if (window.location.search.indexOf("kw=")!= -1 && window.location.href.indexOf("search")==-1){
                    addListenerToList();
                    adinListClean();
                    disableForumCard();
                    //Զָ *20180509
                    closeGuide();
                    //ر *20181013
                    removePopupModal();
                } else {
                    if (window.location.href.indexOf("tieba.baidu.com/p/") != -1){
                        addListenerToPage();
                        tpointADClean();
                        adinPageClean();
                        reverseorder();
                    } else {
                        if (homePageMatch.test(window.location.href)){
                            bindCleanIndexADEvent();
                            cleanADinIndex();
                        }
                    }
                }
                reStart();
                function reStart(){
                    n++;
                    if (n>delayScriptRunTimes-1){
                        clearInterval(interval);
                    }
                }
            }
        },sleepTime);
    } else {
        setTimeout(function(){
            var interval = setInterval(check,sleepTime * checkrate);
            var n = 0;
            function check(){
                //
                try{
                    console.warn('ҳ澫 by BackRunner: ڴ');
                    if(isCtrlPanelOn){
                        createControlPanel();
                    }
                } catch(e){
                    console.error(e);
                }
                //ײϢ
                try{
                    createFooterLayer();
                } catch(e){
                    console.error(e);
                }
                n++;
                if (n>delayScriptRunTimes -1){
                    clearInterval(interval);
                }
            }
        });

        $(document).ready(function(){
            //ؼʱ
            finishTime = new Date().getTime() - startTime;
            console.log('ҳ澫 by BackRunner: ҳʱ: ' + finishTime);
            //
            try{
                console.warn('ҳ澫 by BackRunner: ڴ');
                if(isCtrlPanelOn){
                    createControlPanel();
                }
            } catch(e){
                console.error(e);
            }
            //ײϢ
            try{
                createFooterLayer();
                addFinishTimeToFooter();
            } catch(e){
                console.error(e);
            }
        });
        console.warn('ҳ澫 by BackRunner: ű');
    }
    //=========================

    //=========================
    // ޸
    //=========================
    //ʼ
    function initialize(){
        sleepTime = initialize_var("sleepTime",300);
        sleepTimeWhenPageTurn = initialize_var("sleepTimeWhenPageTurn",800);
        reverseSleepTime = initialize_var("reverseSleepTime",200);
        postprocess = initialize_var("postprocess",true);
        isRedirect = initialize_var("isRedirect",true);
        homePageProcess = initialize_var("homePageProcess",true);
        groupPageProcess = initialize_var("groupPageProcess",true);
        noLoginProcess = initialize_var("noLoginProcess",true);
        reverse = initialize_var("reverse",false);
        homeProcess = initialize_var("homeProcess",true);
        isHeadimg = initialize_var("isHeadimg",false);
        checkrate = initialize_var("checkrate",1);
        delayScriptRunTimes = initialize_var("delayScriptRunTimes",6);
        displayLive = initialize_var("displayLive",false);
        displaySign = initialize_var("displaySign",false);
        autoCloseGuide = initialize_var("autoCloseGuide",true);
        //̶ *20180510
        isCtrlPanelOn = initialize_var("isCtrlPanelOn",true);
    }
    function initialize_var(name,defaultSet){
        var obj = GM_getValue(name,defaultSet);
        if (obj === undefined){
            obj = defaultSet;
        }
        return obj;
    }
    //css޸
    function appendCSS(){
        var cssText = "";
        //ֱ
        cssText += '#video_frs_head {display: none !important;}';
        //ҲԱģ
        cssText += '.celebrity {display: none !important;}';
        //ҲҵӦģ
        cssText += '.my_app {display: none !important;}';
        //
        cssText += '.topic_list_box {display: none !important;}';
        //ɫlabel
        cssText += '.label_text {display: none !important;}';
        //ұһͼ
        cssText += '.j_icon_slot {display: none !important;}';
        //Աͼ꣨ûƣһ̶Ӱۣ
        //cssText += '.icon_tbworld {display: none !important;}';
        //ܸȤİ
        cssText += '.forum_recommend {display: none !important;}';
        //ϽӦ
        cssText += '.u_menu_wrap.u_appcenterEntrance_wrap {display: none !important;}';
        //ϽǻԱ
        cssText += '.u_joinvip_wrap.vip_red.j_btn_getmember {display: none !important;}';
        cssText += '.u_joinvip {display: none !important;}';
        //ҳbanner
        cssText += '.l_banner {display: none !important;}';
        //ҳɾsection
        cssText += '.achievement_medal_section {display: none !important;}';
        //ϷϢ
        cssText += '.game-head-game-info-wrapper {display: none !important;}';
        //ҳﰴť
        cssText += '.post-foot-send-gift-btn {display: none !important;}';
        //Աť
        cssText += '.save_face_bg {display: none !important;}';
        cssText += '.save_face_bg_2 {display: none !important;}';
        //ײϷ
        cssText += '#duoku_servers_list {display: none !important;}';
        //Ҳſ
        cssText += '.u9_aside {display: none !important;}';
        //ҲС˵
        cssText += '.novel-ranking-frs-body {display: none !important;}';
        //ͷһͼ
        cssText += '.icon_wrap {display: none !important;}';
        //ɴƹͼƬӳٴǰԤ
        cssText += '.tpoint-imgs {display: none !important;}';
        //½Ͳ
        cssText += '.j-firework-sender {display: none !important;}';
        //̸ֱ
        if (!displayLive){
            cssText += '.interview {display: none !important;}';
        }
        //Ƽ
        cssText += '.thread_recommend {display: none !important;}';
        //ڹ
        cssText += '.iframe_wrapper {display: none !important;}';
        //Ҳ֤
        cssText += '.author-manager {display: none !important;}';
        //placeholder
        cssText += '.tb_poster_placeholder {display: none !important;}';
        //ͼƬǩ
        if (!displaySign){
            cssText += '.j_user_sign {display: none !important;}';
            cssText += '.d_sign_split {display: none !important;}';
        }
        //Ա
        cssText += '.pb_nameplate {display: none !important;}';
        //Ҳ˵
        cssText += '.tbui_fbar_props {display: none !important;}';
        cssText += '.tbui_fbar_tsukkomi {display: none !important;}';
        //ר
        cssText += '.tbui_fbar_bazhu {display: none !important;}';
        //Ҳɵ
        if (window.location.href !== "http://tieba.baidu.com/" && window.location.href !== "https://tieba.baidu.com/"){
            cssText += '.j_click_stats {display: none !important;}';
        }
        //ҲƵƼ
        cssText += '#video_aside {display: none !important;}';
        //һ¥Ӱ
        cssText += '.tpoint-skin {display: none !important;}';
        //ֱҳײ
        cssText += '.attention_tip_wrap {display: none !important;}';
        //ײע/¼
        cssText += '.guidance_fc {display: none !important;}';
        //content-ad span
        cssText += '.content-ad {display: none !important;}';
        //head ad span
        cssText += '.head_ad_tag {display: none !important;}';
        cssText += '.head_ad_pop {display: none !important;}';
        //Աöbubble
        cssText += '#tb_message_tip_d {display: none !important;}';
        //Ӧnav
        cssText += '.app_forum_top_nav {display: none !important;}';
        cssText += '#j_core_title_wrap {top:0px !important;}';
        //Ա
        cssText += '.u_member {display: none !important;}';
        //Ϸ
        cssText += '#j_navtab_game {display: none !important;}';
        //
        cssText += '.ui_bubble_body {display: none !important;}';
        //
        cssText += '.gift-goin {display: none !important;}';
        //Ưƿ
        cssText += '.tbui_fbar_nobottle {display: none !important;}';
        //ɨAPP
        cssText += '.app_download_box {display: none !important;}';
        //ĳҲมcloseť
        cssText += '.j_click_close {display: none !important;}';
        //618 Banner
        cssText += '.showBar {display: none !important;}';
        //Ͻ
        cssText += '.ui_bubble_content {display: none !important}';
        //Hao123
        cssText += '.j_u_menu_extra_url_link {display: none !important}';
        //Ҷ
        cssText += '.search_back_box {display:none !important}';
        //APP
        cssText += '.tbui_fbar_down {display:none !important}';
        //nani
        cssText += '.nani_app_download_box {display:none !important;}';
        //Ա˵
        cssText += '.u_menu_member {display:none !important;}';
        //˵ *20181012
        cssText += '.u_mytbmall,.u_game,.u_blue{display:none !important;}';

        //ȺҳҲ
        if (groupPageProcess){
            if (window.location.href.indexOf("tieba.baidu.com/group") != -1){
                cssText += '.right {display: none !important;}';
            }
        }

        //ҳģ龫
        if (homePageProcess){
            if (homePageMatch.test(window.location.href)){
                //
                try{
                    if (unsafeWindow.PageData.user.is_login === 1){
                        cssText += '.top-sec {display: none !important;}';
                    }
                } catch (e){
                    console.error('ҳ澫 by BackRunner: ¼״̬');
                    console.error(e);
                }
                //ֱ
                cssText += '.spage_liveshow_slide {display: none !important;}';
                //ģΪҲһ»ҳʱģȻڿڵƣһɹҳʱģλȫ
                //߰Ĺģ
                cssText += '#plat_act_wrapper {display: none !important;}';
                //Ѿֲ
                cssText += '.member_rank {display: none !important;}';
                //ýפ
                cssText += '#adide_platform {display: none !important;}';
                //
                cssText += '#media_item {display: none !important;}';
                //ҵϷ
                cssText += '#spage_game_tab_wrapper {display: none !important;}';
                //
                cssText += '#notice_item {display: none !important;}';
                //λ *20171223
                cssText += '.right-sec {margin-top:-15px !important;}';
                //ɾѡר *20171223
                cssText += '.aggregate_entrance_wrap {display:none !important;}';
                //δ¼ʱĹ *20180504
                cssText += '#rec_left {display:none !important;} #rec_right{float:right;position:absolute;right:30px;margin-top:15px;}';
                //ûһЩͼ *20181012
                cssText += '.icon_tbworld {display:none !important;} .user_score{display:none !important;}';
            }
        }

        //ҳ
        if (homeProcess){
            if (window.location.href.indexOf("tieba.baidu.com/home") !== -1){
                //Ҳ
                cssText += ".ihome-aside-gift-center {display: none !important}";
                //鰴ť
                cssText += ".userinfo-marry {display: none !important}";
                //ҵĶᱦ
                cssText += ".ihome-aside-grab-treasure {display: none !important}";
                //ҵT
                cssText += ".userinfo_scores {display: none !important}";
            }
        }

        //бҳͷͼ
        if (window.location.href.indexOf("tieba.baidu.com/f") !== -1){
            if (isHeadimg){
                cssText += ".head_banner_img,.head_ad_pop,.head_banner {display: none !important}";
                //css *20171223
                cssText += '.head_content {padding-top:20px !important}';
                cssText += '.card_banner,.plat_recom_carousel {display:none !important}';
                cssText += '.card_head {width:70px !important;height:70px !important;margin-bottom:10px !important;margin-top:100px !important}';
                cssText += '.card_head_img {width:70px !important;height:70px !important;}';
                cssText += '.card_title {margin-left:-50px !important;}';
                cssText += '.card_slogan {margin-left:-50px !important;}';
            }
        }

        //css
        cssText += '#backrunnerJSSetting_root {right:0 !important;} #backrunnerJSSetting_con {right:0 !important;width:200px important;}';
        cssText += '#backrunnerJSSetting_con a {display:block !important;white-space:nowrap; !important}';
        cssText += '#backrunnerJSSetting_con li {line-height:23px !important;display:block !important;white-space:nowrap !important;}';
        cssText += '#backrunnerJSSetting_con li a {padding-top:0px !important;padding-bottom:0px !important;padding-right:1em !important;padding-left:1em !important;margin:0 !important;line-height:2.4em !important;}';
        cssText += '.search_nav {margin-right:470px !important;}';

        //reversecss *20171223
        cssText += '.br_reverse_li {display:inline-block !important;margin-left:15px !important;margin-top:-14px !important;}';
        cssText += '.core_title_txt {width:400px !important;}';

        //ײϢcss
        cssText += '#footer {padding-bottom:0px !important;}';

        console.log('ҳ澫 by BackRunner: css');

        var element = document.createElement('link');
        element.rel="stylesheet";
        element.type="text/css";
        element.href='data:text/css,'+cssText;

        document.documentElement.appendChild(element);

        var modStyle = document.querySelector('#modCSS');
        if (modStyle === null){
            modStyle = document.createElement('style');
            modStyle.id = 'modCSS';
            document.body.appendChild(modStyle);
            modStyle.innerHTML = cssText;
        }
        console.log('ҳ澫 by BackRunner: css');
    }
    //бҳ
    function addListenerToList(){
        $('#frs_list_pager a').each(function(){
            this.addEventListener('click',listPageTurnEvent);
        });
        console.log('ҳ澫 by BackRunner: бҳ');
    }
    //ӡƷҳ
    function addListenerToNav(){
        $('.nav_list li').each(function(){
            if(this.attributes['data-tab-main'] || this.attributes['data-tab-good']){
                this.getElementsByTagName('a')[0].addEventListener('click',listPageTurnEvent);
            }
        });
    }
    //бҳ¼
    function listPageTurnEvent(){
        console.warn('ҳ澫 by BackRunner: бҳ¼');
        setTimeout(function(){
            var interval = setInterval(check,sleepTimeWhenPageTurn * checkrate);
            function check(){
                addListenerToNav();
                addListenerToList();
                adinListClean();
            }
            setTimeout(function(){
                addListenerToNav();
                addListenerToList();
                adinListClean();
            },sleepTime);
            setTimeout(function(){
                addListenerToNav();
                addListenerToList();
                adinListClean();
                clearInterval(interval);
            },finishTime/3);
        },sleepTimeWhenPageTurn);
    }
    function addListenerToPage(){
        $('.l_pager a').each(function(){
            this.addEventListener('click',pageTurnEvent);
        });
        console.log('ҳ澫 by BackRunner: ӷҳ');
    }
    function pageTurnEvent(){
        console.warn('ҳ澫 by BackRunner: ӷҳ¼');
        setTimeout(function(){
            var interval = setInterval(check,sleepTimeWhenPageTurn * checkrate);
            function check(){
                addListenerToNav();
                addListenerToPage();
                adinPageClean();
                tpointADClean();
                reverseorder();
            }
            setTimeout(function(){
                addListenerToNav();
                addListenerToPage();
                adinPageClean();
                tpointADClean();
                reverseorder();
            },sleepTime);
            setTimeout(function(){
                addListenerToNav();
                addListenerToPage();
                adinPageClean();
                tpointADClean();
                reverseorder();
                clearInterval(interval);
            },finishTime/3);
        },sleepTimeWhenPageTurn);
    }
    //ƹ
    function tpointADClean(){
        console.log('ҳ澫 by BackRunner: ʼ򴥵ƹ');
        var pointad = document.getElementsByClassName('tpoint-imgs');
        console.log('ҳ澫 by BackRunner: ץȡĹԪ: '+pointad.length);
        for(var i=0;i<pointad.length;i++){
            pointad[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.removeChild(pointad[i].parentNode.parentNode.parentNode.parentNode.parentNode);
        }
        console.log('ҳ澫 by BackRunner: ƹ㾫');
    }

    //бڹ
    function adinListClean(){
        console.log('ҳ澫 by BackRunner: ʼбڹ');
        var adinList = document.getElementsByClassName('threadlist_rep_num');
        var num = 0;
        console.log('ҳ澫 by BackRunner: бԪץȡ: '+adinList.length);
        for (var i=0;i<adinList.length;i++){
            if (adinList[i].title === "" || adinList[i].title === ""){
                num++;
                adinList[i].parentNode.parentNode.parentNode.parentNode.removeChild(adinList[i].parentNode.parentNode.parentNode);
            }
        }
        console.log('ҳ澫 by BackRunner: бڹ澫ϣ '+ num + ' ');
        //class
        var list = document.getElementById('thread_list').children;
        for (i=0;i<list.length;i++){
            var className=list[i].getAttribute('class');
            if (className != null){
                if (list[i].getAttribute('class').indexOf('thread_top_list_folder')==-1 && list[i].getAttribute('class').indexOf('j_thread_list')==-1){
                    list[i].parentNode.removeChild(list[i]);
                }
            }
        }
        console.log('ҳ澫 by BackRunner: ɨб');
        //tab *20181012
        var tabs = document.getElementsByClassName("j_tbnav_tab_a");
        for (i=0;i<tabs.length;i++){
            if (tabs[i].innerHTML.indexOf("ֱ") != -1){
                tabs[i].parentNode.removeChild(tabs[i]);
            }
            if (tabs[i].innerHTML.indexOf("Ӧ") != -1){
                tabs[i].parentNode.removeChild(tabs[i]);
            }
        }
    }

    //ڹ
    function adinPageClean(){
        console.log('ҳ澫 by BackRunner: ʼڹ');
        var ad = document.getElementsByClassName('iframe_wrapper');
        console.log('ҳ澫 by BackRunner: ץȡĹԪ: '+ad.length);
        for(var i=0;i<ad.length;i++){
            ad[i].parentNode.parentNode.removeChild(ad[i].parentNode);
        }
        console.log('ҳ澫 by BackRunner: ڹ澫');
        //ڲ
        var plus = document.getElementsByClassName('d_name');
        for (i=0;i<plus.length;i++){
            var dat = plus[i].getAttribute('data-field');
            if(!dat || !dat.replace(/\s/g,'')){
                plus[i].parentNode.parentNode.parentNode.parentNode.removeChild(plus[i].parentNode.parentNode.parentNode);
            }
        }
    }

    //¼
    function noLogin(){
        try{
            var login = document.createElement('script');
            login.innerHTML="PageData.user.is_login = true";
            document.head.appendChild(login);
        } catch(e){
            console.error('ҳ澫 by BackRunner: ¼ش');
            console.error(e);
        }
    }

    //
    function updateAlert(){
        var s_update = "ҳ澫 by BackRunner\n⵽ű汾\n\n";
        var version = GM_getValue("version");
        if (version !== GM_info.script.version){
            console.warn("ҳ澫 by BackRunner⵽ű汾ģ" + version + "  " + GM_info.script.version);
            if (version === undefined){
                version = "δ֪";
            }
            switch (version){
                default:
                    //汾ʱɾ
                    deleteTrashValue();
                    s_update += "汾Ѵ " + version + " Ϊ " + GM_info.script.version + "\n\n" + GM_info.script.version + "汾ĸΪ\n޸°Chromeļ⡣\n\nBug뼰ʱύл\n\nҪѣؿ\nûаװAdblock밲װAdblockԻ\n\nűѾȽȶֻ޸Bug͸ɵı仯¹\n";
                    break;
                case "δ֪":
                    s_update += "ӭʹҳ澫ű by BackRunner\nǰĽű汾Ϊ " + version + "\n\ná\nͨϽǵعԻ\n\nҪѣؿ\nûаװAdblock밲װAdblockԻ\n\nűѾȽȶֻ޸Bug͸ɵı仯¹\n";
                    break;
                case "2.7.15":
                    s_update += "汾Ѵ " + version + " Ϊ " + GM_info.script.version + "\n\n" + "ʹ°汾ĽűԻ\nöʧ\n";
                    break;
            }
            s_update += "\nκ̵GreasyFork\n߷ʼdev@backrunner.top\nñűÿʹ֧ɨGreasyForkеĶάײеĶάҾ\nյľһὫid뵽л\nлʾͽű";
            alert(s_update);
            GM_setValue("version",GM_info.script.version);
        } else {
            console.warn("ҳ澫 by BackRunnerδ⵽ű汾");
        }
    }
    //
    function createControlPanel(){
        if (document.querySelector('#backrunnerJSSetting_root') === null){
            try{
                //userbarȡ㴴
                var userbar = $('#com_userbar').children('ul')[0];
                var li = document.createElement('li');
                var div = document.createElement('div');
                var a = document.createElement('a');

                //
                a.innerHTML = "ҳ澫ű";
                div.setAttribute('style','padding-top:6px');

                //¼
                div.addEventListener('mouseenter',cpOnMouseEnter);
                div.addEventListener('mouseleave',cpOnMouseLeave);

                //˵㴴
                var menuroot = document.createElement('div');
                var menucon = document.createElement('div');
                var menudiv = document.createElement('div');
                menuroot.setAttribute('class','u_ddl');
                menuroot.setAttribute('id','backrunnerJSSetting_root');
                menucon.setAttribute('class','u_ddl_con');
                menucon.setAttribute('id','backrunnerJSSetting_con');
                menudiv.setAttribute('class','u_ddl_con_top');
                menudiv.setAttribute('id','backrunnerJSSetting_main');
                menucon.appendChild(menudiv);
                menuroot.appendChild(menucon);

                //˵¼
                menuroot.addEventListener('mouseenter',cpOnMouseEnter);
                menuroot.addEventListener('mouseleave',cpOnMouseLeave);

                //˵б
                var menu = document.createElement('ui');
                //0:ı 1:ѡ 2:ť
                createMenuItem(menu,1,"postprocess","ű","");
                createMenuItem(menu,1,"isRedirect","ض","");
                createMenuItem(menu,1,"homepageprocess","ҳ","");
                createMenuItem(menu,1,"isHeadimg","бҳͷͼ","");
                createMenuItem(menu,1,"displayLive","Ƿʾֱ","");
                createMenuItem(menu,1,"displaySign","Ƿʾûǩ","");
                createMenuItem(menu,1,"autoCloseGuide","Զָ","");
                createMenuItem(menu,1,"homeProcess","ҳ","");
                createMenuItem(menu,1,"groupprocess","Ⱥҳ澫","");
                createMenuItem(menu,1,"nologin","¼","");
                createMenuItem(menu,1,"reverse","ʵԣ","");
                createMenuItem(menu,0,"sleepTime","űӳʱ");
                createMenuItem(menu,0,"delayScriptRunTimes","ӳٽűִд","");
                createMenuItem(menu,0,"sleepTimePage","ҳűӳʱ","");
                createMenuItem(menu,0,"reverseSleepTime","鿴űӳʱ","");
                createMenuItem(menu,0,"checkrate","űӳʱ䱶","");
                createMenuItem(menu,2,"refresh","ˢҳ","ˢ");
                createMenuItem(menu,2,"submit","ύ","ύ");
                createMenuItem(menu,2,"reset","","");
                menudiv.appendChild(menu);

                //˵
                div.appendChild(a);
                div.setAttribute('class','u_menu_item');
                div.setAttribute('id','backrunnerJSSetting_item');
                li.appendChild(div);
                li.appendChild(menuroot);
                userbar.appendChild(li);

                //˵ݳʼ
                menuInitialize();

                //˵¼
                createMenuEvent();
            } catch(e){
                console.error(e);
            }
        } else {
            console.warn('ҳ澫 by BackRunner: Ѵ');
        }
    }
    //˵
    function createMenuItem(menu,type,id,labelContent,innerContent){
        //㴴
        var li = document.createElement('li');
        var div = document.createElement('div');
        var d_a = document.createElement('div');
        var d_i = document.createElement('div');
        //
        d_a.setAttribute('style','float:left;width:150px');
        switch(type){
            case 0:
                d_i.setAttribute('style','padding-top:7px;padding-left:150px;padding-right:10px;');
                break;
            case 1:
                d_i.setAttribute('style','padding-top:8px;padding-left:192px;padding-right:10px;');
                break;
            case 2:
                d_i.setAttribute('style','padding-top:6px;padding-left:150px;padding-right:10px;');
                break;
        }
        //Ԫش
        var a = document.createElement('a');
        var i = document.createElement('input');
        //Ԫ
        a.innerHTML = labelContent;
        switch(type){
            case 0:
                i.setAttribute('style','width:50px;text-align:right');
                i.setAttribute('id','backrunner_i_'+id);
                break;
            case 1:
                //ѡ
                i.setAttribute('type','checkbox');
                i.setAttribute('id','backrunner_i_'+id);
                break;
            case 2:
                //ť
                i = document.createElement('button');
                i.innerHTML = innerContent;
                i.setAttribute('style','width:52px');
                i.setAttribute('id','backrunner_btn_'+id);
                break;
        }

        //Ԫӵ
        d_a.appendChild(a);
        d_i.appendChild(i);
        //Ԫزӵ
        div.appendChild(d_a);
        div.appendChild(d_i);
        //ӵli
        li.appendChild(div);
        //liӵmenu
        menu.appendChild(li);
    }
    //˵ʼ
    function menuInitialize(){
        menuInitialize_checkbox('postprocess',postprocess);
        menuInitialize_checkbox('isRedirect',isRedirect);
        menuInitialize_checkbox('homepageprocess',homePageProcess);
        menuInitialize_checkbox('homeProcess',homeProcess);
        menuInitialize_checkbox('isHeadimg',isHeadimg);
        menuInitialize_checkbox('groupprocess',groupPageProcess);
        menuInitialize_checkbox('nologin',noLoginProcess);
        menuInitialize_checkbox('reverse',reverse);
        menuInitialize_checkbox('displayLive',displayLive);
        menuInitialize_checkbox('displaySign',displaySign);
        menuInitialize_checkbox('autoCloseGuide',autoCloseGuide);
        menuInitialize_input('sleepTime',sleepTime);
        menuInitialize_input('delayScriptRunTimes',delayScriptRunTimes);
        menuInitialize_input('sleepTimePage',sleepTimeWhenPageTurn);
        menuInitialize_input('reverseSleepTime',reverseSleepTime);
        menuInitialize_input('checkrate',checkrate);
    }
    //˵checkboxʼ
    function menuInitialize_checkbox(id,variable){
        var i = document.getElementById('backrunner_i_'+id);
        if (i !== null){
            if (variable){
                i.checked = true;
            } else {
                i.checked = false;
            }
        }
    }
    //˵inputʼ
    function menuInitialize_input(id,variable){
        var i = document.getElementById('backrunner_i_'+id);
        if (i !== null){
            i.value = variable;
        }
    }

    //˵¼
    function createMenuEvent(){
        try{
            //ˢ
            var btn_refresh = document.getElementById('backrunner_btn_refresh');
            if (btn_refresh !== null){
                btn_refresh.addEventListener('click',btn_refresh_click);
            }
            //ύ
            var btn_submit = document.getElementById('backrunner_btn_submit');
            if (btn_submit !== null){
                btn_submit.addEventListener('click',btn_submit_click);
            }
            //
            var btn_reset = document.getElementById('backrunner_btn_reset');
            if (btn_reset !== null){
                btn_reset.addEventListener('click',btn_reset_click);
            }
        }catch(e){
            console.error(e);
        }
    }
    //ˢ°ť¼
    function btn_refresh_click(){
        window.location.reload(true);
    }
    //ύť¼
    function btn_submit_click(){
        postprocess = convertBoolValue('postprocess');
        isRedirect = convertBoolValue('isRedirect');
        homePageProcess = convertBoolValue('homepageprocess');
        homeProcess = convertBoolValue('homeProcess');
        isHeadimg = convertBoolValue('isHeadimg');
        displayLive = convertBoolValue('displayLive');
        displaySign = convertBoolValue('displaySign');
        autoCloseGuide = convertBoolValue('autoCloseGuide');
        groupPageProcess = convertBoolValue('groupprocess');
        noLoginProcess = convertBoolValue('nologin');
        reverse = convertBoolValue('reverse');
        sleepTime = convertNumValue('sleepTime');
        delayScriptRunTimes = convertNumValue('delayScriptRunTimes');
        sleepTimeWhenPageTurn = convertNumValue('sleepTimePage');
        reverseSleepTime = convertNumValue('reverseSleepTime');
        checkrate = convertNumValue('checkrate');

        if (postprocess){
            if (window.confirm('ȷҪã')){
                replaceStoredSettings();
                menuInitialize();
                window.location.reload(true);
            } else {
                menuInitialize();
            }
        } else {
            if (window.confirm('Ҫ\n˺űű󲿷ֹܽ޷ʹ\n\nȷҪã')){
                replaceStoredSettings();
                menuInitialize();
                window.location.reload(true);
            } else {
                menuInitialize();
            }
        }
    }
    function btn_reset_click(){
        if (window.confirm('ȷҪã')){
            GM_deleteValue("postprocess");
            GM_deleteValue("isRedirect");
            GM_deleteValue("sleepTime");
            GM_deleteValue("delayScriptRunTimes");
            GM_deleteValue("displayLive");
            GM_deleteValue("displaySign");
            GM_deleteValue("autoCloseGuide");
            GM_deleteValue("sleepTimeWhenPageTurn");
            GM_deleteValue("reverseSleepTime");
            GM_deleteValue("homePageProcess");
            GM_deleteValue("homeProcess");
            GM_deleteValue("isHeadimg");
            GM_deleteValue("groupPageProcess");
            GM_deleteValue("noLoginProcess");
            GM_deleteValue("reverse");
            GM_deleteValue("checkrate");
            window.location.reload(true);
        }
    }
    //˵еboolֵ
    function convertBoolValue(id){
        var i = document.getElementById('backrunner_i_'+id);
        if (i !== null){
            if (i.checked){
                return true;
            } else {
                return false;
            }
        }
    }
    //˵еֵ
    function convertNumValue(id){
        var i = document.getElementById('backrunner_i_'+id);
        if (i !== null){
            return(parseFloat(i.value));
        }
    }
    //¼
    function cpOnMouseEnter(){
        try{
            var cp = document.getElementById('backrunnerJSSetting_root');
            cp.setAttribute('style','display:block;right:0;');
        } catch(e){
            console.error(e);
        }
    }
    function cpOnMouseLeave(){
        try{
            var cp = document.getElementById('backrunnerJSSetting_root');
            cp.setAttribute('style','display:none;right:0');
        } catch(e){
            console.error(e);
        }
    }

    //滻
    function replaceStoredSettings(){
        GM_setValue("postprocess",postprocess);
        GM_setValue("isRedirect",isRedirect);
        GM_setValue("sleepTime",sleepTime);
        GM_setValue("delayScriptRunTimes",delayScriptRunTimes);
        GM_setValue("displayLive",displayLive);
        GM_setValue("displaySign",displaySign);
        GM_setValue("autoCloseGuide",autoCloseGuide);
        GM_setValue("sleepTimeWhenPageTurn",sleepTimeWhenPageTurn);
        GM_setValue("reverseSleepTime",reverseSleepTime);
        GM_setValue("homePageProcess",homePageProcess);
        GM_setValue("homeProcess",homeProcess);
        GM_setValue("isHeadimg",isHeadimg);
        GM_setValue("groupPageProcess",groupPageProcess);
        GM_setValue("noLoginProcess",noLoginProcess);
        GM_setValue("reverse",reverse);
        GM_setValue("checkrate",checkrate);
    }

    //ײϢ
    function createFooterLayer(){
        var footDiv;
        var id;
        if (document.querySelector('#backrunnerJSFooter')===null){
            if (window.location.href.indexOf("tieba.baidu.com/f?") !== -1){
                try{
                    footDiv = document.getElementsByClassName('frs_content_footer_pagelet')[0];
                    foot = document.createElement('div');
                    id = document.createElement('div');
                    foot.setAttribute('class','footer');
                    foot.setAttribute('id','footer');
                    foot.setAttribute('style','padding-top:0px !important;');
                    id.setAttribute('id','backrunnerJSFooter');
                    createFooterElement(foot,"ӦBackRunnerɾű ("+ GM_info.script.version+")","https://greasyfork.org/zh-CN/scripts/23687",false);
                    createFooterElement(foot,"(֧ά)","https://smallfile.backrunner.top/images/alipay.jpg",true);
                    footDiv.appendChild(foot);
                    footDiv.appendChild(id);
                    console.warn('ҳ澫 by BackRunner: ײϢ');
                } catch(e){
                    console.error(e);
                }
            } else {
                if (window.location.href.indexOf("tieba.baidu.com/p/") !== -1){
                    try{
                        footDiv = document.getElementsByClassName('pb_footer')[0];
                        foot = document.createElement('div');
                        id = document.createElement('div');
                        foot.setAttribute('class','footer');
                        foot.setAttribute('id','footer');
                        foot.setAttribute('style','padding-top:0px !important;');
                        id.setAttribute('id','backrunnerJSFooter');
                        createFooterElement(foot,"ӦBackRunnerɾű ("+ GM_info.script.version+")","https://greasyfork.org/zh-CN/scripts/23687",false);
                        createFooterElement(foot,"߾(֧ά)","https://smallfile.backrunner.top/images/alipay.jpg",true);
                        footDiv.appendChild(foot);
                        footDiv.appendChild(id);
                        console.warn('ҳ澫 by BackRunner: ײϢ');
                    } catch(e){
                        console.error(e);
                    }
                }
            }
        } else {
            console.warn('ҳ澫 by BackRunner: ײϢѴ');
        }
    }
    function addFinishTimeToFooter(){
        if (finishTime !== -1){
            addSplitLineToFooter(foot);
            createFooterElement(foot,"ҳʱ䣺" + finishTime+"ms","javascript:;",true);
        }
    }
    function addSplitLineToFooter(footer){
        try{
            var splitLine=document.createElement('span');
            splitLine.innerHTML="|";
            footer.appendChild(splitLine);
        }catch(e){
            console.error(e);
        }
    }
    function createFooterElement(footer,text,href,isLast){
        var splitLine=document.createElement('span');
        splitLine.innerHTML="|";
        var content = document.createElement('a');
        content.innerHTML=text;
        content.setAttribute('href',href);
        content.setAttribute('target',"_blank");
        content.setAttribute('pv_code',"0");
        footer.appendChild(content);
        if (!isLast){
            footer.appendChild(splitLine);
        }
    }
    //ͷͼ
    function disableForumCard(){
        if (isHeadimg){
            try{
                var forumcard = document.getElementById('forum-card-banner');
                console.warn('ҳ澫 by BackRunner: ھͷͼ');
                if (forumcard !== null){
                    if (forumcard.getAttribute("src")!==""){
                        forumcard.setAttribute("src","");
                        forumcard.setAttribute("style","height:180px !important;background-color:white !important;");
                    }
                }
            } catch(e){
                console.error(e);
            }
        }
    }
    //ض
    function redirect(){
        var url = window.location.href;
        //?red_tagض
        var elements = window.location.search.toString().split('&');
        for (var i = 0;i<elements.length;i++){
            if (elements[i].indexOf('red_tag') !== -1){
                url = url.replace('&'+elements[i],'');
                url = url.replace(elements[i],'');
            }
        }
        //ض
        if (url.indexOf('dq.tieba.com')!=-1){
            url = url.replace('dq.tieba.com','tieba.baidu.com');
        }
        if (url.indexOf('jump.bdimg.com')!=-1){
            url = url.replace('jump2.bdimg.com','tieba.baidu.com');
        }
        if (url.indexOf('jump2.bdimg.com')!=-1){
            url = url.replace('jump2.bdimg.com','tieba.baidu.com');
        }
        if (url.indexOf('?traceid=')!=-1){
            url = url.replace('?traceid=','');
        }
        if (url !== window.location.href){
            window.location = url;
        }
    }
    //ť
    function reverseorder(){
        if (reverse){
            //жǰ״̬
            var status = initialize_var('reverse_status',false);
            if (document.querySelector('#br_reverse') === null){
                var rightbtn = document.getElementsByClassName('core_title_btns')[0];
                var btnli = document.createElement('li');
                var btn = document.createElement('a');
                console.warn('ҳ澫 by BackRunner: ڴ鿴ť');
                //жǷΪһ
                var lastpage = initialize_var('reverse_lastpage','');
                var currenthref = window.location.href;
                var currentsearch = window.location.search;
                var currentpage = currenthref.replace(currentsearch,'').replace('https://tieba.baidu.com/p/','').replace('http://tieba.baidu.com/p/','');
                if (currentpage!=lastpage){
                    status = false;
                }
                GM_setValue('reverse_lastpage',currentpage);
                GM_setValue('reverse_status',status);
                if (status){
                    btn.innerHTML = '鿴';
                }else {
                    btn.innerHTML='鿴';
                }
                btn.setAttribute('id','br_reverse');
                btn.setAttribute('class','btn-sub btn-small');
                btnli.setAttribute('class','br_reverse_li quick_reply');
                btnli.appendChild(btn);
                rightbtn.appendChild(btnli);
                rightbtn.addEventListener('click',reverse_click);
                currenthref = window.location.href;
                reverse_contents();
            }
        }
    }
    //¼
    function reverse_click(){
        var status = initialize_var('reverse_status',false);
        if(status){
            console.log('click');
            //ָ
            GM_setValue('reverse_status',false);
            var btns = document.getElementsByClassName('pb_list_pager')[0].children;
            if (btns.length === 0){
                window.location.reload(true);
            }else {
                for (var i = 0;i < btns.length;i++){
                    if (btns[i].innerHTML == '1'){
                        //ҳת
                        window.location.href = btns[i].href;
                    }
                }
            }
        } else {
            //
            GM_setValue('reverse_status',true);
            btns = document.getElementsByClassName('pb_list_pager')[0].children;
            if (btns.length === 0){
                window.location.reload(true);
            }else {
                for (var j = 0;j<btns.length;j++){
                    if (btns[j].innerHTML == 'βҳ'){
                        //βҳת
                        window.location.href = btns[j].href;
                    }
                }
            }
        }
    }
    //תǰҳ
    function reverse_contents(){
        setTimeout(function(){
            var status = initialize_var('reverse_status',false);
            if (status){
                console.warn('ҳ澫 by BackRunner: ڷתǰҳ');

                //ȡǰpn
                var search = window.location.search;
                var searchs = search.split('&');
                var pn;
                for (var child = 0;child<searchs.length;child++){
                    if (searchs[child].indexOf('pn=') !== -1){
                        pn = searchs[child].replace('?','').replace('pn=','');
                    }
                }
                var list = document.getElementById('j_p_postlist');
                var contents = list.children;
                console.warn(contents);
                if (contents.length == 1 && contents[0].id == "j_p_postlist"){
                    list = document.getElementsByClassName('p_postlist')[1];
                    contents = list.children;
                }
                console.warn(contents);
                //
                for(var i = contents.length - 1; i >= 0; i--) {
                    list.appendChild(contents[i]);
                }
            }
        },reverseSleepTime);
    }
    //Զָ
    var isAutoCloseGuideSet = false;
    function closeGuide(){
        if (autoCloseGuide && !isAutoCloseGuideSet){
            var topContent = document.getElementsByClassName("top_content");
            var topContentMain = document.getElementsByClassName("top_cont_main");
            if (topContent.length>0 && topContentMain.length>0){
                topContent[0].setAttribute("class","top_content top_content_closed");
                topContentMain[0].setAttribute("style","display:none;");
                isAutoCloseGuideSet = true;
            }
        }
    }
    //汾ɾ
    function deleteTrashValue(){
        var trashList = {};
        for (var i = 0;i<trashList.length;i++){
            GM_deleteValue("trashList[i]");
        }
        console.warn('ҳ澫 by BackRunner: 汾£ɾ');
    }
    //ۿȵ
    function removePopupModal(){
        var dialog = document.getElementsByClassName('dialogJ');
        var dialogmodal = document.getElementsByClassName('dialogJmodal');
        for (var i = 0;i<dialog.length;i++){
            dialog[i].parentNode.removeChild(dialog[i]);
        }
        for (i = 0;i<dialogmodal.length;i++){
            dialogmodal[i].parentNode.removeChild(dialogmodal[i]);
        }
        setTimeout(function(){
            var dialog = document.getElementsByClassName('dialogJ');
            var dialogmodal = document.getElementsByClassName('dialogJmodal');
            for (var i = 0;i<dialog.length;i++){
                dialog[i].parentNode.removeChild(dialog[i]);
            }
            for (i = 0;i<dialogmodal.length;i++){
                dialogmodal[i].parentNode.removeChild(dialogmodal[i]);
            }
        },5000);
    }
    //ҳ̬ڵĹ *20181022
    var indexADEvent_a = false;
    var indexADEvent_scroll = false;
    var indexADEvent_tag_hot = false;
    var indexADEvent_tag_personal = false;
    var windowTempScrollTop = 0;
    function bindCleanIndexADEvent(){
        var btn = document.getElementById('btn_more');
        var a = btn.children[0];
        if (!indexADEvent_a){
            console.log("ҳ澫 by BackRunner: Ѱҳ̬İť¼");
            a.addEventListener('click',function(){
                setTimeout(function(){
                    cleanADinIndex();
                },sleepTime);
            });
            indexADEvent_a = true;
        }
        if (!indexADEvent_scroll){
            console.log("ҳ澫 by BackRunner: Ѱҳ̬Ĺ¼");
            document.onscroll = function(){
                if (document.documentElement.scrollTop - windowTempScrollTop > 300){
                    console.log("ҳ澫 by BackRunner: ̬ڵĹ");
                    cleanADinIndex();
                    windowTempScrollTop = document.documentElement.scrollTop;
                }
            }
            indexADEvent_scroll = true;
        }
        if (!indexADEvent_tag_hot){
            var tag_hot = document.getElementById('j_remen_nav');
            tag_hot.addEventListener('click',function(){
                clearScrollTopTemp();
            });
            indexADEvent_tag_hot = true;
        }
        if (!indexADEvent_tag_personal){
            var tag_personal = document.getElementById('nav-personal');
            tag_personal.addEventListener('click',function(){
                clearScrollTopTemp();
            });
            indexADEvent_tag_hot = true;
        }
    }
    function clearScrollTopTemp(){
        console.log("ҳ澫 by BackRunner: ߶Ȼ");
        windowTempScrollTop = 0;
    }
    function cleanADinIndex(){
        var ads = document.getElementsByClassName('home-place-item');
        var count = 0;
        for (var i = 0;i<ads.length;i++){
            var extinfo = ads[i].getAttribute('data-ext_info');
            if (typeof extinfo != 'undefined'){
                ads[i].parentNode.removeChild(ads[i]);
                count++;
            };
        }
        console.log("ҳ澫 by BackRunner: "+count+"̬ڵĹ");
    }
})();