// ==UserScript== // @name ChatSPSS Optimization // @namespace http://tampermonkey.net/ // @version 1.1 // @description 为chatspss.cn提供主题切换、侧边栏折叠和滚动条美化功能 // @author YourName // @match https://chatspss.cn/* // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // ==/UserScript== (function() { 'use strict'; // 配置常量 const CONFIG = { theme: { light: { sidebarBg: '#ffffff', sidebarText: '#000000' }, dark: { sidebarBg: '#1e293b', sidebarText: '#ffffff' } }, sidebar: { expandedWidth: '300px', collapsedWidth: '50px', saveKey: 'chatspss_sidebar_collapsed' }, scrollbar: { width: '4px', light: { track: 'rgba(241, 245, 249, 0.5)', thumb: 'rgba(148, 163, 184, 0.5)', thumbHover: 'rgba(148, 163, 184, 0.7)' }, dark: { track: 'rgba(30, 41, 59, 0.5)', thumb: 'rgba(100, 116, 139, 0.5)', thumbHover: 'rgba(100, 116, 139, 0.7)' } } }; // 添加全局样式 GM_addStyle(` /* 按钮样式 */ .theme-toggle-button { width: 64px; height: 64px; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 30px; background: transparent; border: 1px solid rgba(0, 0, 0, 0.2); transition: all 0.2s ease; position: absolute; } .dark-theme .theme-toggle-button { border-color: rgba(255, 255, 255, 0.3); } .theme-toggle-button:hover { transform: scale(1.1); } .sidebar-collapse-toggle-bottom { position: absolute; bottom: 15px; left: 0; right: 0; margin: auto; width: 40px; height: 40px; border-radius: 4px; background: rgba(255, 255, 255, 0.1); border: 1px solid rgba(255, 255, 255, 0.2); color: #fff; cursor: pointer; z-index: 1000; display: flex; align-items: center; justify-content: center; transition: all 0.3s ease; font-size: 16px; font-weight: bold; } .sidebar-collapse-toggle-bottom:hover { background: rgba(0, 0, 0, 0.2) !important; transform: scale(1.1) !important; } /* 主题样式 */ .light-theme aside.ant-layout-sider.slide-in-left, .light-theme aside.ant-layout-sider.slide-in-right { background: ${CONFIG.theme.light.sidebarBg} !important; box-shadow: rgba(0, 0, 0, 0.08) 2px 0px 12px !important; } .dark-theme aside.ant-layout-sider.slide-in-left, .dark-theme aside.ant-layout-sider.slide-in-right { background: ${CONFIG.theme.dark.sidebarBg} !important; } .light-theme .conversation-item, .light-theme .conversation-item:hover, .light-theme .conversation-item.selected, .light-theme .ant-list-item-meta-title { color: ${CONFIG.theme.light.sidebarText} !important; } .light-theme aside.ant-layout-sider.slide-in-left *, .light-theme aside.ant-layout-sider.slide-in-right * { color: ${CONFIG.theme.light.sidebarText} !important; } .dark-theme aside.ant-layout-sider.slide-in-left *, .dark-theme aside.ant-layout-sider.slide-in-right * { color: ${CONFIG.theme.dark.sidebarText} !important; } .light-theme .conversation-item { border-bottom: 1px solid rgba(0, 0, 0, 0.1) !important; } .light-theme .conversation-item:hover { background-color: rgba(0, 0, 0, 0.05) !important; } .light-theme .conversation-item.selected { background-color: rgba(0, 0, 0, 0.08) !important; } /* 滚动条美化 */ .custom-scrollbar::-webkit-scrollbar, [data-eusoft-scrollable-element]::-webkit-scrollbar, .ChatInterface_messageListArea__H5GSb::-webkit-scrollbar { width: ${CONFIG.scrollbar.width} !important; } .light-theme .custom-scrollbar::-webkit-scrollbar-track, .light-theme [data-eusoft-scrollable-element]::-webkit-scrollbar-track, .light-theme .ChatInterface_messageListArea__H5GSb::-webkit-scrollbar-track { background: ${CONFIG.scrollbar.light.track} !important; border-radius: 2px !important; } .light-theme .custom-scrollbar::-webkit-scrollbar-thumb, .light-theme [data-eusoft-scrollable-element]::-webkit-scrollbar-thumb, .light-theme .ChatInterface_messageListArea__H5GSb::-webkit-scrollbar-thumb { background: ${CONFIG.scrollbar.light.thumb} !important; border-radius: 2px !important; } .light-theme .custom-scrollbar::-webkit-scrollbar-thumb:hover, .light-theme [data-eusoft-scrollable-element]::-webkit-scrollbar-thumb:hover, .light-theme .ChatInterface_messageListArea__H5GSb::-webkit-scrollbar-thumb:hover { background: ${CONFIG.scrollbar.light.thumbHover} !important; } .dark-theme .custom-scrollbar::-webkit-scrollbar-track, .dark-theme [data-eusoft-scrollable-element]::-webkit-scrollbar-track, .dark-theme .ChatInterface_messageListArea__H5GSb::-webkit-scrollbar-track { background: ${CONFIG.scrollbar.dark.track} !important; border-radius: 2px !important; } .dark-theme .custom-scrollbar::-webkit-scrollbar-thumb, .dark-theme [data-eusoft-scrollable-element]::-webkit-scrollbar-thumb, .dark-theme .ChatInterface_messageListArea__H5GSb::-webkit-scrollbar-thumb { background: ${CONFIG.scrollbar.dark.thumb} !important; border-radius: 2px !important; } .dark-theme .custom-scrollbar::-webkit-scrollbar-thumb:hover, .dark-theme [data-eusoft-scrollable-element]::-webkit-scrollbar-thumb:hover, .dark-theme .ChatInterface_messageListArea__H5GSb::-webkit-scrollbar-thumb:hover { background: ${CONFIG.scrollbar.dark.thumbHover} !important; } /* 兼容Firefox */ .custom-scrollbar, [data-eusoft-scrollable-element], .ChatInterface_messageListArea__H5GSb { scrollbar-width: thin !important; } .light-theme .custom-scrollbar, .light-theme [data-eusoft-scrollable-element], .light-theme .ChatInterface_messageListArea__H5GSb { scrollbar-color: ${CONFIG.scrollbar.light.thumb} ${CONFIG.scrollbar.light.track} !important; } .dark-theme .custom-scrollbar, .dark-theme [data-eusoft-scrollable-element], .dark-theme .ChatInterface_messageListArea__H5GSb { scrollbar-color: ${CONFIG.scrollbar.dark.thumb} ${CONFIG.scrollbar.dark.track} !important; } /* 折叠状态样式 */ aside.ant-layout-sider.slide-in-left[data-collapsed="true"] { transition: all 0.3s ease !important; overflow: hidden !important; width: ${CONFIG.sidebar.collapsedWidth} !important; min-width: ${CONFIG.sidebar.collapsedWidth} !important; max-width: ${CONFIG.sidebar.collapsedWidth} !important; flex: 0 0 ${CONFIG.sidebar.collapsedWidth} !important; } aside.ant-layout-sider.slide-in-left[data-collapsed="true"] .ant-layout-sider-children { display: flex !important; flex-direction: column !important; height: 100% !important; width: 100% !important; align-items: center !important; } aside.ant-layout-sider.slide-in-left[data-collapsed="true"] button > span:not(.ant-btn-icon) { display: none !important; } aside.ant-layout-sider.slide-in-left[data-collapsed="true"] > div > div { align-items: center !important; padding: 0 !important; margin: 0 !important; } aside.ant-layout-sider.slide-in-left[data-collapsed="true"] > div > div > div:nth-child(2), aside.ant-layout-sider.slide-in-left[data-collapsed="true"] > div > div > div:nth-child(3) > div:nth-child(2), aside.ant-layout-sider.slide-in-left[data-collapsed="true"] > div > div > div:nth-child(4) > div:nth-child(2) { display: none !important; } aside.ant-layout-sider.slide-in-left[data-collapsed="true"] > div > div > div:nth-child(1), aside.ant-layout-sider.slide-in-left[data-collapsed="true"] > div > div > div:nth-child(3) > div:nth-child(1) { padding: 15px 0 !important; margin: 0 !important; text-align: center !important; border-bottom: 1px solid rgba(0, 0, 0, 0.1) !important; width: 100% !important; box-sizing: border-box !important; } aside.ant-layout-sider.slide-in-left[data-collapsed="true"] button { justify-content: center !important; width: 35px !important; height: 35px !important; margin: 0 auto !important; padding: 0 !important; flex-shrink: 0 !important; } `); // 等待元素加载 function waitForElement(selector, timeout = 10000) { return new Promise((resolve, reject) => { const startTime = Date.now(); const interval = setInterval(() => { const element = document.querySelector(selector); if (element) { clearInterval(interval); resolve(element); } else if (Date.now() - startTime > timeout) { clearInterval(interval); reject(new Error(`Element ${selector} not found within ${timeout}ms`)); } }, 100); }); } // 主题管理器 class ThemeManager { constructor() { this.currentTheme = GM_getValue('chatspss_theme', 'dark'); this.applyTheme(this.currentTheme); } applyTheme(theme) { document.documentElement.classList.remove('light-theme', 'dark-theme'); document.body.classList.remove('light-theme', 'dark-theme'); document.documentElement.classList.add(`${theme}-theme`); document.body.classList.add(`${theme}-theme`); this.currentTheme = theme; GM_setValue('chatspss_theme', theme); } toggleTheme() { this.applyTheme(this.currentTheme === 'dark' ? 'light' : 'dark'); } } // 侧边栏管理器 class SidebarManager { constructor() { this.isCollapsed = GM_getValue(CONFIG.sidebar.saveKey, false); this.sidebar = null; this.init(); } async init() { try { this.sidebar = await waitForElement('aside.ant-layout-sider.slide-in-left'); this.applyState(); } catch (error) { console.error('Failed to initialize sidebar:', error); } } applyState() { if (!this.sidebar) return; if (this.isCollapsed) { this.sidebar.setAttribute('data-collapsed', 'true'); } else { this.sidebar.removeAttribute('data-collapsed'); } GM_setValue(CONFIG.sidebar.saveKey, this.isCollapsed); } toggle() { this.isCollapsed = !this.isCollapsed; this.applyState(); return this.isCollapsed; } } // 应用滚动条样式到元素 function applyScrollbarStyles() { const elements = [ 'aside.ant-layout-sider.slide-in-left > div > div > div:nth-child(4) > div:nth-child(2) > div', 'aside.ant-layout-sider.slide-in-right > div > div > div.custom-scrollbar', 'div.ant-layout.site-layout > main > div > div > div.ChatInterface_messageListArea__H5GSb' ]; elements.forEach(selector => { const element = document.querySelector(selector); if (element && !element.classList.contains('custom-scrollbar')) { element.classList.add('custom-scrollbar'); } }); } // 初始化 async function init() { const themeManager = new ThemeManager(); const sidebarManager = new SidebarManager(); try { const [headerSpan, leftSidebar] = await Promise.all([ waitForElement('div.ant-layout.site-layout > header > div > div:nth-child(1) > span'), waitForElement('aside.ant-layout-sider.slide-in-left') ]); // 创建主题切换按钮 const themeBtn = document.createElement('button'); themeBtn.className = 'theme-toggle-button'; themeBtn.textContent = '◨'; themeBtn.title = '切换主题'; const headerRect = headerSpan.closest('header').getBoundingClientRect(); const targetRect = headerSpan.getBoundingClientRect(); themeBtn.style.left = `${targetRect.right - headerRect.left + 10}px`; themeBtn.style.top = `${targetRect.top - headerRect.top}px`; headerSpan.parentNode.appendChild(themeBtn); themeBtn.addEventListener('click', () => themeManager.toggleTheme()); // 创建侧边栏折叠按钮 const sidebarBtn = document.createElement('button'); sidebarBtn.className = 'sidebar-collapse-toggle-bottom'; sidebarBtn.innerHTML = '◫'; sidebarBtn.title = '折叠侧边栏'; sidebarBtn.style.transform = sidebarManager.isCollapsed ? 'rotate(90deg)' : 'rotate(0deg)'; leftSidebar.style.position = 'relative'; leftSidebar.appendChild(sidebarBtn); sidebarBtn.addEventListener('click', (e) => { e.stopPropagation(); const isCollapsed = sidebarManager.toggle(); sidebarBtn.style.transform = isCollapsed ? 'rotate(90deg)' : 'rotate(0deg)'; }); // 应用滚动条样式 applyScrollbarStyles(); new MutationObserver(() => applyScrollbarStyles()) .observe(document.body, { childList: true, subtree: true }); console.log('ChatSPSS优化脚本加载成功'); } catch (error) { console.error('初始化失败:', error); } } // 启动 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();