// ==UserScript== // @name GitHub To DeepWiKi // @name:zh-CN GitHub 跳转到 DeepWiKi // @description GitHub to DeepWiKi.Supports desktop and mobile devices // @description:zh-CN GitHub 存储库跳转到 DeepWiKi,支持电脑桌面端和移动端 // @author 人民的勤务员 // @namespace https://github.com/ChinaGodMan/UserScripts // @supportURL https://github.com/ChinaGodMan/UserScripts/issues // @homepageURL https://github.com/ChinaGodMan/UserScripts // @license MIT // @match https://github.com/* // @icon https://raw.githubusercontent.com/ChinaGodMan/UserScriptsHistory/main/scriptsIcon/deepwiki.png // @compatible chrome // @compatible firefox // @compatible edge // @compatible opera // @compatible safari // @compatible kiwi // @compatible qq // @compatible via // @compatible brave // @version 2025.5.17.1 // @downloadURL none // ==/UserScript== const DESKTOP_SELECTOR = '.mt-2' const MOBILE_SELECTOR = '#responsive-meta-container .d-flex.gap-2.mt-n3.mb-3.flex-wrap' const INITIAL_DELAY = 50 const MAX_DELAY = 2000 const MAX_ATTEMPTS = 15 let checkTimer = null let currentDelay = INITIAL_DELAY let attemptCount = 0 function checkAndInsert() { if (checkTimer) { clearTimeout(checkTimer) checkTimer = null } insertCustomElement(document.querySelector(MOBILE_SELECTOR), 'beforeend') const elements = document.querySelectorAll(DESKTOP_SELECTOR) if (elements.length >= 2) { const target = elements[elements.length - 2] if (insertCustomElement(target)) { console.log('插入成功,终止检测') return true } } if (attemptCount >= MAX_ATTEMPTS) { console.warn(`已达到最大尝试次数 ${MAX_ATTEMPTS} 次`) return false } currentDelay = Math.min(currentDelay * 1.5, MAX_DELAY) attemptCount++ checkTimer = setTimeout(() => { checkAndInsert() }, currentDelay) return false } function insertCustomElement(target) { if (!target?.parentNode) return false try { const deepWikiSvg = '' const newDiv = document.createElement('div') let url = window.location.href.replace('github.com', 'deepwiki.com') newDiv.className = 'mt-2' newDiv.innerHTML = ` ${deepWikiSvg} DeepWiki ` target.insertAdjacentElement('afterend', newDiv) return document.contains(newDiv) } catch (error) { console.error('插入失败:', error) return false } } function observeUrlChanges(callback, delay = 10) { let lastUrl = location.href const observer = new MutationObserver(() => { const url = location.href if (url !== lastUrl) { lastUrl = url setTimeout(() => { callback() }, delay) } }) observer.observe(document, { subtree: true, childList: true }) return observer } observeUrlChanges(checkAndInsert) checkAndInsert()