头条搜索增强

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

Ajankohdalta 15.11.2022. Katso uusin versio.

  1. // ==UserScript==
  2. // @name 头条搜索增强
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description 单/双列自适应、去重定向、美化样式、PC端自动翻页、移动端悬浮底栏
  6. // @author tutu辣么可爱(greasyfork)/IcedWatermelonJuice(github)
  7. // @require https://greasyfork.org/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. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. const device = location.search.search("dvpf=pc") >= 0 ? "pc" : "mobile";
  18. const css = {
  19. pc: `.main{width:100%!important;}.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;}.s-result-list .result-content:first-child,.s-result-list .whole-row-item{grid-column-start:1;grid-column-end:3;}@media (max-width:1100px){.s-result-list{grid-template-columns:100%;}.s-result-list .result-content:first-child,.s-result-list .whole-row-item{grid-column-start:1;grid-column-end:2;}}.s-result-list .result-content[data-i],.s-result-list .radius-solid-border{border:1px solid var(--black-6);border-radius:5px;padding:12px;}.main .s-side-list{display:none!important;}.result-content .nav_2v7eXx{padding:16px 160px 12px!important;justify-content:center!important;}.result-content .searchWrap_14Gtni{position:absolute;left:50%;transform:translateX(-50%);}.margin-top-24-important{margin-top:24px!important;}`,
  20. mobile: `#head-bar nav{text-align:center;}#results{display:grid;grid-template-columns:repeat(2,calc(50% - 5px));grid-gap:10px;padding:10px 10px 20px;}@media (max-width:800px){#results{grid-template-columns:100%;}}#results .result-content .l-card-bg{background-color:transparent !important;}#results .result-content{background-color:var(--bg-card);border-radius:15px;height:100%;}.ghost-search-bar{position:fixed;bottom:10px;left:50%;width:100%!important;transform:translateX(-50%);z-index:99999;background:var(--bg-page2);border-radius:15px;}#page-bottom .logo_3Mdxl3{display:none;}#page-bottom .from_1OKQFa{display:none;}#page-bottom .formWrap{display:none;}`
  21. };
  22. GM_addStyle(css[device]);
  23.  
  24. if (device === "pc") {
  25. function otherCss(dom, index = 1) {
  26. dom = dom instanceof jQuery ? dom : $(dom);
  27. dom.attr("data-res-id", index);
  28. $(`<div class="result-content whole-row-item radius-solid-border text-center">当前页:第${index}页</div>`).insertBefore(dom.children(".result-content[data-i=0]"))
  29. dom.children(".result-content:not([data-i])").each((i, e) => {
  30. e = $(e);
  31. if (e.children("[shared]")[0]) {
  32. e.addClass("whole-row-item text-center margin-top-24-important");
  33. if (/下一页|上一页/.test(e.text())) {
  34. e.find("a[data-search]").each((i, a) => {
  35. a = $(a);
  36. if (a.text() === "下一页") {
  37. a.data("targetID", index + 1)
  38. } else if (a.text() === "上一页") {
  39. a.data("targetID", (index - 1) < 1 ? 1 : (index - 1))
  40. } else {
  41. a.data("targetID", a.text())
  42. }
  43. a.click((e) => {
  44. e.preventDefault()
  45. navJump(a.data("targetID"));
  46. })
  47. })
  48. } else {
  49. e.hide();
  50. }
  51. }
  52. })
  53. dom.children(".result-content[data-i]:last").addClass("whole-row-item").hide();
  54. dom.find(".result-content[data-i] a").each((i, e) => {
  55. if (e.href.indexOf("/search/jump?url=") !== -1) {
  56. var url = new URL(e.href).searchParams.get("url");
  57. console.log("重定向: " + e.href + " --> " + url)
  58. e.href = url;
  59. }
  60. })
  61. }
  62.  
  63. function navJump(id) {
  64. id = typeof id === "number" ? id : parseInt(id);
  65. if (!$(".s-result-list").is(":visible") || !id) {
  66. return false
  67. }
  68. if ($(".s-result-list").length < id) {
  69. autoNextPage(".s-result-list:last", () => {
  70. $("html,body").stop(true);
  71. $("html,body").animate({
  72. scrollTop: $(`.s-result-list:last`).offset().top - 85
  73. }, 500, () => {
  74. navJump(id);
  75. });
  76. })
  77. } else if ($(`.s-result-list[data-res-id=${id}]`).is(":visible")) {
  78. $("html,body").stop(true);
  79. $("html,body").animate({
  80. scrollTop: $(`.s-result-list[data-res-id=${id}]`).offset().top - 85
  81. }, 500);
  82. }
  83. }
  84.  
  85. function autoNextPage(dom, fn) {
  86. dom = dom instanceof jQuery ? dom : $(dom);
  87. dom.find(".result-content:not([data-i]).whole-row-item a").each((i, e) => {
  88. if (e.innerText === "下一页" && /^https?:\/\//i.test(e.href)) {
  89. $.get(e.href, (r) => {
  90. r = $(r).find(".s-result-list");
  91. if (r.length === 1) {
  92. if (!$(`.s-result-list[data-res-id=${$(e).data("targetID")}]`)[0]) {
  93. otherCss(r, $(e).data("targetID"));
  94. r.css("padding-top", "0");
  95. r.find(".result-content:first-child").hide();
  96. $(".main").append(r);
  97. dom.animate({
  98. paddingBottom: 0
  99. }, "fast");
  100. //dom.css("", "0")
  101. }
  102. typeof fn === "function" && fn();
  103. }
  104. })
  105. return false
  106. }
  107. })
  108. }
  109. $(".s-result-list").ready(() => {
  110. otherCss(".s-result-list");
  111. var loadFlag = true;
  112. $(document).scroll(function() {
  113. if (loadFlag && $(window).scrollTop() + $(window).height() > $(document).height() -
  114. 50) {
  115. loadFlag = false;
  116. autoNextPage(".s-result-list:last", () => {
  117. setTimeout(() => {
  118. loadFlag = true;
  119. }, 500)
  120. })
  121. }
  122. });
  123. });
  124. } else {
  125. $("#head-bar form").ready(() => {
  126. var ghostBar = $("#bottom-bar form").clone();
  127. ghostBar.find("a").hide();
  128. ghostBar.find("input").attr("readonly", "readonly");
  129. ghostBar.addClass("ghost-search-bar");
  130. ghostBar.attr("isAnimate", "");
  131. ghostBar.click(()=>{
  132. $("#page-bottom input").click();
  133. })
  134. ghostBar.hide();
  135. ghostBar.extend({
  136. "display": function(needShow = true, animateSpan = 100) {
  137. var that = this;
  138. if (that.data("animate")) {
  139. return false
  140. }
  141. if (needShow && !that.is(":visible")) {
  142. that.data("animate", "animate");
  143. that.css("opacity", "0");
  144. that.show();
  145. that.animate({
  146. opacity: 1
  147. }, animateSpan, () => {
  148. that.css("opacity", "");
  149. that.data("animate", "");
  150. });
  151. } else if (!needShow && that.is(":visible")) {
  152. that.data("animate", "animate");
  153. that.css("opacity", "1");
  154. that.animate({
  155. opacity: 0
  156. }, animateSpan, () => {
  157. that.hide();
  158. that.css("opacity", "");
  159. that.data("animate", "");
  160. });
  161. }
  162. }
  163. })
  164. $("#page-main").append(ghostBar);
  165. function changeGhostDisplay() {
  166. var scroll = $(window).scrollTop(),
  167. scrollMin = $("#page-head").height(),
  168. scrollMax = $("#page-bottom input").offset().top - $(window).height();
  169. if (scroll >= scrollMin && scroll <= scrollMax) {
  170. ghostBar.display(true, 500);
  171. } else {
  172. ghostBar.display(false, 200);
  173. }
  174. }
  175. changeGhostDisplay();
  176. $(document).scroll(function() {
  177. changeGhostDisplay()
  178. });
  179. $("#results .result-content a").each((i, e) => {
  180. if (e.href.indexOf("/search/jump?url=") !== -1) {
  181. var url = new URL(e.href).searchParams.get("url");
  182. console.log("重定向: " + e.href + " --> " + url)
  183. e.href = url;
  184. }
  185. })
  186. })
  187. }
  188. })();