头条搜索增强

单/双列自适应、去重定向、美化样式、PC端自动翻页、移动端悬浮底栏

  1. // ==UserScript==
  2. // @name 头条搜索增强
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2
  5. // @description 单/双列自适应、去重定向、美化样式、PC端自动翻页、移动端悬浮底栏
  6. // @author tutu辣么可爱(greasyfork)/IcedWatermelonJuice(github)
  7. // @require https://gf.qytechs.cn/scripts/415581-jquery%E5%BA%93/code/jquery%E5%BA%93.js?version=866373
  8. // @match https://so.toutiao.com/search*
  9. // @icon http://toutiao.com/favicon.ico
  10. // @grant GM_addStyle
  11. // @run-at document-start
  12. // @license MIT License
  13. // @data css v22.11.16.0
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. const css =
  19. `[data-css-device=pc] .main{width:100% !important}[data-css-device=pc] .s-result-list{display:grid;grid-template-columns:50% 50%;grid-gap:8px;margin:0 160px !important;max-width:100% !important;width:calc(100% - 320px) !important}[data-css-device=pc] .s-result-list .result-content:first-child,[data-css-device=pc] .s-result-list .whole-row-item{grid-column-start:1;grid-column-end:3}@media (max-width:1000px){[data-css-device=pc] .s-result-list{grid-template-columns:100%;width:calc(100% - 20px) !important;margin:0 10px !important}[data-css-device=pc] .s-result-list .result-content:first-child,[data-css-device=pc] .s-result-list .whole-row-item{grid-column-start:1;grid-column-end:2}}@media (max-width:900px){[data-css-device=pc] .logoWrap_D3HTJ a:not([class]){display:none;}[data-css-device=pc] .logoWrap_D3HTJ_ .logo_3DlEdN svg{display:none;}[data-css-device=pc] .logoWrap_D3HTJ_ .logo_3DlEdN{width:40px;height:40px;background:url(http://toutiao.com/favicon.ico) no-repeat;background-size:contain;padding:0;}}@media (max-width:800px){[data-css-device=pc] .logoWrap_D3HTJ_ .logo_3DlEdN{display:none;}}[data-css-device=pc] .s-result-list .result-content[data-i],[data-css-device=pc] .s-result-list .radius-solid-border{border:1px solid var(--black-6);border-radius:5px;padding:12px}[data-css-device=pc] .main .s-side-list{display:none !important}[data-css-device=pc] .result-content .logoWrap_D3HTJ,[data-css-device=pc] .result-content .wrap_2lSYxG{min-width:100% !important;}[data-css-device=pc] .result-content .nav_2v7eXx{padding:16px 10px 12px !important;justify-content:center !important}[data-css-device=pc] .result-content .searchWrap_14Gtni{position:absolute;left:50%;transform:translateX(-50%);max-width:588px;width:calc(100% - 20px);}[data-css-device=pc] .margin-top-24-important{margin-top:24px !important;}[data-css-device=pc] .auto-loading-notice[data-pos=bottom] span{background-image:-webkit-linear-gradient(right,#0000007f,#0000005f 25%,#0000007f 50%,#0000005f 75%,#0000007f 100%);-webkit-text-fill-color:transparent;-webkit-background-clip:text;-webkit-background-size:200% 100%;-webkit-animation:streamer-text-animation 1s linear infinite;}@keyframes streamer-text-animation{0%{background-position:0 0;}100%{background-position:-100% 0;}}[data-css-device=pc] .auto-loading-notice[data-pos=center]{width:200px;height:50px;position:fixed;left:50%;top:50%;transform:translateX(-50%);color:white;line-height:50px;backdrop-filter:blur(4px);background-color:rgba(34,34,34,.3);border-radius:15px}[data-css-device=mobile] #head-bar nav{text-align:center;}[data-css-device=mobile] #results{display:grid;grid-template-columns:repeat(2,calc(50% - 5px));grid-gap:10px;padding:10px 10px 20px;}[data-css-device=mobile][data-css-page=xiaoshipin] #results,[data-css-device=mobile][data-css-page=atlas] #results{display:block !important;}@media (max-width:800px){[data-css-device=mobile] #results{grid-template-columns:100%;}}[data-css-device=mobile] #results .result-content .l-card-bg{background-color:transparent !important;}[data-css-device=mobile] #results .result-content{background-color:var(--bg-card);border-radius:15px;height:100%;}[data-css-device=mobile] .ghost-search-bar{position:fixed;bottom:0;left:50%;width:100% !important;transform:translateX(-50%);z-index:99999;border-radius:15px;}[data-css-device=mobile] .ghost-search-bar input{text-align:center !important;margin-left:-30px;}[data-css-device=mobile] #page-bottom .logo_3Mdxl3{display:none;}[data-css-device=mobile] #page-bottom .from_1OKQFa{display:none;}[data-css-device=mobile] #page-bottom .formWrap{display:none;}`;
  20.  
  21. const device = location.search.search("dvpf=pc") >= 0 ? "pc" : "mobile";
  22. const enableMap = {
  23. map: {
  24. pc: ["synthesis", "information", "user", "question", "weitoutiao", "baike", "music"],
  25. mobile: ["*"]
  26. },
  27. check: function(d, p) {
  28. var m = this.map,
  29. ck=m.hasOwnProperty(d) && (m[d].indexOf("*") > -1 || m[d].indexOf(p) > -1);
  30. $("html").attr("data-css-device", ck?d:"");
  31. $("html").attr("data-css-page", ck?p:"");
  32. return ck
  33. }
  34. }
  35. var page = new URL(location.href).searchParams.get("pd") || "synthesis";
  36. GM_addStyle(css);
  37. if (!enableMap.check(device, page)) {
  38. return
  39. }
  40. if (device === "pc") {
  41. function otherCss(dom, index = 1) {
  42. dom = dom instanceof jQuery ? dom : $(dom);
  43. dom.attr("data-res-id", index);
  44. $(`<div class="result-content whole-row-item radius-solid-border text-center current-page-notice"><a href="javascript:void(0)" style="float:left;padding:0 12px"><< 首页</a><span>当前页:第${index}页</span><a href="javascript:void(0)" style="float:right;padding:0 12px">导航 >></a></div>`)
  45. .insertBefore(dom.children(".result-content[data-i=0]:first"));
  46. dom.find(".result-content.current-page-notice a").click((e) => {
  47. e = $(e.target).text();
  48. if (/首页/.test(e)) {
  49. $("html,body").animate({
  50. scrollTop: 0
  51. }, 500);
  52. } else if (/导航/.test(e)) {
  53. $("html,body").animate({
  54. scrollTop: dom.offset().top + dom.height() - $(window).height()
  55. }, 500);
  56. }
  57. })
  58. dom.children(".result-content:not([data-i])").each((i, e) => {
  59. e = $(e);
  60. if (e.children("[shared]")[0]) {
  61. e.addClass("whole-row-item text-center margin-top-24-important");
  62. if (/下一页|上一页/.test(e.text())) {
  63. e.attr("data-nav-id", index);
  64. e.find("a[data-search]").each((i, a) => {
  65. a = $(a);
  66. if (a.text() === "下一页") {
  67. a.data("targetID", index + 1)
  68. } else if (a.text() === "上一页") {
  69. a.data("targetID", (index - 1) < 1 ? 1 : (index - 1))
  70. } else {
  71. a.data("targetID", a.text())
  72. }
  73. a.click((e) => {
  74. e.preventDefault()
  75. navJump(a.data("targetID"));
  76. })
  77. })
  78. } else {
  79. e.hide();
  80. }
  81. }
  82. })
  83. dom.children(".result-content[data-i]:last").addClass("whole-row-item").hide();
  84. console.group();
  85. dom.find(".result-content[data-i] a").each((i, e) => {
  86. if (e.href.indexOf("/search/jump?url=") !== -1) {
  87. var url = new URL(e.href).searchParams.get("url");
  88. console.log("重定向: " + e.href + " --> " + url)
  89. e.href = url;
  90. }
  91. })
  92. console.groupEnd();
  93. }
  94.  
  95. function navJump(id) {
  96. id = typeof id === "number" ? id : parseInt(id);
  97. if (!$(".s-result-list").is(":visible") || !id) {
  98. return false
  99. }
  100.  
  101. function load(id, waitTime = false) {
  102. if ($(".s-result-list").length >= id) {
  103. $(".auto-loading-notice[data-pos=center]").remove();
  104. var target = $(`.s-result-list[data-res-id=${id}]`).is(":visible") ? $(
  105. `.s-result-list[data-res-id=${id}]`) : $(`.s-result-list:last`);
  106. if (waitTime) {
  107. setTimeout(() => {
  108. $("html,body").animate({
  109. scrollTop: target.offset().top - 85
  110. }, 500);
  111. }, 210)
  112. } else {
  113. $("html,body").animate({
  114. scrollTop: target.offset().top - 85
  115. }, 500);
  116. }
  117. return false
  118. }
  119. if ($(".auto-loading-notice[data-pos=center]").is(":visible")) {
  120. $(".auto-loading-notice[data-pos=center] span").text(
  121. `正在加载(${$(".s-result-list").length}/${id})`)
  122. } else {
  123. $("body").append(
  124. `<div class="auto-loading-notice text-center" data-pos="center"><span>正在加载(${$(".s-result-list").length}/${id})</span></div>`
  125. );
  126. }
  127. autoNextPage(".s-result-list:last", () => {
  128. setTimeout(() => {
  129. load(id, true);
  130. }, 0)
  131. })
  132. }
  133. load(id);
  134. }
  135.  
  136. function autoNextPage(dom, fn1, fn2) {
  137. dom = dom instanceof jQuery ? dom : $(dom);
  138. dom.find(".result-content:not([data-i]).whole-row-item a").each((i, e) => {
  139. if (e.innerText === "下一页" && /^https?:\/\//i.test(e.href)) {
  140. $(".auto-loading-notice[data-pos=bottom]").remove();
  141. dom.append(
  142. `<div class="auto-loading-notice result-content whole-row-item text-center" data-pos="bottom"><span>正在加载下一页</span></div>`
  143. );
  144. $.ajax({
  145. url: e.href,
  146. type: "GET",
  147. timeout: 10000,
  148. success: (r) => {
  149. r = $(r).find(".s-result-list");
  150. if (r.length === 1) {
  151. if (!$(`.s-result-list[data-res-id=${$(e).data("targetID")}]`)[
  152. 0]) {
  153. otherCss(r, $(e).data("targetID"));
  154. r.css("padding-top", "0");
  155. r.find(".result-content:first-child").hide();
  156. $(".auto-loading-notice[data-pos=bottom]").remove();
  157. $(".main").append(r);
  158. dom.animate({
  159. paddingBottom: 0
  160. }, 200);
  161. }
  162. typeof fn1 === "function" && fn1();
  163. }
  164. },
  165. error: (r) => {
  166. if (confirm("请求失败或超时,是否直接打开网页?")) {
  167. location.href = e.href;
  168. }
  169. $(".auto-loading-notice[data-pos=bottom]").remove();
  170. typeof fn2 === "function" && fn2();
  171. console.log("请求失败或超时", r)
  172. }
  173. })
  174. }
  175. })
  176. }
  177. $(".s-result-list").ready(() => {
  178. otherCss(".s-result-list");
  179. var loadFlag = true;
  180. $(document).scroll(function() {
  181. if (loadFlag && $(window).scrollTop() + $(window).height() > $(document).height() -
  182. 50) {
  183. loadFlag = false;
  184. autoNextPage(".s-result-list:last", () => {
  185. setTimeout(() => {
  186. loadFlag = true;
  187. }, 500)
  188. }, () => {
  189. $("html,body").animate({
  190. scrollTop: $(window).scrollTop() - 100
  191. }, 200, () => {
  192. loadFlag = true;
  193. });
  194. })
  195. }
  196. });
  197. });
  198. } else {
  199. $("#head-bar form").ready(() => {
  200. var ghostBar = $("#head-bar form").clone();
  201. ghostBar.find("i").attr("class","search-icon_TKyEHg");
  202. ghostBar.find("a").hide();
  203. ghostBar.find("input").attr("readonly", "readonly");
  204. ghostBar.addClass("ghost-search-bar");
  205. ghostBar.click(() => {
  206. $("#head-bar input").click();
  207. })
  208. ghostBar.hide();
  209. ghostBar.extend({
  210. "display": function(type, animateSpan) {
  211. var that = this;
  212. if (!type || !animateSpan || type === that.data("animate")) {
  213. return false
  214. }
  215. if (type === "show" && !that.is(":visible")) {
  216. that.data("animate", type);
  217. that.css("opacity", "0");
  218. that.css("bottom", "-65px");
  219. that.show();
  220. that.animate({
  221. opacity: 1,
  222. bottom: "0"
  223. }, animateSpan, "swing", () => {
  224. that.css("opacity", "");
  225. that.data("animate", "");
  226. });
  227. } else if (type === "hide" && that.is(":visible")) {
  228. that.data("animate", type);
  229. that.css("opacity", "1");
  230. that.animate({
  231. opacity: 0
  232. }, animateSpan, "linear", () => {
  233. that.hide();
  234. that.css("opacity", "");
  235. that.data("animate", "");
  236. });
  237. }
  238. }
  239. })
  240. $("#page-main").append(ghostBar);
  241.  
  242. function changeGhostDisplay() {
  243. var scroll = $(window).scrollTop(),
  244. scrollMin = $("#page-head").height(),
  245. scrollMax = $("#bottom-bar").offset().top - $(window).height();
  246. if (scroll >= scrollMin && scroll <= scrollMax) {
  247. ghostBar.display("show", 300);
  248. } else {
  249. ghostBar.display("hide", 200);
  250. }
  251. }
  252. changeGhostDisplay();
  253. $(document).scroll(function() {
  254. changeGhostDisplay()
  255. });
  256. $("#results .result-content a").each((i, e) => {
  257. var url = new URL(e.href);
  258. if (url.pathname === "/search/jump" && url.search.indexOf("url=") !== -1) {
  259. url = url.searchParams.get("url");
  260. console.log("重定向: " + e.href + " --> " + url)
  261. e.href = url;
  262. }
  263. })
  264. })
  265. }
  266. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址