// ==UserScript== // @name tophub.today 侧边栏 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.5 // @description 添加左侧侧边栏,点击标签控制页面滚动 // @author You // @match https://tophub.today/ // @grant none // ==/UserScript== (function() { 'use strict'; // 定义21个目标标签和位置计算 const categories = [ '推荐', '综合', '科技', '娱乐', '社区', '购物', '财经', '大学', '日报', '地方门户', '影视', '阅读', '游戏', '体育', '产品', '开发', '设计', '应用', '汽车', '安全',' ' ]; // 生成滚动目标对象(位置 = 索引 * 512.78) const scrollTargets = categories.reduce((acc, category, index) => { acc[category] = index * 512.78; return acc; }, {}); // 创建侧边栏容器 const sidebar = document.createElement('div'); sidebar.id = 'scroll-sidebar'; sidebar.style.cssText = ` position: fixed !important; top: 64px !important; left: 0 !important; width: 130px !important; height: calc(100vh - 64px) !important; background: rgba(255,255,255,0.98) !important; display: flex !important; flex-direction: column !important; padding: 12px 0 !important; z-index: 99999 !important; overflow-y: auto !important; `; // 创建导航按钮 categories.forEach(category => { const btn = document.createElement('button'); btn.dataset.target = category; btn.textContent = category; btn.style.cssText = ` flex: 0 0 auto; width: 100%; padding: 12px 0; border: none; border-bottom: 1px solid #f0f0f0; cursor: pointer; background: transparent !important; text-align: center; font-size: 14px; transition: background 0.3s; `; btn.addEventListener('click', () => handleScroll(category)); sidebar.appendChild(btn); }); // 添加空白元素以控制底部间距 const bottomSpacing = document.createElement('div'); bottomSpacing.style.height = '20px'; // 可根据需要调整此值来控制底部间距 sidebar.appendChild(bottomSpacing); // 插入侧边栏 document.body.appendChild(sidebar); // 滚动处理函数 function handleScroll(target) { window.scrollTo({ top: scrollTargets[target], behavior: 'smooth' }); } // 鼠标交互样式 sidebar.addEventListener('mouseover', e => { if (e.target.tagName === 'BUTTON') { e.target.style.background = '#f8f9fa !important'; } }); sidebar.addEventListener('mouseout', e => { if (e.target.tagName === 'BUTTON') { e.target.style.background = 'transparent !important'; } }); })();