// ==UserScript== // @name 手机百度贴吧自动展开楼层 // @namespace http://tampermonkey.net/ // @version 0.6 // @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, TIME_OUT=30) { //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{ let element = document.querySelector(selector) element.style.display = "none"; element.style.visibility = "hidden"; }catch(e){ console.error(`尝试隐藏元素${selector}时报错,${e}`); } } function loader() { waitElementLoaded(".post-page-entry-btn", (postbtn) => { let tiebaName = document.querySelector(".forum-block"); // 吧名 if(!tiebaName){ tiebaName = document.createElement("div"); } let tie = document.querySelector(".main-thread-content"); // 楼主发帖内容 if(!tie){ tie = document.createElement("div"); } 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" try{ let postpage = document.querySelector(".post-page"); // 评论页面 let navfixed = document.querySelector("div.nav-bar-v2-fixed:nth-child(2)"); // 标题下方的分割 postpage.insertBefore(tieclone, navfixed) }catch(e){console.error(e);} // 尝试优化标题显示 waitElementLoaded(".thread-title", (threadtitle) => { // 将标题置顶显示 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) }, 10) // 尝试找回楼主丢失的头像 function repairAvatar(lzavatar){ lzavatar.style.backgroundImage = `url("${lzavatar.getAttribute("data-src")}")` } waitElementLoaded("div.user-line-wrapper:nth-child(3) > div:nth-child(1) > div:nth-child(1)", repairAvatar, 5) waitElementLoaded(".thread-user-line > div:nth-child(1) > div:nth-child(1)", repairAvatar, 5) // 尝试复原发帖内容的字体 function repairTextContent(textContent){ textContent.style.marginTop = ".18rem" textContent.style.fontSize = ".16rem" textContent.style.lineHeight = ".28rem" } waitElementLoaded("p.thread-text:nth-child(4)", repairTextContent, 5) waitElementLoaded(".main-thread-content-margin > p:nth-child(2)", repairTextContent, 5) // 删减打开APP的诱导按钮 findAndHide(".comment-box"); findAndHide(".only-lz"); findAndHide(".nav-bar-bottom"); findAndHide(".open-app"); findAndHide(".more-image-desc"); }) }) } (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() })() })();