// ==UserScript== // @name 手机百度贴吧自动展开楼层 // @namespace http://tampermonkey.net/ // @homepage https://greasyfork.org/scripts/445657 // @version 4.2 // @description 有时候用手机的浏览器打开百度贴吧,只想看一眼就走,并不想打开APP,这个脚本用于帮助用户自动展开楼层。注意:只支持手机浏览器,测试环境为Iceraven+Tampermonkey // @author voeoc // @icon https://tieba.baidu.com/favicon.ico // @match https://tieba.baidu.com/* // @match https://jump2.bdimg.com/* // @match https://tiebac.baidu.com/* // @exclude /^https?:\/\/[\.a-z]*\/(index|f\?.*kw=)/ // @exclude /^https?:\/\/[.a-z]*\/(index|f?.*kw=)/ // @connect tieba.baidu.com // @connect jump2.bdimg.com // @connect tiebac.baidu.com // @grant unsafeWindow // @grant GM_addStyle // @grant GM_xmlhttpRequest // @grant GM_getValue // @grant GM_setValue // @grant GM_listValues // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant GM_getResourceText // @run-at document-start // @noframes // @license MIT // @downloadURL none // ==/UserScript== (function () { 'use strict'; function __classPrivateFieldGet(receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); } function __classPrivateFieldSet(receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; } var _Stack_items, _ElementSearcher_options; class Stack { constructor() { _Stack_items.set(this, void 0); __classPrivateFieldSet(this, _Stack_items, [], "f"); } push(element) { __classPrivateFieldGet(this, _Stack_items, "f").push(element); } pop() { return __classPrivateFieldGet(this, _Stack_items, "f").pop(); } peek() { return __classPrivateFieldGet(this, _Stack_items, "f")[__classPrivateFieldGet(this, _Stack_items, "f").length - 1]; } isEmpty() { return __classPrivateFieldGet(this, _Stack_items, "f").length === 0; } size() { return __classPrivateFieldGet(this, _Stack_items, "f").length; } clear() { __classPrivateFieldSet(this, _Stack_items, [], "f"); } } _Stack_items = new WeakMap(); function ASSERT(condition, msg) { if (!condition) { throw new Error(`${msg}`); } } function AUTO_CATCH_ERROR(func, msg, dirObj) { try { func(); } catch (e) { if (msg) { void 0; } else { void 0; } if (dirObj) { void 0; } } } class ElementSearcher { constructor(options) { _ElementSearcher_options.set(this, void 0); __classPrivateFieldSet(this, _ElementSearcher_options, options, "f"); } search(selector) { return ElementSearcher.SEARCH(selector, __classPrivateFieldGet(this, _ElementSearcher_options, "f")); } static SEARCH(selector, options) { const realOptions = Object.assign(Object.assign({}, ElementSearcher.DEFAULT_OPTIONS), options); if (!selector) { selector = realOptions.selector; } return new Promise((resolve, reject) => { let findTimeNum = 0; let timer = setInterval(() => { let element = realOptions.searchFunc(selector); if (element) { clearInterval(timer); resolve(element); return; } findTimeNum++; if (realOptions.searchNum < findTimeNum) { clearInterval(timer); reject(new Error(`search time out: ${selector}=${element}`)); } }, realOptions.interval); }); } } _ElementSearcher_options = new WeakMap(); ElementSearcher.DEFAULT_OPTIONS = { selector: "", searchNum: 100, searchFunc: document.querySelector.bind(document), interval: 50 }; ElementSearcher.CommonSearcher = new ElementSearcher(); function WAIT_DOCUMENT_READY(documentNode = document) { return new Promise((resolve, reject) => { document.addEventListener("readystatechange", function onDocumentReady(event) { if (documentNode.readyState === "complete") { document.removeEventListener("readystatechange", onDocumentReady); resolve(true); } }); }); } function MATCH_REG(regExp, str) { let regExpMatchArray = regExp.exec(str); if (regExpMatchArray && regExpMatchArray.length > 1) { return regExpMatchArray[1].trim(); } return undefined; } class IframeUtils { constructor(parentNode, id) { this.iframeNode = parentNode.ownerDocument.createElement("iframe"); this.iframeNode.id = id; this.parentNode = parentNode; this.parentNode.appendChild(this.iframeNode); } get contentDocument() { return this.iframeNode.contentDocument; } get contentWindow() { return this.iframeNode.contentWindow; } get ownerDocument() { return this.iframeNode.ownerDocument; } writeDoc(data) { try { this.contentDocument.open().write(data); } finally { this.contentDocument.close(); } } } function GET_INPUT_ELEMENT_VALUE(inputElement) { switch (inputElement.type) { case "checkbox": return inputElement.checked; case "range": return Number(inputElement.value); default: return inputElement.value; } } function SET_INPUT_ELEMENT_VALUE(inputElement, value) { switch (inputElement.type) { case "checkbox": inputElement.checked = value; break; default: inputElement.value = String(value); break; } } function STOPPROPAGATION(event) { if (event.stopPropagation) { event.stopPropagation(); } else { event.cancelBubble = true; } return false; } function GET_URL_ATTR(url, attrName) { return MATCH_REG(RegExp(`${attrName}=([^&]*)&?`, 'i'), url); } var _MainPage_instances, _MainPage_tryCollectSomeKey; const CommonSearcher = ElementSearcher.CommonSearcher; const STR_VOEOCMARK = "VOEOCMARK"; const REGEXP_PBDATA = RegExp(`getPbData\?.*pn\=.*`, 'i'); const REGEXP_POSTPAGE = RegExp(`postPage\?(?=.*tid\=)(?=.*postAuthorId\=)(?=.*forumId\=)`, 'i'); const REGEXP_LZLPAGE = RegExp(`lzlPage\?(?=.*floor\=)(?=.*pid\=)`, 'i'); var PAGE_TYPE; (function (PAGE_TYPE) { PAGE_TYPE[PAGE_TYPE["UNKNOW"] = 0] = "UNKNOW"; PAGE_TYPE[PAGE_TYPE["MAINPAGE"] = 1] = "MAINPAGE"; PAGE_TYPE[PAGE_TYPE["POSTPAGE"] = 2] = "POSTPAGE"; PAGE_TYPE[PAGE_TYPE["LZLPAGE"] = 3] = "LZLPAGE"; PAGE_TYPE[PAGE_TYPE["TIEBAPAGE"] = 4] = "TIEBAPAGE"; })(PAGE_TYPE || (PAGE_TYPE = {})); function GET_PAGE_TYPE() { let isTiePage = false; if (RegExp(`^/f`).test(window.location.pathname)) { if (GET_URL_ATTR(window.location.href, "kw")) { return PAGE_TYPE.TIEBAPAGE; } else if (GET_URL_ATTR(window.location.href, "kz")) { isTiePage = true; } } else if (RegExp(`^/p`).test(window.location.pathname)) { isTiePage = true; } if (isTiePage) { let hash = window.location.hash; if (hash === "" || hash === "#/") { return PAGE_TYPE.MAINPAGE; } else if (REGEXP_POSTPAGE.test(hash)) { return PAGE_TYPE.POSTPAGE; } else if (REGEXP_LZLPAGE.test(hash)) { return PAGE_TYPE.LZLPAGE; } } return PAGE_TYPE.UNKNOW; } class MainPage { constructor() { _MainPage_instances.add(this); this.currentHash = window.location.hash; this.currentScrollYPos = NaN; this.storagePageData = { tieNode: undefined, tiebaNameNode: undefined, lzId: "", floorDataList: new Map(), someKey: { host: "tieba.baidu_mobile_expand.com", tid: "", postAuthorId: "", forumId: "", source: "", } }; const thisClass = this; const oldXHR = unsafeWindow["XMLHttpRequest"]; unsafeWindow["XMLHttpRequest"] = function () { const realXHR = new oldXHR(); realXHR.addEventListener('readystatechange', () => { if (REGEXP_PBDATA.test(realXHR.responseURL) && realXHR.response !== "") { thisClass.parsePbData(realXHR.response, realXHR.responseURL); } }, false); return realXHR; }; unsafeWindow.onscroll = () => { this.checkUrlHashChange(); if (GET_PAGE_TYPE() === PAGE_TYPE.POSTPAGE) { if (window.scrollY !== 0) { this.currentScrollYPos = window.scrollY; } } }; unsafeWindow.onhashchange = () => { this.checkUrlHashChange(); }; } listenReady(callback) { WAIT_DOCUMENT_READY().then(callback); } static REPLACE_HASH(hash) { window.location.replace(MainPage.GET_MAIN_URL() + hash); } static GET_MAIN_URL() { return window.location.href.replace(window.location.hash, ""); } tryGetFirstFloorAndGoPostPage() { __classPrivateFieldGet(this, _MainPage_instances, "m", _MainPage_tryCollectSomeKey).call(this); let url = `${window.origin}/mg/p/getPbData?kz=${this.storagePageData.someKey.tid}&obj_param2=firefox&format=json&eqid=&refer=&pn=1&rn=5`; GM_xmlhttpRequest({ method: "GET", url: url, onload: (details) => { this.parsePbData(details.responseText, url); }, onerror: () => { }, }); const pbdataSearcher = new ElementSearcher({ selector: "pbdata", searchNum: 100, searchFunc: () => { if (this.storagePageData.someKey.tid && this.storagePageData.someKey.postAuthorId && this.storagePageData.someKey.forumId) { return true; } return undefined; } }); CommonSearcher.search("div.nav-bar-v2-fixed:nth-child(1)").then(() => { this.storagePageData.tiebaNameNode = document.querySelector(".forum-block"); this.storagePageData.tieNode = document.querySelector(".main-thread-content"); let postbtn = document.querySelector(".post-page-entry-btn"); if (postbtn) { pbdataSearcher.search().then(() => { MainPage.REPLACE_HASH(`#/postPage?tid=${this.storagePageData.someKey.tid}&postAuthorId=${this.storagePageData.someKey.postAuthorId}&forumId=${this.storagePageData.someKey.forumId}&locateConfig=%5B%5D&source=`); }).catch(() => { if (postbtn) { postbtn.click(); this.checkUrlHashChange(false, PAGE_TYPE.MAINPAGE); } }); } else { pbdataSearcher.search().then(() => { this.checkUrlHashChange(true); }); } }); } listenFloorChange(floorParentNode) { const searchAndUpdatePostPage = () => { let floorNodeList = floorParentNode.querySelectorAll(`div.post-item:not(.${STR_VOEOCMARK})`); floorNodeList.forEach((value) => { this.onNewFloorAdded.call(this, value); }); }; let observer = new MutationObserver((_mutationList) => { searchAndUpdatePostPage(); }); observer.observe(floorParentNode, { attributes: false, childList: true, characterData: false, subtree: false, }); searchAndUpdatePostPage(); } checkUrlHashChange(force = false, lastPageType) { var _a; if (this.currentHash !== window.location.hash) { this.currentHash = window.location.hash; } else if (!force) { return false; } let pageType = GET_PAGE_TYPE(); if (pageType === PAGE_TYPE.POSTPAGE) { if (lastPageType === PAGE_TYPE.MAINPAGE) ; __classPrivateFieldGet(this, _MainPage_instances, "m", _MainPage_tryCollectSomeKey).call(this); CommonSearcher.search(".post-page-list").then((postpagelist) => { this.listenFloorChange(postpagelist); }); this.restorePostPage(); MainPage.scrollTo(this.currentScrollYPos); } else if (pageType === PAGE_TYPE.MAINPAGE) { if (this.storagePageData.tieNode) { CommonSearcher.search(".pb-page-wrapper").then((pbpageNode) => { this.listenFloorChange(pbpageNode); }); CommonSearcher.search("#replySwitch").then((splitlineNode) => { AUTO_CATCH_ERROR(() => { if (!this.storagePageData.tieNode) { throw new Error("1楼不存在"); } if (!splitlineNode.parentNode) { throw new Error("复原1楼时页面加载失败"); } splitlineNode.parentNode.insertBefore(this.storagePageData.tieNode, splitlineNode); }); }); MainPage.scrollTo(0); } } (_a = this.onEnterFinalPage) === null || _a === void 0 ? void 0 : _a.call(this); return true; } static scrollTo(yPos, documentNode = document) { CommonSearcher.search(".post-page").then(() => { documentNode.documentElement.scrollTop = yPos; setTimeout(() => { documentNode.documentElement.scrollTop = yPos; }, 200); }); } restorePostPage() { CommonSearcher.search(".text").then((titletextNode) => { var _a; (_a = this.onEnterFinalPage) === null || _a === void 0 ? void 0 : _a.call(this); AUTO_CATCH_ERROR(() => { if (!this.storagePageData.tiebaNameNode || !titletextNode.parentNode) { throw new Error("贴吧名无法加载"); } let tiebaNameCloneNode = this.storagePageData.tiebaNameNode.cloneNode(true); titletextNode.parentNode.replaceChild(tiebaNameCloneNode, titletextNode); tiebaNameCloneNode.onclick = this.storagePageData.tiebaNameNode.click.bind(this.storagePageData.tiebaNameNode); }); AUTO_CATCH_ERROR(() => { if (!this.storagePageData.tieNode) { throw new Error("1楼内容为空!"); } this.storagePageData.tieNode.style.cssText = `margin-left:0.12rem;margin-right:0.12rem;margin-bottom:0.25rem;`; CommonSearcher.search(".avatar").then(lzavatarNode => { if (lzavatarNode) { lzavatarNode.style.backgroundImage = `url("${lzavatarNode.getAttribute("data-src")}")`; } else { } }); let textContentNode = (this.storagePageData.tieNode.querySelector(".thread-text")); if (textContentNode) { textContentNode.style.cssText = `margin-top:0.18rem;font-size:0.16rem;line-height:0.28rem;`; } else { } let replySwitchNode = document.querySelector("#replySwitch"); if (replySwitchNode && replySwitchNode.parentNode) { replySwitchNode.parentNode.insertBefore(this.storagePageData.tieNode, replySwitchNode); } else { } }); AUTO_CATCH_ERROR(() => { let threadtitleNode = document.querySelector(".thread-title"); if (!threadtitleNode) { threadtitleNode = document.querySelector(".bottom-thread-title"); } if (!threadtitleNode) { throw new Error("标题为空!"); } threadtitleNode.style.cssText = `margin-bottom: 0.13rem;font-size:0.22rem;font-weight:700;line-height:0.33rem;`; }); }); } parsePbData(responseText, responseURL) { AUTO_CATCH_ERROR(() => { var _a, _b; let data; if (typeof responseText == "object") { data = responseText.data; } else { data = JSON.parse(responseText).data; } ASSERT(data, "无法获取PBDATA"); if ((_a = data === null || data === void 0 ? void 0 : data.forum) === null || _a === void 0 ? void 0 : _a.id) { this.storagePageData.someKey.forumId = data.forum.id; } if ((_b = data === null || data === void 0 ? void 0 : data.thread) === null || _b === void 0 ? void 0 : _b.id) { this.storagePageData.someKey.tid = data.thread.id; } AUTO_CATCH_ERROR(() => { let post_list = data.post_list; if (!post_list) { throw new Error(`该PBDATA没有post_list数据, ${data}`); } AUTO_CATCH_ERROR(() => { if (GET_URL_ATTR(responseURL, "pn") === "1") { this.storagePageData.lzId = post_list[0].author.id; this.storagePageData.someKey.postAuthorId = this.storagePageData.lzId; } }, "无法获取楼主id", post_list); for (let i = 0; i < post_list.length; i++) { let d = post_list[i]; this.storagePageData.floorDataList.set(d.floor.toString(), d); } }); }); } } _MainPage_instances = new WeakSet(), _MainPage_tryCollectSomeKey = function _MainPage_tryCollectSomeKey() { this.storagePageData.someKey.host = window.location.hostname; let tid; let postAuthorId; let forumId; let source; if ((tid = GET_URL_ATTR(this.currentHash, "tid")) || ((tid = window.location.pathname.replace("/p/", "")) && !isNaN(tid))) { this.storagePageData.someKey.tid = tid; } if ((postAuthorId = GET_URL_ATTR(this.currentHash, "postAuthorId")) !== undefined) { this.storagePageData.someKey.postAuthorId = postAuthorId; } if ((forumId = GET_URL_ATTR(this.currentHash, "forumId")) !== undefined) { this.storagePageData.someKey.forumId = forumId; } if ((source = GET_URL_ATTR(this.currentHash, "source")) !== undefined) { this.storagePageData.someKey.source = source; } }; var CSS_BAIDU = ":root {\r\n --str_lzl_page_transition_duration: 0.2s;\r\n}\r\n.open-app-text-real {\r\n display: block !important;\r\n -webkit-box-flex: 0;\r\n -webkit-flex: none;\r\n -ms-flex: none;\r\n flex: none;\r\n font-size: .13rem;\r\n color: #614ec2;\r\n}\r\n.open-app-text-real.error {\r\n color: #ff3366 !important;\r\n text-decoration: line-through;\r\n}\r\n\r\n@keyframes rotate3d {\r\n 0%{-webkit-transform:rotate3d(1, 0, 0, 0deg);}\r\n 25%{-webkit-transform:rotate3d(1, 0, 0, 90deg);}\r\n 50%{-webkit-transform:rotate3d(1, 0, 0, 180deg);}\r\n 75%{-webkit-transform:rotate3d(1, 0, 0, 270deg);}\r\n 100%{-webkit-transform:rotate3d(1, 0, 0, 360deg);}\r\n}\r\n.open-app-guide.loading {\r\n animation: rotate3d 0.5s linear infinite;\r\n pointer-events: none;\r\n}\r\n.VOEOC_DIALOG {\r\n position: fixed;\r\n z-index: 999;\r\n height: 0;\r\n visibility: hidden;\r\n transition: visibility var(--str_lzl_page_transition_duration);\r\n}\r\n.VOEOC_DIALOG.VOEOC_DIALOG_SHOW {\r\n display: block;\r\n height: 100%;\r\n visibility: visible;\r\n}\r\n#VOEOC-ID-LZLPAGE {\r\n overscroll-behavior: none;\r\n width: 100%;\r\n overflow: scroll;\r\n}\r\n#VOEOC-ID-LZLPAGE::-webkit-scrollbar { width: 0 !important }\r\n#VOEOC-ID-LZLPAGEBACKGROUND {\r\n position: fixed;\r\n width: 100%;\r\n height: 200%;\r\n background-color: #00000077;\r\n z-index: 1;\r\n opacity: 0;\r\n transition: opacity var(--str_lzl_page_transition_duration);\r\n}\r\n.VOEOC-CLASSNAME-LZLPAGEIFRAME {\r\n position: fixed;\r\n width: 100%;\r\n height: 0%;\r\n background-color: #ffffff;\r\n bottom: 0;\r\n transition: height var(--str_lzl_page_transition_duration);\r\n overflow: hidden;\r\n}\r\n#VOEOC-ID-LZLPAGE.loading, #VOEOC-ID-LZLPAGE.loading > .VOEOC-CLASSNAME-LZLPAGEIFRAME {\r\n pointer-events: none;\r\n}\r\n#VOEOC-ID-LZLPAGE.VOEOC_DIALOG_SHOW > .VOEOC-CLASSNAME-LZLPAGEIFRAME.VOEOC_DIALOG_SHOW {\r\n height: 80%;\r\n}\r\n#VOEOC-ID-LZLPAGE.VOEOC_DIALOG_SHOW > #VOEOC-ID-LZLPAGEBACKGROUND {\r\n opacity: 1;\r\n}\r\n#VOEOC-ID-LZLPAGE.VOEOC_DIALOG_SHOW > .lzl-reload-btn {\r\n margin-left: 0.1rem;\r\n}\r\n.lzl-nav-btn.lzl-reload-btn {\r\n position:fixed;\r\n top:20%;\r\n margin: 0.1rem 0.1rem 0.1rem -0.5rem;\r\n transition: margin-left var(--str_lzl_page_transition_duration);\r\n}\r\n.lzl-nav-btn {\r\n width: .32rem;\r\n height: .32rem;\r\n display: flex;\r\n background-color:#d0d0d04a;\r\n border-radius:50%;\r\n cursor: pointer;\r\n}\r\n.lzl-nav-btn > *{\r\n position: relative;\r\n top: 50%;\r\n left: 50%;\r\n transform: translate(-50%, -50%);\r\n height: fit-content;\r\n width: fit-content;\r\n}\r\n@keyframes rotate {\r\n 0%{-webkit-transform:rotate(0deg);}\r\n 25%{-webkit-transform:rotate(90deg);}\r\n 50%{-webkit-transform:rotate(180deg);}\r\n 75%{-webkit-transform:rotate(270deg);}\r\n 100%{-webkit-transform:rotate(360deg);}\r\n}\r\n#VOEOC-ID-LZLPAGE.loading > .lzl-reload-btn {\r\n animation: rotate 0.2s linear infinite;\r\n pointer-events: none;\r\n }\r\n#VOEOC-ID-LZLPAGE.error > .lzl-reload-btn {\r\n background-color: #ff000070 !important;\r\n }\r\n.slide-down-btn{\r\n position: fixed;\r\n top: 20%;\r\n left: 50%;\r\n margin-left: -0.32rem;\r\n opacity: 0;\r\n transition: opacity,margin-top,background-color 0.1s;\r\n}\r\n.slide-down-btn.confirm{\r\n background-color: #0000004a;\r\n}\r\n#VOEOC-ID-SETTING-FRAME {\r\n height: 100%;\r\n width: 100%;\r\n position: fixed;\r\n z-index: 9999;\r\n display: block;\r\n left: 0;\r\n top: 0;\r\n overscroll-behavior: none;\r\n background-color: white;\r\n}\r\n\r\n.VOEOC_DIALOG_BACKGROUND {\r\n position: fixed;\r\n width: 100%;\r\n height: 101%;\r\n left: 0;\r\n top: 0;\r\n z-index: 9999;\r\n overflow: scroll;\r\n overscroll-behavior: none;\r\n background-color: #00000077;\r\n transition: opacity var(--str_lzl_page_transition_duration);\r\n}\r\n.VOEOC_DIALOG_BACKGROUND::before {\r\n content: \" \";\r\n width: 100%;\r\n height: 101%;\r\n display: block;\r\n overscroll-behavior: none;\r\n}\r\n.VOEOC_DIALOG_BACKGROUND::-webkit-scrollbar,\r\n.VOEOC_DIALOG_BACKGROUND::before::-webkit-scrollbar {\r\n width: 0 !important;\r\n}"; var CSS_REMOVE = ".comment-box, .only-lz, .nav-bar-bottom, .open-app, .more-image-desc {\r\n display: none !important;\r\n}\r\n.logo-wrapper {\r\n visibility: hidden !important;\r\n pointer-events: none !important;\r\n height: 0;\r\n}\r\n.open-app-text {\r\n display: none !important;\r\n}"; var HTML_SETTINGS_DIALOG = "