// ==UserScript== // @name 【手机】去除百度跳转及去推广,标识csdn下载 // @namespace http://tampermonkey.net/ // @version 0.4.7 // @description 由于手机搜索东西搜到cdsn下载的网站,特意标识一下防止误进恶心人,自动加载搜索结果的下一页(默认关闭),加载百度贴吧的所有评论 // @author MT-戒酒的李白染 // @include *://m.baidu.com/* // @include *://www.baidu.com/* // @include *://baijiahao.baidu.com/* // @include *://tieba.baidu.com/* // @include *://wk.baidu.com/* // @include *://jingyan.baidu.com/* // @include *://baike.baidu.com/* // @include *://zhidao.baidu.com/* // @connect www.baidu.com // @connect m.baidu.com // @grant GM_addStyle // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant GM_getValue // @grant GM_setValue // @grant GM_deleteValue // @grant GM_listValues // @grant GM_xmlhttpRequest // @require http://cdn.staticfile.org/jquery/2.1.4/jquery.min.js // @run-at document-start // @downloadURL none // ==/UserScript== (function () { 'use strict'; var flagTitleCSS = ` .component-box .praise { padding-right: 20px; background: #ff5722; text-indent: 1em; border-top-left-radius: 50px; border-top-right-radius: 50px; border-bottom-left-radius: 50px; border-bottom-right-radius: 50px; background: -webkit-linear-gradient(left,#ff5722,#f78d6b); background: -o-linear-gradient(right,#ff5722,#f78d6b); background: -moz-linear-gradient(right,#ff5722,#f78d6b); background: linear-gradient(to right,#ff5722,#f78d6b); } .component-box .praise,.component-box .share { /*width: 110px; height: 34px; line-height: 34px;*/ height:auto;line-height:normal;color: #fff; } .component-box a { display: inline-block; font-size: 14px; } .component-box { /*margin: 0 auto; text-align: center; display: inline;*/ display: flex; margin: 0; text-align: left; font-size: 0; position: relative; width: 260px; } ` const isLoadingCSS = ` #whitesev-isloading-outside, #whitesev-isloading-within{ position: absolute; margin-left: 140px; } #whitesev-isloading-outside { background-color: rgba(0, 0, 0, 0); border: 5px solid rgba(0, 183, 229, 0.9); opacity: .9; border-right: 5px solid rgba(0, 0, 0, 0); border-left: 5px solid rgba(0, 0, 0, 0); border-radius: 50px; box-shadow: 0 0 35px #2187e7; width: 20px; height: 20px; margin: 0 auto; /*position: fixed; left: 50%; top: 50%; margin-left: -25px; margin-top: -25px;*/ -moz-animation: spinPulse 1s infinite ease-in-out; -webkit-animation: spinPulse 1s infinite ease-in-out; -o-animation: spinPulse 1s infinite ease-in-out; -ms-animation: spinPulse 1s infinite ease-in-out } #whitesev-isloading-within { background: rgba(0, 0, 0, 0) no-repeat center center; border: 5px solid rgba(0, 183, 229, 0.9); opacity: .9; border-top: 5px solid rgba(0, 0, 0, 0); border-bottom: 5px solid rgba(0, 0, 0, 0); border-radius: 50px; box-shadow: 0 0 15px #2187e7; width: 20px; height: 20px; margin: 0 auto; /*position: fixed; left: 50%; top: 50%; margin-left: -15px; margin-top: -15px;*/ -moz-animation: spinoffPulse 3s infinite linear; -webkit-animation: spinoffPulse 3s infinite linear; -o-animation: spinoffPulse 3s infinite linear; -ms-animation: spinoffPulse 3s infinite linear } @-moz-keyframes spinPulse { 0% { -moz-transform: rotate(160deg); opacity: 0; box-shadow: 0 0 1px #505050 } 50% { -moz-transform: rotate(145deg); opacity: 1 } 100% { -moz-transform: rotate(-320deg); opacity: 0 } } @-moz-keyframes spinoffPulse { 0% { -moz-transform: rotate(0deg) } 100% { -moz-transform: rotate(360deg) } } @-webkit-keyframes spinPulse { 0% { -webkit-transform: rotate(160deg); opacity: 0; box-shadow: 0 0 1px #505050 } 50% { -webkit-transform: rotate(145deg); opacity: 1 } 100% { -webkit-transform: rotate(-320deg); opacity: 0 } } @-webkit-keyframes spinoffPulse { 0% { -webkit-transform: rotate(0deg) } 100% { -webkit-transform: rotate(360deg) } } @-o-keyframes spinPulse { 0% { -o-transform: rotate(160deg); opacity: 0; box-shadow: 0 0 1px #505050 } 50% { -o-transform: rotate(145deg); opacity: 1 } 100% { -o-transform: rotate(-320deg); opacity: 0 } } @-o-keyframes spinoffPulse { 0% { -o-transform: rotate(0deg) } 100% { -o-transform: rotate(360deg) } } @-ms-keyframes spinPulse { 0% { -ms-transform: rotate(160deg); opacity: 0; box-shadow: 0 0 1px #505050 } 50% { -ms-transform: rotate(145deg); opacity: 1 } 100% { -ms-transform: rotate(-320deg); opacity: 0 } } @-ms-keyframes spinoffPulse { 0% { -ms-transform: rotate(0deg) } 100% { -ms-transform: rotate(360deg) } } ` const isLoadingHTML = `
Loading...
`; function changeIsLoadingVisible(visible_) { // 修改正在加载中的状态 $(".page-isloading").css("display", visible_ ? "flex" : "none"); } function changeIsLoadingIconVisible(visible_) { $(".page-isloading div").hidden = visible_ ? true : false; } function setIsLoadingHtml(val) { // 设置加载的文本 $(".whitesev-isloading-text").html("" + val + "") } function setIsLoadingAllHtml(val) { // 设置加载的文本 $(".page-isloading").html("" + val + "") } function repalceLink_Event() { let real_link = getRealLinkJson(); // console.log(real_link.items); function repalceA_Tag(DOM, new_href) { let aTag = Array.from(DOM.getElementsByTagName("a")); aTag.forEach((item) => { if (real_link.has(item.href)) { item.href = real_link.get(item.href); console.log("真实", item.href); } else { item.href = new_href; } }) removeControl(); } function Dictionary() { this.items = {}; //检查是否有某一个键 this.has = function (key) { return this.items.hasOwnProperty(key); } //为字典添加某一个值 this.set = function (key, val) { this.items[key] = val; } //删除某一个键 this.delete = function (key) { if (this.has(key)) { delete this.items[key]; return true; } return false; } //查找某一特定项 this.get = function (key) { return this.has(key) ? this.items[key] : undefined; } //返回字典中的所有值 this.values = function () { var res = []; for (var prop in this.items) { if (this.has(prop)) { res.push(this.items[prop]); } } return res; } //清空字典 this.clear = function () { this.items = {}; } //获取字典的长度 this.size = function () { return Object.keys(this.items).length; } //获取字典所有的键 this.keys = function () { return Object.keys(this.items); } //返回字典本身 this.getItems = function () { return this.items; } } function getRealLinkJson() { // 由于真实链接存储在 script 标签中,得取出 let script_ele = $("script[id^='atom-data-']"); let script_ele_array = Array.from(script_ele); let ret_data = new Dictionary(); script_ele_array.forEach((item) => { let json_data = JSON.parse(item.innerHTML); if (json_data["data"]["resultAtomData"] != null) { json_data = json_data["data"]["resultAtomData"]; let child_site = json_data['content']['site']; if (child_site != null) { let child_list = child_site['list']; child_list.forEach((item) => { let tcUrl = item['urlParams']['tcUrl']; let original = item['urlParams']['originUrl']; ret_data.set(tcUrl, original); }) } } }) return ret_data; } function addWebsiteFlag(DOM) { let WebsiteTitle = Array.from(DOM.getElementsByClassName("c-title-text")); if (WebsiteTitle.length) { console.log(WebsiteTitle[0]); let Title_Div = document.createElement("div"); Title_Div.className = 'component-box'; Title_Div.innerHTML = `CSDN下载`; WebsiteTitle[0].append(Title_Div); } } function removeControl() { // 移除元素 if ($(".icon-logo").length) { $(".icon-logo")[0].remove(); //底部下一页前面图标删除 } $("#page-relative").remove(); //末尾 ===>> 大家都在搜 广告位 $(".c-recomm-wrap.new-ux-recom-wrapper.c-bg-color-white.animation").remove(); //中间 ===>> 大家都在搜 广告位 $("#pop-up").remove(); //跳转百度app提示 $(".ec_wise_ad").parent().remove(); // 顶部的部分商品广告 let searchWebSitePages = $(".c-result.result"); searchWebSitePages = Array.from(searchWebSitePages); searchWebSitePages.forEach((items, index) => { let dataLog = JSON.parse(items.dataset.log); let searchArticleOriginal_link = dataLog['mu']; if (searchArticleOriginal_link.match(/recommend_list.baidu.com/g)) { console.log('%c%s', 'color:blue;', "删除广告==>大家都在搜"); items.remove(); } if (items.outerText.substr(0, 5) == "大家还在搜") { console.log('%c%s', 'color:blue;', "删除广告==>大家都在搜:显示出来的"); items.remove(); } if (items.childNodes.length > 1) { if (items.outerText.match(/(大家还在搜|百度APP内打开)/)) { console.log('%c%s', 'color:blue;', "删除广告==>大家都在搜:隐藏的(点击后,跳出来的)"); items.childNodes[1].remove(); } } let bottom_logo_text = items.getElementsByClassName("c-color-source"); //底部标识 if (bottom_logo_text != null) { let bottom_remove_flag = false; let bottom_logo = Array.from(bottom_logo_text); bottom_logo.forEach((items_b, index_b) => { let it_text = items_b.outerText; if (it_text.match(/百度(APP内打开|手机助手)/)) { bottom_remove_flag = true; } }) if (bottom_remove_flag) { console.log('%c%s', 'color:blue;', "删除广告==>百度APP内打开"); items.remove(); } } if (items.getElementsByClassName('component-box').length == 0 && searchArticleOriginal_link.match(/http(s|):\/\/(download.csdn.net|www.iteye.com\/resource)/g)) { addWebsiteFlag(items); console.log("插入csdn标题"); } if (items.hasAttribute("srcid") && items.attributes.srcid.value.match(/(sigma|vid_fourfold)/g)) { console.log('%c%s', 'color:blue;', "删除推荐==>xxx 相关 xxx"); items.remove(); } if (searchArticleOriginal_link.match(/expert.baidu.com/g)) { console.log('%c%s', 'color:blue;', "删除广告==>百度健康"); items.remove(); } if (searchArticleOriginal_link.match(/author.baidu.com\/home\//g)) { console.log('%c%s', 'color:blue;', "删除广告==>百家号聚合"); items.remove(); } if (dataLog['ensrcid'] == 'wenda_inquiry') { console.log('%c%s', 'color:blue;', "删除广告==> 问一问"); items.remove(); } }) let every_result = $("span"); every_result = Array.from(every_result); every_result.forEach((items, index) => { let result_parent = items.parentElement.parentElement; if (items.outerText.match(/百度APP内打开/) || result_parent.getAttribute("data-from") == "etpl") { console.log("存在隐藏"); every_result[index].parentElement.parentElement.remove(); } }) } function replaceLink() { let searchWebSitePages = $(".c-result.result"); searchWebSitePages = Array.from(searchWebSitePages); searchWebSitePages.forEach((items, index) => { let searchArticleOriginal_link = JSON.parse(items.dataset.log)['mu']; if (searchArticleOriginal_link) { let BaiduSearchReplaceAfter = items.getElementsByTagName("article")[0]; if (BaiduSearchReplaceAfter && BaiduSearchReplaceAfter.hasAttribute('rl-link-href')) { let BaiduSearchReplaceAfterValue = BaiduSearchReplaceAfter.getAttributeNode("rl-link-href").value; if (!BaiduSearchReplaceAfterValue.match(/http(s|):\/\/m.baidu.com\/from/g)) { // console.info('%c%s', 'color:red;', "当前" + runNum + "的 ===>> " + BaiduSearchReplaceAfterValue); if (GM_getValue("menu_showisdirect")) { let getIsRedirectIcon = $(items).find(".white-bdsearch-isredirecrt"); if (getIsRedirectIcon.length === 0) { let title_text_element = $(items).find(".c-title-text"); let is_redirect_icon = document.createElement("div"); is_redirect_icon.className = "white-bdsearch-isredirecrt"; is_redirect_icon.innerHTML = ""; title_text_element.prepend(is_redirect_icon); } } return } if (BaiduSearchReplaceAfterValue.match(/http:\/\/www.internal.video.baidu.com/g) || searchArticleOriginal_link.match(/http:\/\/www.internal.video.baidu.com/g)) { let internalVideo = decodeURIComponent(BaiduSearchReplaceAfter.getAttribute("rl-link-data-log")); let internalVideoMatch = internalVideo.match(/\/sf\?pd=video_pag(.*?)={/g); if (internalVideoMatch) { internalVideoMatch = internalVideoMatch[0]; let newinternalVideo = internalVideoMatch.substring(0, internalVideoMatch.length - 2); // console.log('%c%s', 'color:orange;', "修复后的视频:" + newinternalVideo); BaiduSearchReplaceAfter.setAttribute("rl-link-href", newinternalVideo); repalceA_Tag(items, newinternalVideo); } } else if (searchArticleOriginal_link.match(/http:\/\/m.baidu.com\/productcard/g)) { console.info('%c%s', 'color:red;', "该链接不予替换"); } else { BaiduSearchReplaceAfter.setAttribute("rl-link-href", searchArticleOriginal_link); repalceA_Tag(items, searchArticleOriginal_link); //console.info('%c%s','color:red;',"修复后的 ===>> "+searchArticleOriginal_link); } } } }) } function main() { removeControl(); replaceLink(); } main(); } var runNum = 0; function replaceLink() { Array.from($("script")).forEach((items, index) => { if (items.text.match(/define\(\"@molecule\/aftclk\/index\",/g)) { console.log('%c%s', 'color:blue;', "删除跳转百度app提示js==>"); items.remove(); } }) $(".search-page").bind("DOMNodeInserted", function () { // console.log("当前执行次数:" + runNum); repalceLink_Event(); runNum = runNum + 1; }) } let baidu = { current_url: window.location.href, init() { this.search(); this.baijiahao(); this.tieba(); this.wenku(); this.jingyan(); this.baike(); this.baiketashuo(); this.zhidao(); }, css: { search: ` .c-container.na-ec-item, .c-container.ec-container, div[data-type="ad"], .c-result.sfc-log[data-tpl="adv_wenku_fc"], .c-recomm-wrap.new-ux-recom-wrapper.animation, #results-pre, .video-recommend, .c-result.sfc-log[data-tpl="search_recomm"], .sfc-image-content-waterfall-item[wat-item-data-id="no-img"], .se-results-pre, .ec_wise_ad, div#copyright + div{ display:none !important; } .searchboxtop.newsearch-white-style .se-form { border-color: #4e6ef2 !important; } .searchboxtop.newsearch-white-style .se-bn { color: #fff !important; background: #4e6ef2 !important; } .se-head-logo .se-logo img { display: inherit !important; } .se-head-tablink { border-bottom: 1px solid #e6e6e6 !important; //background-color: #fff !important; background-color: transparent !important; } a.se-tabitem span{ color: #000 !important; } // div.c-peak-layer{ // display:none !important; // } 百度关键字背景 .se-tablink-scroll-wrapper .se-tab-cur:after{ border-bottom: 2px solid #38f !important; } .c-tags-scroll.c-padding-x{ display: none !important; } .white-bdsearch-isredirecrt{ display: inline-flex; background: #43ba76; color: #fff; width: 28px; font-size: 16px; line-height: 25px; justify-content: center; align-items: center; border-radius: 5px; margin: 0 auto; margin-right: 6px; } `, searchmain: ` html, body, div#header{ height: 100%; } form#index-form{ /* fixed垂直水平居中 */ position: fixed; top:0; right:0; bottom:0; left:0; margin:auto !important; width: 90%; } div#navs ~ div, #login-wraps, a.square-enterance, div#ts-image-uploader-icon, div.baiduappcall-wrap div.voice.call, div.tab_news, div#navs{ display: none !important; } `, baijiahao: ` .layer-wrap, .openImg, .oPadding, .infinite-scroll-component__outerdiv, .bottomTTSStruct, .undefined, .headDeflectorContainer, .followSuper, #searchwordSdk ~ div:nth-child(n+4), #searchwordSdk, div#commentModule div div span:last-child{ display:none !important; } body.scrollHide{ overflow:auto !important; } .mainContent{ height: auto !important; } `, tieba: ` .tb-backflow-defensive, .fixed-nav-bar-defensive, .post-cut-guide, .ertiao-wrap-defensive, .feed-warp.gray-background, .pb-page-wrapper.app-view.transition-fade nav:first-child, .comment-box, .only-lz, .nav-bar-v2 .nav-bar-bottom, .more-image-desc, .fengchao-banner-defensive, .wake-app, .banner-wrapper-defensive{ display:none !important; } body.tb-modal-open{ overflow:auto !important; }`, wenku: ` .reader-pop-manager-view-containter, .core-download, .card-wrap.card-vip, #app{ display:none !important; } .reader-wrap{ height:auto !important; } #view-app, #view-rr-app{ max-height: 100% !important; } .top-card{ margin-top: 10px !important; } *{ -webkit-touch-callout: inherit !important; -khtml-user-select: auto !important; -moz-user-select: auto !important; -ms-user-select: auto !important; user-select: auto !important; } `, jingyan: ` .article-feed-next, .wgt-rel-exp-feed, .article-feed-btn-fixed, .read-whole-mask.app, .asp-self-rander{ display:none !important; } .exp-content-container{ max-height: 100% !important; overflow:auto !important; } `, baike: ` .BK-after-content-wrapper, .yitiao-container, .BK-content-load, #J-tashuo-button-fixed, #J-super-layer-promote{ display:none !important; } #J-other-content{ display:block !important; } `, zhidao: ` /* .dec + div, */ #feed-recommend, .dec, .wgt-topic-hot, #respect-footer, #wap-youx-change-asp, div.question-line + div:not(.replies-container), .wgt-asp-youx, .w-detail-display-btn, .ask-for-friend, #knowledge-answer-list, .go-to-ask, div[class*='ads']{ display:none !important; } .w-detail-container{ max-height: 100% !important; overflow: auto !important; } `, }, search() { // 百度搜索 if (this.current_url.match(/(m|www).baidu.com/g)) { GM_addStyle(this.css.search); if(this.current_url.match(/^http(s|):\/\/(m|www).baidu.com\/$/g)){ GM_addStyle(this.css.searchmain); } $(function () { main_start(); }) } }, baijiahao() { // 百家号 if (this.current_url.indexOf("baijiahao.baidu.com") != -1) { GM_addStyle(this.css.baijiahao); } }, tieba() { // 贴吧 if (this.current_url.indexOf("tieba.baidu.com") != -1) { GM_addStyle(this.css.tieba); if (this.current_url.indexOf("tieba.baidu.com/p/") != -1) { setTimeout(function () { tiebaLoadComments(); }, 2000); } } }, wenku() { // 文库 if (this.current_url.indexOf("wk.baidu.com") != -1) { GM_addStyle(this.css.wenku); } }, jingyan() { // 经验 if (this.current_url.indexOf("jingyan.baidu.com") != -1) { GM_addStyle(this.css.jingyan); } }, baike() { // 百科 if (this.current_url.indexOf("baike.baidu.com") != -1) { GM_addStyle(this.css.baike); let page_ = 1; let page_interval_lock = false; let more_url = "https://baike.baidu.com" + window.location.pathname; let ele_url; try { ele_url = document.getElementById("J-gotoPC-top").href; } catch (err) { return; } let match_id = ele_url.match(/item\/.*\/(\d*)/); function set_normal_img_size() { //GM_xmlhttpRequest获取到的图片大小要重新设置 let col_para = document.getElementsByClassName("col-para"); $.each(col_para, (i, n) => { n.setAttribute("style", "width: 42.936vw;margin: 0 auto;"); let content_img_item = n.getElementsByClassName("content-img-item"); let content_img_link = n.getElementsByClassName("content-img-link"); let content_album = n.getElementsByClassName("content-album"); if (content_album.length != 0) { content_album[0].setAttribute("style", ""); content_img_item = n.getElementsByClassName("content-album-item"); content_img_link = n.getElementsByClassName("content-album-link"); } if (content_img_item.length != 0) { content_img_item[0].setAttribute("style", "max-height: 39vw;max-width: 30vw;border-radius: 0.09rem;margin: 0 auto;overflow: hidden;"); } if (content_img_link.length != 0) { content_img_link[0].setAttribute("style", "width: 30vw;"); } }) } function insert_img() { // GM_xmlhttpRequest获取到的要重新将图片链接插入到img标签中 let content_img = document.getElementsByClassName("lazy-img"); $.each(content_img, (i, n) => { let content_img = n.parentElement.parentElement.parentElement; let img_url = content_img.getAttribute("data-src") ? content_img.getAttribute("data-src") : n.getAttribute("data-url"); if (img_url != null) { n.innerHTML = ''; } }) } function loadMore() { // 循环加载更多内容 GM_addStyle(isLoadingCSS); $(".BK-main-content").after(isLoadingHTML); if (match_id.length >= 2) { // 由于不知道有多少页,定时器加载判断 var page_interval = setInterval(function () { console.log("定时器loading"); changeIsLoadingVisible(true); if (page_interval_lock == false) { page_interval_lock = true; more_url = more_url + "?wpf=3&ldr=1&page=" + page_ + "&insf=1&_=" + new Date().getTime(); console.log(more_url); GM_xmlhttpRequest({ url: more_url, timeout: 5000, method: "GET", async: false, headers: { "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Mobile Safari/537.36 Edg/96.0.1054.62" }, onload: function (resp) { changeIsLoadingVisible(false); let $_resp = $(resp.response); let main_content = $_resp.find(".BK-main-content"); let new_content = main_content.prevObject[0].innerHTML; if (new_content.trim() == ``) { console.log("已到达最大页" + (page_ - 1)); insert_img(); set_normal_img_size(); setIsLoadingAllHtml("已到达最大页" + (page_ - 1)); clearInterval(page_interval); } else { setIsLoadingHtml("正在加载页 " + page_); $(".BK-main-content").append($(new_content)); }; window.history.pushState('forward', null, setLocationUrl); page_ = page_ + 1; page_interval_lock = false; }, onerror: function (resp) { console.log(resp); console.log("请求失败"); insert_img(); set_normal_img_size(); setIsLoadingAllHtml("请求失败"); changeIsLoadingIconVisible(true); clearInterval(page_interval); } }) } }, 1000) } else { console.log("匹配id失败"); } } setTimeout(function () { loadMore(page_); }, 3000); } }, baiketashuo() { if (this.current_url.indexOf("baike.baidu.com/tashuo") != -1) { setTimeout(function () { remove_bottom_ad(); }, 2000) function remove_bottom_ad() { // 去除底部广告 let index_tashuo_list_bottom = document.getElementById("index_tashuo_list").children; for (let i = 0; i < index_tashuo_list_bottom.length; i++) { let item = index_tashuo_list_bottom[i]; let class_name = item.className; if (class_name != "J-hot-item-container") { console.log("TA has ad,remove !"); item.remove(); i--; } } } } }, zhidao() { // 知道 if (this.current_url.indexOf("zhidao.baidu.com") != -1) { GM_addStyle(this.css.zhidao); $(".ec-ad").parent().remove(); } } } function autoLoadNextPage() { var isloding_flag = false; $("#page-controller").after($(isLoadingHTML)); GM_addStyle(isLoadingCSS); $(window).bind("scroll", function () { // scroll at bottom let userScrollHeight = Math.ceil($(window).scrollTop() + $(window).height() + 150); // let userScrollHeight = Math.ceil($(window).scrollTop() + $(window).height()); if (userScrollHeight >= $(document).height()) { // load data if (isloding_flag == false) { changeIsLoadingVisible(true); isloding_flag = true; let next_page_url = $(".new-nextpage").attr("href") || $(".new-nextpage-only").attr("href"); let params_pn = new URL(next_page_url).search.match(/[0-9]+/); ; let next_page_textContent = params_pn.length == 0 ? "第 2 页" : "第 " + (parseInt(params_pn[0])) + " 页"; console.log("正在请求%s数据", next_page_textContent, next_page_url); /* setIsLoadingHtml(next_page_textContent + " Loading..."); */ setIsLoadingHtml("Loading..."); GM_xmlhttpRequest({ url: next_page_url, timeout: 5000, method: "GET", onload: function (resp) { changeIsLoadingVisible(false); console.log("正在请求的%surl", next_page_textContent, next_page_url); // console.log(resp.response); let page_html = $(resp.response); let search_result = page_html.find(".c-result.result"); let next_html_next_page_html = page_html.find("#page-controller"); let this_page_results = $("#results"); if (this_page_results != void 0) { $.each(search_result, (i, n) => { this_page_results.append(n); }) $("#page-controller").html(next_html_next_page_html); } else { setIsLoadingAllHtml("已加载所有的搜索结果"); $(window).unbind(); } isloding_flag = false; }, onerror: function (resp) { if (next_page_url == undefined) { console.log("未获取到下一页的url"); } else { console.log(resp); setIsLoadingAllHtml("%s加载失败", next_page_textContent); } } }) } else { let next_page_textContent = $(".new-nowpage").length == 0 ? "第 2 页" : "第 " + (parseInt($(".new-nowpage")[0].textContent.match(/([0-9]+)/)) + 1) + " 页"; console.log("正在加载%s中请稍后,请勿重复", next_page_textContent); } } }) } async function tiebaLoadComments() { // 贴吧加载评论 const tieba = { useragent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39", getNewCommentInnerHTML: (element, user_commands_list) => { // 根据dom获取需要插入的评论的html let data_field = JSON.parse(element.attributes["data-field"].value); if(Object.keys(data_field).length == 0){ return; } let user_id = data_field['author']['user_id']; let builderId = data_field['content']['builderId']; let user_command = data_field["content"]["content"]; let user_home_url = element.querySelector(".p_author_face").attributes["href"].value; let user_landlord_name = data_field["author"]["user_name"]; let user_name = element.querySelector(".p_author_name"); if (user_name) { user_name = user_name.textContent; } else { user_name = element.querySelector(".p_author_face > img").attributes["username"].value; } let user_avator = element.querySelector(".p_author_face > img").attributes["data-tb-lazyload"] || element.querySelector(".p_author_face > img").attributes["src"]; user_avator = user_avator.value; let is_landlord = 0; if (user_id == builderId) { user_name = user_name + ''; is_landlord = 1; } let ele_tail_wrap = element.querySelector(".post-tail-wrap"); let user_ip_position = ele_tail_wrap; let user_floor = ''; let user_comment_time = '1970-1-1 00:00:00'; if (ele_tail_wrap) { user_ip_position = user_ip_position.children[0].textContent; if (user_ip_position.match('来自|禁言')) { user_ip_position = ''; } user_floor = ele_tail_wrap.children[ele_tail_wrap.children.length - 2].textContent; let tail_children = ele_tail_wrap.querySelectorAll("span.tail-info"); user_comment_time = tail_children[tail_children.length - 1].textContent; } else { ele_tail_wrap = element.querySelector(".acore_reply_tail"); user_ip_position = data_field['content']['ip_address']; user_floor = data_field['content']['post_no'] + '楼'; user_comment_time = data_field['content']['date']; if (!user_command) { user_command = element.querySelector(".d_post_content").innerHTML; } if (user_ip_position) { user_ip_position = "IP属地:" + user_ip_position } } let currentTime = new Date(); //结束时间 let timeDifference = currentTime.getTime() - new Date(user_comment_time.replace(/-/g, "/")).getTime(); //时间差的毫秒数 //------------------------------ //计算出相差天数 let days = Math.floor(timeDifference / (24 * 3600 * 1000)) if (days > 0) { user_comment_time = days + "天前" } else { //计算出小时数 let leave1 = timeDifference % (24 * 3600 * 1000) //计算天数后剩余的毫秒数 let hours = Math.floor(leave1 / (3600 * 1000)) if (hours > 0) { user_comment_time = hours + "小时前" } else { //计算相差分钟数 let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数 let minutes = Math.floor(leave2 / (60 * 1000)) if (minutes > 0) { user_comment_time = minutes + "分钟前" } else { //计算相差秒数 let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数 let seconds = Math.round(leave3 / 1000); user_comment_time = seconds + "秒前" } } } let post_id = data_field["content"]["post_id"]; let new_user_commands_innerHTML = ''; if (user_commands_list[post_id]) { Array.from(user_commands_list[post_id].comment_info).forEach(result => { let u_user_name = result['show_nickname']; let u_content = result['content']; let u_user_id = result['user_id']; if (builderId == u_user_id) { u_user_name += ''; } let newInnerHTML = '
' + '
' + '' + u_user_name + ': ' + '
' + u_content + '
' + '
' + '
'; new_user_commands_innerHTML += newInnerHTML; }) } if (new_user_commands_innerHTML) { new_user_commands_innerHTML = '
' + new_user_commands_innerHTML + '
' } let new_command_innerHTML = `

