tieba page

显示手机版贴吧里被隐藏的楼层与翻页按钮及其它增强

目前為 2020-03-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         tieba page
// @namespace    http://tampermonkey.net/
// @version      0.72
// @author       fthvgb1
// @match        https://tieba.baidu.com/*
// @grant        GM_xmlhttpRequest
// @description 显示手机版贴吧里被隐藏的楼层与翻页按钮及其它增强
// ==/UserScript==




(function () {
    'use strict';


    function gif3(v) {
        var imgs = v.querySelectorAll('img.BDE_Image');
        if (imgs.length > 0) {
            imgs.forEach(img => {
                var src = img.src;
                var s = /&src=(.*)/.exec(src);
                if (s != null) {
                    var x = s.length > 0 ? s[1] : src;
                    img.src = decodeURIComponent(x);
                }

            })
        }
    }

    function gif(v) {
        var imgs = v.querySelectorAll('div[data-class="BDE_Image"]');
        if (imgs.length > 0) {
            imgs.forEach(value => {
                var src = decodeURIComponent(value.getAttribute('data-url'));

                //console.log(src);
                var s = /&src=(.*)/.exec(src);
                if (s != null) {
                    var ss = s[1];
                    var img = document.createElement('img');
                    img.src = (ss);
                    img.className = 'BDE_Image';
                    value.outerHTML = img.outerHTML;
                }
            })
        }
        gif3(v)
    }

    function delElement(selectors) {
        selectors.forEach(value => {
            let x = document.querySelector(value);
            if (x) {
                x.parentNode.removeChild(x)
            }
        });
    }

    function t() {
        lz();
        $("ul#pblist>li").forEach(function (e, iii) {
            f(e);
            if (iii === 0) {
                var oo = e.querySelectorAll('.pb_img_item');
                if (oo.length > 0) {
                    oo.forEach(value => {
                        if (value.getAttribute('data-url')) {
                            value.setAttribute('data-class', 'BDE_Image');
                            value.setAttribute('src', value.getAttribute('data-url'));
                        }
                    });
                    //oo[0].parentElement.outerHTML=`<span class="wrap pbimgwapper">${oo[0].parentElement.innerHTML}</span>`
                }
               

            }
            delElement(['#diversBanner', '.j_videoFootDownBtn']);

            gif(e);
            let videos = e.querySelectorAll('.video');
            if(videos.length>0){
                videos.forEach(video=>{
                    let src = video.getAttribute('data-vhsrc');
                    let img = video.querySelector('img');
                    video.outerHTML=`<video poster="${img.src}" src="${src}" controls="controls"  style="max-width:100%;min-width:100%"></video>`;
                });
            }

            var ee = $(e);
            var tid = ee.attr("tid");
            var content = ee.find(".list_item_top");
            var x = ee.find('.list_item_top a.j_report_btn');
            if (x && x.length > 0) {
                var kz = x[0].href.match(/tid=(\d+)&/);
                kz = kz[1];
            }

            var floor = e.getElementsByClassName('pb_floow_load');
            if (floor.length > 0) {
                var text = floor[0].textContent;
                var url = `https://tieba.baidu.com/t/p/${tid}`;
                var num = parseInt(text.match(/\d+/));
                content.append(`<div style="text-align:center;background-color: #eee;margin: 8px 0 0 42px;"><a style="padding:12px;display:block;" href="javascript:void(0)" data-url="${url}" class="reply">查看剩余` + num + `条回复</a></div>`);
                var res = content.find('a.reply');
                var orgnum = num;
                //console.log(content,res);
                if (res) {
                    res.forEach(function (v, i) {
                        var page = 2;
                        v.addEventListener('click', function () {
                            var that = this;

                            if (num === orgnum) {
                                var url = this.getAttribute('data-url');
                                $.get(url, function (rst) {
                                    var dom = (new DOMParser()).parseFromString(rst, 'text/html');
                                    var r = dom.querySelector('.j_floor_panel');
                                    var lii = r.querySelectorAll('li');
                                    lii.forEach(function (li, index) {
                                        if (index < 2) {
                                            return;
                                        }
                                        var username = li.querySelector('.left>div .user_name').outerHTML;
                                        username = username.replace('</a>', ':</a>');
                                        var s = li.querySelector('.content span');
                                        s.className = 'floor_content';
                                        var c = li.querySelector('.content').innerHTML;
                                        var div = `
        <div class="fmain j_floor_main">
            <div class="floor_footer_item">
            ${username}
            ${c}
            </div>
        </div>`;
                                        li.innerHTML = div;
                                        var ll = document.createElement('li');
                                        ll.classList.add('list_item_floor');
                                        ll.classList.add('j_list_item_floor');
                                        ll.innerHTML = div;
                                        //console.log(content.find('.flist'))
                                        content.find('.flist')[0].appendChild(ll)

                                    });
                                    if (num <= 8) {
                                        that.parentNode.removeChild(that);
                                    } else {
                                        num -= 8;
                                        that.innerText = `查看剩余${num}条回复`;
                                    }
                                });
                            } else {
                                var url = `https://tieba.baidu.com/mo/q//flr?fpn=${page}&kz=${kz}&pid=${tid}&is_ajax=1&has_url_param=0&template=lzl`;
                                $.get(url, function (res) {
                                    var ht = (new DOMParser()).parseFromString(res.data.floor_html, 'text/html');
                                    var lii = ht.querySelectorAll('li');
                                    lii.forEach(function (li, index) {
                                        var username = li.querySelector('.left>div .user_name').outerHTML;
                                        username = username.replace('</a>', ':</a>');
                                        var s = li.querySelector('.content span');
                                        s.className = 'floor_content';
                                        var c = li.querySelector('.content').innerHTML;
                                        var div = `
        <div class="fmain j_floor_main">
            <div class="floor_footer_item">
            ${username}
            ${c}
            </div>
        </div>`;
                                        li.innerHTML = div;
                                        var ll = document.createElement('li');
                                        ll.classList.add('list_item_floor');
                                        ll.classList.add('j_list_item_floor');
                                        ll.innerHTML = div;
                                        //console.log(content.find('.flist'))
                                        content.find('.flist')[0].appendChild(ll)

                                    });
                                    ++page;
                                    if (num > 10) {
                                        num -= 10;
                                        that.innerText = `查看剩余${num}条回复`;
                                    } else {
                                        that.parentNode.removeChild(that);
                                    }
                                })
                            }

                        })
                    })
                }
                floor[0].parentNode.removeChild(floor[0])
            }

        });
    }

    function lz() {
        var lz = document.querySelector('span.poster_only');
        if (lz) {
            lz.onclick = null;
            var h = location.href;
            var ff = 0;
            if (h.indexOf('see_lz=1') > -1) {
                lz.textContent = '取消只看楼主';
                h = h.replace('see_lz=1', 'see_lz=0');
                ff = 1;
            }
            lz.addEventListener('click', () => {

                if (ff === 0) {
                    h = h.indexOf('?') < 0 ? h + '?see_lz=1' : h + '&see_lz=1';
                }
                location.href = h;

            });
        }
    }

    function f(value) {
        var dt = JSON.parse(value.getAttribute('data-info'));
        if (dt) {
            var fl = dt.floor_num;
            var l = document.createElement('span');
            l.style.color = 'green';
            l.textContent = fl + '楼';
            if (fl === 1) {
                l.textContent = '楼主';
            } else if (fl === 2) {
                l.textContent = '沙发';
            } else if (fl === 3) {
                l.textContent = '板凳';
            }
            value.querySelector('.list_item_time').parentNode.appendChild(l);
        }
    }

    function check() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
        var flag = 0;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > -1) {
                flag = 1;
                break;
            }
        }
        return flag;
    }

    function createTime() {
        let url = location.href.replace('&mo_device=1', '');
        url = decodeURIComponent(url);
        if(url.indexOf('/mo/')>-1){
            let word = /word=(.*?)&/.exec(url)[1];
            url = url.replace('mo/q/m','f').replace(/word=(.*?)&/,'kw='+word+'&');        
        }
        GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            headers: {
                "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
            },
            //responseType: obj.responseType,
            onload: function (res) {
                let r = (new DOMParser()).parseFromString(res.responseText, 'text/html');
                let w = r.getElementById('pagelet_html_frs-list/pagelet/thread_list').innerHTML;
                let ul = w.replace('<!--', '').replace('-->', '');
                let u = document.createElement('div');
                u.innerHTML = ul;
                let lis = u.querySelectorAll('li.j_thread_list');
                if (lis.length > 0) {
                    lis.forEach(li => {
                        //debugger
                        let time = li.querySelector('.is_show_create_time');
                        if (!time) {
                            return
                        }
                        time = time.textContent;
                        let tid = li.dataset.tid;


                        if (tid !== null || tid !== 'null') {
                            let tar = document.querySelector('li[data-tid="' + tid + '"] .ti_author_icons');
                            if (!tar) {
                                return;
                            }

                            let d = document.createElement('span');
                            d.style.marginLeft = '1rem';
                            d.innerHTML = `<span style="color: #9999b3">${time}</span>`;
                            tar.appendChild(d)
                        }

                    })
                }
            },

        });
    }

    function list() {
        [
            '.frs_daoliu_for_app', '.tl_shadow_for_app_modle', '.footer_logo', '.footer_link_highlight'
        ].forEach(value => {
            var x = document.querySelector(value);
            if (x) {
                x.parentNode.removeChild(x)
            }
        });
        var ads = document.querySelectorAll('li.tl_shadow_for_app');
        if (ads.length > 0) {
            var url = document.querySelector('.tl_shadow_for_app').parentNode.querySelector('a.j_common').href;
            ads.forEach(v => {
                //v.classList.remove('tl_shadow_for_app');
                var a = v.querySelector('a.j_enter_for_app');
                var tid = v.getAttribute('data-tid');
                a.href = url.replace(/\/(\d+)\?/.exec(url)[1], tid);
                a.classList.remove('tl_shadow_for_app');
            })
        }
        createTime();


        let list = document.querySelector('#tlist');
        let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
        let observer = new MutationObserver((mutations) => {
            if (mutations.length > 0) {
                createTime();
            }
        });

        observer.observe(list, {
            attributes: true,
            childList: true,
            characterData: true
        });

    }

    function detail() {
        document.querySelectorAll('ul#pblist>li').forEach(value => {
            if (value.classList.contains('class_hide_flag')) {
                value.classList.remove('class_hide_flag');
            }

        });
        t();

        [
            '.img_desc', '.father-cut-recommend-normal-box', '.father-cut-daoliu-normal-box',
            '#diversBanner', '.footer_logo', '.j_footer_link', '.frs_daoliu_for_app', '.j_videoFootDownBtn'
        ].forEach(value => {
            var x = document.querySelector(value);
            if (x) {
                x.parentNode.removeChild(x)
            }
        });


        document.querySelector('.father-cut-pager-class-no-page').classList.remove('father-cut-pager-class-no-page');


        let list = document.querySelector('ul#pblist');
        let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
        let observer = new MutationObserver((mutations) => {
            if (mutations.length > 0) {
                t();
            }
        });

        observer.observe(list, {
            attributes: true,
            childList: true,
            characterData: true
        });
    }

    if (check()) {
        var url = location.href;

       if (document.querySelector('#pblist')) {
            detail();
        }
        if (document.querySelector('#tlist')) {
            list();
        }
    }

})();