// ==UserScript== // @name 素问搜索助手 // @namespace http://tampermonkey.net/ // @version 1.0.1 // @description 为爱发电、知乎文章添加跳转素问按钮 // @author You // @match https://www.zhihu.com/question/*/answer/* // @match https://sooon.ai/home/read/** // @match https://afdian.com/p/* // @grant GM_log // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/532096/%E7%B4%A0%E9%97%AE%E6%90%9C%E7%B4%A2%E5%8A%A9%E6%89%8B.user.js // @updateURL https://update.greasyfork.icu/scripts/532096/%E7%B4%A0%E9%97%AE%E6%90%9C%E7%B4%A2%E5%8A%A9%E6%89%8B.meta.js // ==/UserScript== (function() { 'use strict'; // Utility functions async function timeSleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function queryEle(selector, all = false) { const maxAttempts = 10; const baseDelay = 100; let totalWaitTime = 0; for (let attempt = 1; attempt <= maxAttempts; attempt++) { const ele = all ? document.querySelectorAll(selector) : document.querySelector(selector); if (ele && (!all || ele.length > 0)) { return ele; } const delay = Math.min( baseDelay * Math.pow(2, attempt - 1) + Math.random() * 100, 2000 ); totalWaitTime += delay; if (totalWaitTime >= 10000) { break; } await new Promise(resolve => setTimeout(resolve, delay)); } return null; } // Zhihu specific functionality function handleZhihuPage() { function extractTag(element) { if (!element) return "知乎回答"; const firstParagraph = element.querySelector('p[data-first-child], p:first-child'); let text = firstParagraph ? firstParagraph.textContent.trim() : element.textContent.trim(); const tagMatch = text.match(/#([^#]+)#/); if (tagMatch) { return tagMatch[1].trim(); } return "知乎回答"; } function addSuwenRedirectButton() { const existingBtn = document.querySelector('.go-sooon-link'); if (existingBtn) return; const contentElement = document.querySelector('#root > div > main > div > div > div.Question-main > div.ListShortcut > div > div.Card.AnswerCard.css-0 > div > div > div > div.RichContent.RichContent--unescapable'); const firstParagraph = contentElement.querySelector('p[data-first-child], p:first-child'); const tag = extractTag(contentElement); if (tag === "知乎回答") return; const button = document.createElement('button'); button.className = 'go-sooon-link'; button.textContent = '跳转素问'; button.addEventListener('click', () => { window.location.href = `https://sooon.ai/home/read/feed?search=${encodeURIComponent(tag)}`; }); firstParagraph.parentNode.insertBefore(button, firstParagraph.nextSibling); } function observePageChanges() { const observer = new MutationObserver(() => { const contentElement = document.querySelector('#root > div > main > div > div > div.Question-main > div.ListShortcut > div > div.Card.AnswerCard.css-0 > div > div > div > div.RichContent.RichContent--unescapable'); if (!contentElement) return; const firstParagraph = contentElement.querySelector('p[data-first-child], p:first-child'); if (firstParagraph && (!firstParagraph.nextElementSibling || !firstParagraph.nextElementSibling.classList.contains('copy-md-link'))) { addSuwenRedirectButton(); } }); observer.observe(document.body, { childList: true, subtree: true }); } observePageChanges(); } // Afdian specific functionality function handleAfdianPage() { async function addGoSooonLink() { await timeSleep(600); let textEle = document.querySelector("div.feed-content.mt16.post-page.unlock>pre"); if (!textEle) { textEle = await queryEle("article > p"); } let text = textEle?.textContent.trim(); const tagMatch = text?.match(/#(.+)#/); const tagText = tagMatch ? tagMatch[1] : "untagged"; const existingBtn = document.querySelector('.go-sooon-link'); if (existingBtn) return; if (tagText === "untagged") return; const goSooonLink = document.createElement('button'); goSooonLink.textContent = '跳转素问'; goSooonLink.className = 'go-sooon-link'; goSooonLink.style.cssText = ` font-size: .75em; color: #946CE6; border: 1px solid #946CE6; `; goSooonLink.addEventListener('click', () => { window.location.href = `https://sooon.ai/home/read/feed?search=${encodeURIComponent(tagText)}`; }); const parent = document.querySelector(".title-box"); parent.appendChild(goSooonLink); } addGoSooonLink(); window.addEventListener('resize', addGoSooonLink); } // Soon.ai specific functionality async function handleSoonPage() { const queryParams = new URLSearchParams(window.location.search); const searchQuery = queryParams.get('search'); if (!searchQuery) return; const searchBtn = await queryEle('button[aria-label="快速搜索"]'); if (!searchBtn) return; searchBtn.click(); const input = await queryEle('input[name="input"]'); if (!input) return; input.value = searchQuery; const submitBtn = await queryEle('button[type="submit"]'); if (!submitBtn) return; submitBtn.focus(); submitBtn.click(); await timeSleep(1200); const titles = await queryEle('form div.mantine-focus-never button span.inline-block', true); if (titles && titles.length > 0) { let target = titles[0].parentElement; target.focus(); target.click(); } } // Main router function init() { const url = window.location.href; const style = document.createElement('style'); style.textContent = ` .go-sooon-link { background: none; font-size: 1em; font-weight: 500; color: #0066FF; border: 1px solid #228BE6; padding: 2px 8px; margin-left: 10px; border-radius: 3px; cursor: pointer; display: inline-block; vertical-align: middle; } .go-sooon-link:hover { opacity: 0.8; color: gray; border-color: gray; } `; document.head.appendChild(style); if (url.includes('zhihu.com')) { handleZhihuPage(); } else if (url.includes('afdian.com')) { handleAfdianPage(); } else if (url.includes('sooon.ai')) { handleSoonPage(); } } if (document.readyState === 'complete') { init(); } else { window.addEventListener('load', init); } })();