Greasy Fork

Greasy Fork is available in English.

手机贴吧自动展开评论区

有时候用手机浏览器打开手机贴吧,只想看一眼就走,并不想打开APP,这个脚本用于帮助用户自动展开评论区。注意:只支持手机浏览器,测试环境为Iceraven+Tampermonkey

目前为 2022-05-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         手机贴吧自动展开评论区
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  有时候用手机浏览器打开手机贴吧,只想看一眼就走,并不想打开APP,这个脚本用于帮助用户自动展开评论区。注意:只支持手机浏览器,测试环境为Iceraven+Tampermonkey
// @author       voeoc
// @match        https://tieba.baidu.com/p/*
// @icon         https://tieba.baidu.com/favicon.ico
// @grant        unsafeWindow
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function DEBUGLOG(msg, label="") {
        console.log(`voeoc(DEBUG)<${label}>: ${msg}`)
    }

    function waitElementLoaded(selector, func) {
        const TIME_OUT = 100; // 找100次没有找到就放弃
        let findTimeNum = 0; // 记录查找的次数
        let timer = setInterval(() => {
            let element = document.querySelector(selector);
            DEBUGLOG(`waitElementLoaded: ${selector}=${element}`);
            if (element != null) {
                // 清除定时器
                clearInterval(timer);
                func(element);
            } else {
                findTimeNum++;
                if (TIME_OUT < findTimeNum) {
                    // 清除定时器
                    clearInterval(timer);
                }
            }
        }, 200);
    }

    function findAndHide(selector) {
        try{
            document.querySelector(selector).style.display = "none";
        }catch(e){
            console.error(e);
        }
    }

    function loader() {
        waitElementLoaded(".post-page-entry-btn", (postbtn) => {

            let tiebaName = document.querySelector(".forum-block"); // 吧名

            let tie = document.querySelector(".main-thread-content"); // 楼主发帖

            DEBUGLOG(postbtn);
            postbtn.click();

            waitElementLoaded(".text", (titletext) => { // 等待标题位置加载
                let postpage = document.querySelector(".post-page"); // 评论页面

                let navfixed = document.querySelector("div.nav-bar-v2-fixed:nth-child(2)"); // 标题下方的分割

                // 显示贴吧名
                let tiebaNameclone = tiebaName.cloneNode(true)
                tiebaNameclone.onclick = function () {
                    tiebaName.click();
                }
                titletext.parentNode.replaceChild(tiebaNameclone, titletext);

                // 显示楼主发帖层
                let tieclone = tie.cloneNode(true)
                tieclone.style.marginLeft = "0.12rem"
                tieclone.style.marginRight = "0.12rem"
                tieclone.style.marginBottom = "0.25rem"
                postpage.insertBefore(tieclone, navfixed)

                // 优化标题显示
                waitElementLoaded(".thread-title", (threadtitle) => {
                    let userline = document.querySelector("div.user-line-wrapper:nth-child(3)"); // 用户昵称行
                    threadtitle.style.opacity = "0"
                    threadtitle.style.marginBottom = "0.15rem"

                    let threadtitleclone = threadtitle.cloneNode(true)
                    threadtitleclone.style.position = "fixed"
                    threadtitleclone.style.zIndex = "99"
                    threadtitleclone.style.opacity = "0.8"
                    threadtitleclone.style.backgroundColor = "#FFFFFF"
                    threadtitle.parentNode.insertBefore(threadtitleclone, threadtitle)
                })

                // 删减打开APP的诱导按钮
                findAndHide(".comment-box");
                findAndHide(".only-lz");
                findAndHide("div.w-row:nth-child(2)");
                findAndHide(".nav-bar-bottom");
            })
        })
    }


    (function main(){
        let url = unsafeWindow.location.href
        url = url.split("/")
        url = url[url.length-1]
        // 简单判断当前页面是否为评论页
        let ispostpage = RegExp(`postPage\?.*postAuthorId=.*`,'i').test(url)
        DEBUGLOG(url, "url");
        DEBUGLOG(ispostpage, "检测是否为评论页");
        if(ispostpage) {
            waitElementLoaded(".logo-wrapper", (backbtn) => {
                // 点击后退按钮
                backbtn.click();
                // 进入主函数
                loader()
            })
        } else {
            // 进入主函数
            loader()
        }
    })()
})();