// ==UserScript== // @name 手机百度贴吧自动展开楼层 // @namespace http://tampermonkey.net/ // @version 0.4 // @description 有时候用手机的浏览器打开百度贴吧,只想看一眼就走,并不想打开APP,这个脚本用于帮助用户自动展开楼层。注意:只支持手机浏览器,测试环境为Iceraven+Tampermonkey // @author voeoc // @match https://tieba.baidu.com/p/* // @match https://jump2.bdimg.com/p/* // @match https://tiebac.baidu.com/p/* // @icon https://tieba.baidu.com/favicon.ico // @grant unsafeWindow // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; function DEBUGLOG(msg, label="") { console.log(`voeoc(DEBUG)<${label}>: ${msg}`) } function waitElementLoaded(selector, func) { const TIME_OUT = 30; // 找n次没有找到就放弃 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(`尝试隐藏元素${selector}时报错,${e}`); } } function loader() { waitElementLoaded(".post-page-entry-btn", (postbtn) => { let tiebaName = document.querySelector(".forum-block"); // 吧名 let tie = document.querySelector(".main-thread-content"); // 楼主发帖内容 try{ }catch(e){console.error(e);} // 点击展开按钮 postbtn.click(); waitElementLoaded(".text", (titletext) => { // 等待标题位置加载 // 显示贴吧名 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" let postpage = document.querySelector(".post-page"); // 评论页面 let navfixed = document.querySelector("div.nav-bar-v2-fixed:nth-child(2)"); // 标题下方的分割 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.13rem" threadtitle.style.fontSize = "0.22rem" threadtitle.style.fontWeight = "700" threadtitle.style.lineHeight = "0.33rem" 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) }) // 尝试找回楼主丢失的头像 waitElementLoaded("div.user-line-wrapper:nth-child(3) > div:nth-child(1) > div:nth-child(1)", (lzavatar) => { lzavatar.style.backgroundImage = `url("${lzavatar.getAttribute("data-src")}")` }) // 尝试复原发帖内容的字体 waitElementLoaded("p.thread-text:nth-child(4)", (textContent) => { textContent.style.marginTop = ".18rem" textContent.style.fontSize = ".16rem" textContent.style.lineHeight = ".28rem" }) // 删减打开APP的诱导按钮 findAndHide(".comment-box"); findAndHide(".only-lz"); findAndHide(".nav-bar-bottom"); findAndHide(".open-app"); findAndHide("div.w-row:nth-child(2)"); }) }) } (function main(){ let url = unsafeWindow.location.href let lastSplitIndex = url.lastIndexOf("\/"); // 简单判断当前页面是否为评论页 let ispostpage = RegExp(`postPage\?.*postAuthorId=.*`,'i').test(url.substring(lastSplitIndex+1,url.length)) DEBUGLOG(url, "url"); DEBUGLOG(ispostpage, "检测是否为评论页"); if(ispostpage) { // 需要先打开主页面 unsafeWindow.open(url.substring(0,lastSplitIndex+1), "_self") } loader() })() })();