头条搜索增强

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

Ajankohdalta 15.11.2022. Katso uusin versio.

// ==UserScript==
// @name         头条搜索增强
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  单/双列自适应、去重定向、美化样式、PC端自动翻页、移动端悬浮底栏
// @author       tutu辣么可爱(greasyfork)/IcedWatermelonJuice(github)
// @require      https://greasyfork.org/scripts/415581-jquery%E5%BA%93/code/jquery%E5%BA%93.js?version=866373
// @match        https://so.toutiao.com/search*
// @icon         http://toutiao.com/favicon.ico
// @grant        GM_addStyle
// @run-at       document-start
// @license      MIT License
// ==/UserScript==

(function() {
	'use strict';
	const device = location.search.search("dvpf=pc") >= 0 ? "pc" : "mobile";
	const css = {
		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;}`,
		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;}`
	};
	GM_addStyle(css[device]);

	if (device === "pc") {
		function otherCss(dom, index = 1) {
			dom = dom instanceof jQuery ? dom : $(dom);
			dom.attr("data-res-id", index);
			$(`<div class="result-content whole-row-item radius-solid-border text-center">当前页:第${index}页</div>`).insertBefore(dom.children(".result-content[data-i=0]"))
			dom.children(".result-content:not([data-i])").each((i, e) => {
				e = $(e);
				if (e.children("[shared]")[0]) {
					e.addClass("whole-row-item text-center margin-top-24-important");
					if (/下一页|上一页/.test(e.text())) {
						e.find("a[data-search]").each((i, a) => {
							a = $(a);
							if (a.text() === "下一页") {
								a.data("targetID", index + 1)
							} else if (a.text() === "上一页") {
								a.data("targetID", (index - 1) < 1 ? 1 : (index - 1))
							} else {
								a.data("targetID", a.text())
							}
							a.click((e) => {
								e.preventDefault()
								navJump(a.data("targetID"));
							})
						})
					} else {
						e.hide();
					}
				}
			})
			dom.children(".result-content[data-i]:last").addClass("whole-row-item").hide();
			dom.find(".result-content[data-i] a").each((i, e) => {
				if (e.href.indexOf("/search/jump?url=") !== -1) {
					var url = new URL(e.href).searchParams.get("url");
					console.log("重定向: " + e.href + " --> " + url)
					e.href = url;
				}
			})
		}

		function navJump(id) {
			id = typeof id === "number" ? id : parseInt(id);
			if (!$(".s-result-list").is(":visible") || !id) {
				return false
			}
			if ($(".s-result-list").length < id) {
				autoNextPage(".s-result-list:last", () => {
					$("html,body").stop(true);
					$("html,body").animate({
						scrollTop: $(`.s-result-list:last`).offset().top - 85
					}, 500, () => {
						navJump(id);
					});
				})
			} else if ($(`.s-result-list[data-res-id=${id}]`).is(":visible")) {
				$("html,body").stop(true);
				$("html,body").animate({
					scrollTop: $(`.s-result-list[data-res-id=${id}]`).offset().top - 85
				}, 500);
			}
		}

		function autoNextPage(dom, fn) {
			dom = dom instanceof jQuery ? dom : $(dom);
			dom.find(".result-content:not([data-i]).whole-row-item a").each((i, e) => {
				if (e.innerText === "下一页" && /^https?:\/\//i.test(e.href)) {
					$.get(e.href, (r) => {
						r = $(r).find(".s-result-list");
						if (r.length === 1) {
							if (!$(`.s-result-list[data-res-id=${$(e).data("targetID")}]`)[0]) {
								otherCss(r, $(e).data("targetID"));
								r.css("padding-top", "0");
								r.find(".result-content:first-child").hide();
								$(".main").append(r);
								dom.animate({
									paddingBottom: 0
								}, "fast");
								//dom.css("", "0")
							}
							typeof fn === "function" && fn();
						}
					})
					return false
				}
			})
		}
		$(".s-result-list").ready(() => {
			otherCss(".s-result-list");
			var loadFlag = true;
			$(document).scroll(function() {
				if (loadFlag && $(window).scrollTop() + $(window).height() > $(document).height() -
					50) {
					loadFlag = false;
					autoNextPage(".s-result-list:last", () => {
						setTimeout(() => {
							loadFlag = true;
						}, 500)
					})
				}
			});
		});
	} else {
		$("#head-bar form").ready(() => {
			var ghostBar = $("#bottom-bar form").clone();
			ghostBar.find("a").hide();
			ghostBar.find("input").attr("readonly", "readonly");
			ghostBar.addClass("ghost-search-bar");
			ghostBar.attr("isAnimate", "");
			ghostBar.click(()=>{
				$("#page-bottom input").click();
			})
			ghostBar.hide();
			ghostBar.extend({
				"display": function(needShow = true, animateSpan = 100) {
					var that = this;
					if (that.data("animate")) {
						return false
					}
					if (needShow && !that.is(":visible")) {
						that.data("animate", "animate");
						that.css("opacity", "0");
						that.show();
						that.animate({
							opacity: 1
						}, animateSpan, () => {
							that.css("opacity", "");
							that.data("animate", "");
						});
					} else if (!needShow && that.is(":visible")) {
						that.data("animate", "animate");
						that.css("opacity", "1");
						that.animate({
							opacity: 0
						}, animateSpan, () => {
							that.hide();
							that.css("opacity", "");
							that.data("animate", "");
						});
					}
				}
			})
			$("#page-main").append(ghostBar);
			function changeGhostDisplay() {
				var scroll = $(window).scrollTop(),
					scrollMin = $("#page-head").height(),
					scrollMax = $("#page-bottom input").offset().top - $(window).height();
				if (scroll >= scrollMin && scroll <= scrollMax) {
					ghostBar.display(true, 500);
				} else {
					ghostBar.display(false, 200);
				}
			}
			changeGhostDisplay();
			$(document).scroll(function() {
				changeGhostDisplay()
			});
			$("#results .result-content a").each((i, e) => {
				if (e.href.indexOf("/search/jump?url=") !== -1) {
					var url = new URL(e.href).searchParams.get("url");
					console.log("重定向: " + e.href + " --> " + url)
					e.href = url;
				}
			})
		})
	}
})();