// ==UserScript== // @name DeepSeek一键回顶 // @namespace https://scriptcat.org/zh-CN/users/214553 // @version 1.1.0 // @description 在DeepSeek网页上添加「回到顶部」按钮,一键回到对话的最开始。 // @author English3(Σπg|!εh๑๑๑) // @license MIT // @icon https://deepseek.com/favicon.ico // @match https://chat.deepseek.com/* // @run-at document-idle // @grant none // ==/UserScript== (() => { 'use strict'; const ID = 'ds-back-top-v4'; const goTop = () => { try { // 找到所有可能的主滚动容器 let bestEl = null; let maxScrollHeight = 0; document.querySelectorAll('div, main, section').forEach(el => { const style = window.getComputedStyle(el); if ((style.overflowY === 'auto' || style.overflowY === 'scroll') && el.scrollHeight > maxScrollHeight && el.clientHeight > 100) { maxScrollHeight = el.scrollHeight; bestEl = el; } }); if (bestEl) { // 聚焦该容器,确保键盘事件能作用在这里 bestEl.setAttribute('tabindex', '-1'); bestEl.focus(); // 模拟按下 Home 键,触发虚拟滚动加载最早期内容 const homeEvent = new KeyboardEvent('keydown', { key: 'Home', code: 'Home', keyCode: 36, which: 36, bubbles: true, cancelable: true }); bestEl.dispatchEvent(homeEvent); // 强制无缝滚动到顶 setTimeout(() => { bestEl.scrollTo({ top: 0, behavior: 'smooth' }); }, 50); } else { // 全局兜底 window.scrollTo({ top: 0, behavior: 'smooth' }); document.documentElement.scrollTop = 0; } } catch (e) { console.warn('DeepSeek回顶脚本执行异常:', e); } }; // 创建悬浮按钮 const btn = document.createElement('button'); btn.id = ID; btn.type = 'button'; btn.title = '回到最早的对话'; btn.setAttribute('aria-label', '回到最早的对话'); btn.innerHTML = ` `; // 强制固定悬浮样式,使用最高层级防遮挡 Object.assign(btn.style, { position: 'fixed', right: '20px', bottom: '100px', width: '48px', height: '48px', borderRadius: '50%', background: 'rgba(0, 0, 0, 0.7)', color: '#ffffff', border: '2px solid rgba(255, 255, 255, 0.2)', display: 'flex', alignItems: 'center', justifyContent: 'center', cursor: 'pointer', zIndex: '9999999', boxShadow: '0 4px 15px rgba(0,0,0,0.4)', opacity: '0.8', transition: 'all 0.2s ease', padding: '0', margin: '0' }); // 悬浮动画 btn.onmouseenter = () => { btn.style.opacity = '1'; btn.style.transform = 'scale(1.1)'; btn.style.background = 'rgba(0, 0, 0, 0.9)'; }; btn.onmouseleave = () => { btn.style.opacity = '0.8'; btn.style.transform = 'scale(1)'; btn.style.background = 'rgba(0, 0, 0, 0.7)'; }; // 点击事件 btn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); goTop(); }); // 核心防丢失机制:定时检查并挂载 const ensureMount = () => { if (!document.body) return; if (!document.getElementById(ID)) { document.body.appendChild(btn); } }; // 初始挂载 ensureMount(); // 使用定时器,每 1.5 秒检查一次按钮是否存活,彻底防止被页面重绘消失 setInterval(ensureMount, 1500); // 延迟挂载,应对页面初始加载慢 setTimeout(ensureMount, 500); setTimeout(ensureMount, 2000); })();