` + user_command + `

` + new_user_commands_innerHTML + `
` return new_command_innerHTML; }, insertNewCommentInnerHTML: (_html_) => { // 根据评论的html插入页面中 if ($(".post-cut-guide").length) { $(".post-cut-guide").before(_html_); } else { $(".pb-page-wrapper").append(_html_); // 老版帖子 } }, insertOnlyLZ: () => { // 插入之看楼主的事件 let ele_parent = $("#replySwitch"); let onlyLzInnerHTML = `
只看楼主
` ele_parent.append(onlyLzInnerHTML); let quxiaoonlylz_css = ` .white-only-lz-qx:before { content: "取消"; } .white-only-lz-none { display: none; }`; GM_addStyle(quxiaoonlylz_css); $(".white-only-lz").on("click", (event) => { tieba.displayCommand(event.currentTarget.classList); }) }, getPageComment: (url) => { // 获取第一页的评论(不包括评论的评论) return new Promise(res => { GM_xmlhttpRequest({ url: url, timeout: 5000, method: "GET", headers: { "User-Agent": tieba.useragent }, onload: function (resp) { let _html_ = $(resp.responseText); res(_html_); }, onerror: function (resp) { if (resp.error.match('wappass.baidu.com')) { let url = resp.error.match(/"(.*?)"/)[1]; window.location.href = url; console.log("触发百度校验", url); } else { console.log(resp); res(400); } } }) }) }, getPageCommentList: (url) => { // 获取第一页的评论的评论 return new Promise(res => { GM_xmlhttpRequest({ url: url, timeout: 5000, method: "GET", headers: { "Accept": "application/json, text/javascript, */*; q=0.01", "User-Agent": tieba.useragent }, onload: function (resp) { let data = JSON.parse(resp.responseText); let comment_list = data["data"]["comment_list"]; res(comment_list); }, onerror: function (resp) { console.log(resp); res(400); } }) }) }, loadingNextCommand: () => { // 自动加载下一页的评论 var isloding_flag = false; $(window).bind("scroll", async function () { let userScrollHeight = Math.ceil($(window).scrollTop() + $(window).height() + 150); if (userScrollHeight >= $(document).height()) { if (isloding_flag) { console.log("正在请求中..."); } else { changeIsLoadingVisible(true); isloding_flag = true; console.log("请求"); window.page += 1; let timeStamp = Date.now(); let next_page_url = "https://tieba.baidu.com/p/" + window.param_tid + "?pn=" + window.page; let next_page_all_comment_url = "https://tieba.baidu.com/p/totalComment?t=" + timeStamp + "&tid=" + window.param_tid + "&fid=" + window.param_forum_id + "&pn=" + window.page + "&see_lz=0"; console.log(next_page_url); console.log(next_page_all_comment_url); let pageHTML = await tieba.getPageComment(next_page_url); let user_commands_list = await tieba.getPageCommentList(next_page_all_comment_url); if (pageHTML == 400 || user_commands_list == 400) { setIsLoadingAllHtml("未知错误,请看控制台"); $(window).unbind(); } let commands = pageHTML.find(".l_post.l_post_bright"); commands = Array.from(commands); commands.forEach(ele => { tieba.insertNewCommentInnerHTML(tieba.getNewCommentInnerHTML(ele, user_commands_list)); window.floor_num += 1; }) console.log("成功请求到下一页"); if (Array.from($(".white-only-lz")[0].classList).includes('white-only-lz-qx')) { let lzReply = $(".post-item"); Array.from(lzReply).forEach(ele => { let landlord = ele.getAttribute("landlord"); if (landlord == '0') { ele.classList.add("white-only-lz-none"); } }) } isloding_flag = false; changeIsLoadingVisible(false); if (window.page >= window.max_page) { setIsLoadingAllHtml("已加载所有的评论"); changeIsLoadingVisible(true); $(window).unbind(); } } } }) }, insertLoadingHTML: () => { console.log("插入loading"); $('.main-page-wrap').append($(isLoadingHTML)); GM_addStyle(isLoadingCSS); }, displayCommand: (classlist) => { // 动态显示只看楼主 if (Array.from(classlist).includes('white-only-lz-qx')) { $(".white-only-lz").removeClass("white-only-lz-qx"); let lzReply = $(".post-item"); Array.from(lzReply).forEach(ele => { ele.classList.remove("white-only-lz-none"); }) } else { $(".white-only-lz").addClass("white-only-lz-qx"); let lzReply = $(".post-item"); Array.from(lzReply).forEach(ele => { let landlord = ele.getAttribute("landlord"); if (landlord == '0') { ele.classList.add("white-only-lz-none"); } }) } } } window.param_tid = window.location.pathname.match(/([0-9]+)/g); if (window.param_tid) { window.param_tid = window.param_tid[0]; window.param_forum_id = $(".recommend-item").attr("data-banner-info"); if (window.param_forum_id) { window.param_forum_id = JSON.parse(window.param_forum_id)['forum_id']; let timeStamp = Date.now(); window.page = 1; let url = "https://tieba.baidu.com/p/totalComment?t=" + timeStamp + "&tid=" + window.param_tid + "&fid=" + window.param_forum_id + "&pn=" + window.page + "&see_lz=0"; let pageUrl = "https://tieba.baidu.com/p/" + window.param_tid + "?pn=" + window.page; let pageHTML = await tieba.getPageComment(pageUrl); let user_commands_list = await tieba.getPageCommentList(url); if (pageHTML == 400 || user_commands_list == 400) { console.log("新评论区获取失败"); } let commands = pageHTML.find(".l_post.l_post_bright"); window.max_page = pageHTML.find(".jump_input_bright"); if (window.max_page.length) { window.max_page = parseInt(max_page[0].attributes['max-page'].value); tieba.insertLoadingHTML(); tieba.loadingNextCommand(); } else { window.max_page = 1; } console.log("共 " + window.max_page + " 页评论"); commands = Array.from(commands); Array.from(document.getElementsByClassName("post-item")).forEach(ele => { ele.remove(); }) commands.shift(); // 此处是百度贴吧帖子的css,应对贴吧前端重新编译文件 GM_addStyle(` .post-item[data-v-74eb13e2] { overflow: hidden; margin: .16rem .13rem 0; } .post-item .user-line-post[data-v-74eb13e2] { margin-bottom: .06rem; } .user-line-wrapper[data-v-188c0e84], .user-line[data-v-188c0e84] { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; } .user-line-wrapper[data-v-188c0e84] { -webkit-box-pack: justify; -webkit-justify-content: space-between; -ms-flex-pack: justify; justify-content: space-between; } .post-item .content[data-v-74eb13e2] { padding-left: .44rem; } .user-line[data-v-188c0e84] { -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; -webkit-box-pack: left; -webkit-justify-content: left; -ms-flex-pack: left; justify-content: left; } .user-line-wrapper[data-v-188c0e84], .user-line[data-v-188c0e84] { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; } .user-line .avatar[data-v-188c0e84] { position: relative; -webkit-box-sizing: border-box; box-sizing: border-box; width: .36rem; height: .36rem; margin-right: .08rem; border-radius: 50%; background-repeat: no-repeat; background-position: 50%; background-size: cover; -webkit-box-flex: 0; -webkit-flex: none; -ms-flex: none; flex: none; } .tbfe-1px-border { position: relative; border-radius: .08rem; font-size: 0; } .user-line .user-info[data-v-188c0e84] { position: relative; overflow: hidden; -webkit-box-flex: 0; -webkit-flex: none; -ms-flex: none; flex: none; } .user-line .avatar[data-v-188c0e84]:after { border-radius: 50%; } .tbfe-1px-border:after { content: ""; position: absolute; z-index: 100; top: 0; left: 0; -webkit-box-sizing: border-box; box-sizing: border-box; border: 1px solid rgba(0,0,0,.12); -webkit-transform-origin: 0 0; -ms-transform-origin: 0 0; transform-origin: 0 0; pointer-events: none; } .user-line .user-info .username[data-v-188c0e84] { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; overflow: hidden; font-size: .15rem; line-height: .21rem; white-space: nowrap; -o-text-overflow: ellipsis; text-overflow: ellipsis; color: #141414; font-weight: 400; } .user-line .user-info .desc-info[data-v-188c0e84] { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; font-size: .12rem; line-height: .18rem; overflow: hidden; white-space: nowrap; -o-text-overflow: ellipsis; text-overflow: ellipsis; color: #a3a2a8; } .user-line .user-info .floor-info[data-v-188c0e84], .user-line .user-info .forum-info[data-v-188c0e84] { margin-right: .08rem; } .post-item .content .post-text[data-v-74eb13e2] { font-size: .16rem; line-height: .24rem; } .thread-text[data-v-ab14b3fe] { font-size: .13rem; line-height: .21rem; color: #141414; text-align: justify; word-break: break-all; } .post-item .content .lzl-post[data-v-74eb13e2] { margin-top: .06rem; } .lzl-post[data-v-5b60f30b] { padding: .08rem .12rem; background: #f7f7fa; border-radius: .08rem; } .post-item .content .post-split-line[data-v-74eb13e2] { margin-top: .12rem; background-color: #ededf0; height: 1px; width: 200%; -webkit-transform: scale(.5); -ms-transform: scale(.5); transform: scale(.5); -webkit-transform-origin: top left; -ms-transform-origin: top left; transform-origin: top left; } .lzl-post .lzl-post-item[data-v-5b60f30b]:first-child { margin-top: 0; } .lzl-post .lzl-post-item[data-v-5b60f30b] { margin-top: .04rem; } .lzl-post .lzl-post-item .text-box[data-v-5b60f30b] { font-size: .13rem; line-height: .2rem; } .lzl-post .lzl-post-item .text-box .link[data-v-5b60f30b] { display: -webkit-inline-box; display: -webkit-inline-flex; display: -ms-inline-flexbox; display: inline-flex; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; font-weight: 600; color: #a3a1a9; } .lzl-post .lzl-post-item .lzl-post-text[data-v-5b60f30b] { display: inline; } .thread-text[data-v-ab14b3fe] { font-size: .13rem; line-height: .21rem; color: #141414; text-align: justify; word-break: break-all; } .lzl-post .lzl-post-item .text-box .link .landlord[data-v-5b60f30b] { width: .28rem; height: .28rem; margin-left: .04rem; } .user-line .user-info .username .landlord[data-v-188c0e84] { width: .28rem; height: .28rem; margin-left: .04rem } `); GM_addStyle(` .thread-text .BDE_Smiley { width: .2rem; height: .2rem; vertical-align: middle; } .thread-text .BDE_Image{ margin-top: 8px; max-width: 350px; cursor: url(//tb2.bdstatic.com/tb/static-pb/img/cur_zin.cur),pointer; height: auto; width: auto; width: 100%; } .text-content .at{ font-weight: 600; color: #a3a1a9; } `); window.floor_num = 1; commands.forEach(element => { tieba.insertNewCommentInnerHTML(tieba.getNewCommentInnerHTML(element, user_commands_list)); window.floor_num += 1; }); tieba.insertOnlyLZ(); } } else { console.log("贴吧:未找到本页参数p"); } } function main_start() { replaceLink(); if (GM_getValue("menu_autoloading")) { autoLoadNextPage(); } } var GM_menus = [ ["menu_autoloading", "自动展开下一页", false], ["menu_showisdirect", "显示已重定向图标", true] ] function change_normal_menu() { //修改菜单默认值 // console.log(GM_listValues()) for (let i = 0; i < GM_menus.length; i++) { let get_GM_value = GM_getValue(GM_menus[i][0]); if (get_GM_value) { GM_menus[i][2] = true; } else { GM_menus[i][2] = false; } } } function register_GM_Menu() { //注册油猴菜单 for (let i = 0; i < GM_menus.length; i++) { let current_v = GM_menus[i][0]; let current_name = GM_menus[i][1]; let current_status = GM_menus[i][2]; GM_registerMenuCommand(`[${current_status?"√":"×"}]${current_name}`, function () { if (current_status) { GM_deleteValue(current_v); } else { GM_setValue(current_v, "1"); } window.location.reload(); }) } } change_normal_menu(); register_GM_Menu(); GM_addStyle(flagTitleCSS); baidu.init(); })();