// ==UserScript== // @name dcms论坛增强插件 // @namespace http://3g.cx/ // @version 2.3 // @description 为dcms论坛提供多种增强功能,提升用户体验 // @author 42hz,pulana // @match https://3g.cx/* // @match http://3g.cx/* // @grant GM_addStyle // @grant GM_getValue // @grant GM_setValue // @grant GM_deleteValue // @grant GM_listValues // @grant GM_xmlhttpRequest // @grant GM_registerMenuCommand // @run-at document-start // @require https://code.jquery.com/jquery-3.6.0.min.js // ==/UserScript== (function() { 'use strict'; // 检查jQuery是否已加载 function waitForJQuery() { if (typeof $ !== 'undefined') { initialize(); } else { setTimeout(waitForJQuery, 100); } } // 初始化脚本 function initialize() { // 添加全局样式 addGlobalStyles(); // 初始化数据结构 initializeStorage(); // 添加导航栏快捷功能 addQuickFeatures(); // 添加论坛页面增强功能 if (window.location.href.includes('/forum/')) { enhanceForum(); } // 添加聊天室增强功能 if (window.location.href.includes('/chat/')) { enhanceChat(); } // 添加日记页面增强功能 if (window.location.href.includes('/plugins/notes/')) { enhanceNotes(); } // 添加留言板增强功能 if (window.location.href.includes('/guest/')) { enhanceGuestbook(); } // 添加用户页面增强功能 if (window.location.href.includes('/user/info.php')) { enhanceUserProfile(); } // 添加快速搜索功能 addQuickSearch(); // 添加夜间模式切换 addNightMode(); // 添加一键清空浏览历史功能 addClearHistoryButton(); // 添加AI助手功能 addAIAssistant(); // 添加页面加载完成后的处理 $(document).ready(function() { // 应用已保存的设置 applySavedSettings(); // 应用已保存的用户过滤 applyUserFilters(); // 添加页面特定功能 addPageSpecificFeatures(); }); } // 初始化存储数据 function initializeStorage() { // 初始化默认设置 if (GM_getValue('settings', null) === null) { GM_setValue('settings', { nightMode: false, autoRefresh: false, refreshInterval: 30, blockedUsers: [], aiChatHistory: [], autoExpandPosts: false, highlightOwnPosts: false, autoHideOldMessages: false, highlightOwnMessages: true }); } } // 获取设置 function getSettings() { return GM_getValue('settings', { nightMode: false, autoRefresh: false, refreshInterval: 30, blockedUsers: [], aiChatHistory: [], autoExpandPosts: false, highlightOwnPosts: false, autoHideOldMessages: false, highlightOwnMessages: true }); } // 保存设置 function saveSettings(settings) { GM_setValue('settings', settings); } // 添加全局样式 function addGlobalStyles() { GM_addStyle(` /* 全局增强样式 */ .gm-enhancement-panel { background: #f0f0f0; border: 1px solid #ccc; padding: 10px; margin: 10px 0; border-radius: 5px; } .gm-btn { background: #007cba; color: white; border: none; padding: 5px 10px; margin: 0 2px; border-radius: 3px; cursor: pointer; font-size: 12px; } .gm-btn:hover { background: #005a87; } .gm-btn-danger { background: #d9534f; } .gm-btn-danger:hover { background: #c9302c; } .gm-btn-success { background: #5cb85c; } .gm-btn-success:hover { background: #449d44; } .gm-btn-warning { background: #f0ad4e; } .gm-btn-warning:hover { background: #ec971f; } .gm-night-mode { background-color: #2c2c2c !important; color: #e0e0e0 !important; } .gm-night-mode a { color: #64b5f6 !important; } .gm-night-mode input, .gm-night-mode textarea { background-color: #424242 !important; color: #e0e0e0 !important; border: 1px solid #666 !important; } .gm-night-mode table { border-color: #666 !important; } .gm-night-mode .gm-enhancement-panel { background: #3c3c3c; border-color: #666; } .gm-quick-actions { position: fixed; bottom: 20px; right: 20px; z-index: 9999; } .gm-quick-action-btn { display: block; width: 40px; height: 40px; background: #007cba; color: white; border-radius: 50%; text-align: center; line-height: 40px; margin: 5px 0; text-decoration: none; box-shadow: 0 2px 10px rgba(0,0,0,0.3); cursor: pointer; } .gm-quick-action-btn:hover { background: #005a87; } .gm-search-box { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 10000; background: white; padding: 15px; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.3); display: none; width: 90%; max-width: 500px; max-height: 80vh; overflow-y: auto; } .gm-night-mode .gm-search-box { background: #3c3c3c; color: #e0e0e0; } .gm-hidden-content { display: none !important; } .gm-highlighted { background-color: #e6f3ff !important; border: 2px solid #007cba !important; } .gm-night-mode .gm-highlighted { background-color: #1a237e !important; border: 2px solid #64b5f6 !important; } .gm-ai-assistant { position: fixed; bottom: 20px; left: 20px; z-index: 9999; width: 300px; background: white; border: 1px solid #ccc; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.3); display: none; } .gm-night-mode .gm-ai-assistant { background: #3c3c3c; border-color: #666; } .gm-ai-header { background: #007cba; color: white; padding: 10px; border-radius: 8px 8px 0 0; cursor: move; } .gm-night-mode .gm-ai-header { background: #005a87; } .gm-ai-content { padding: 10px; max-height: 200px; overflow-y: auto; } .gm-ai-input { width: calc(100% - 20px); padding: 5px; margin: 10px 0; } .gm-ai-message { margin: 5px 0; padding: 5px; border-radius: 5px; } .gm-ai-user { background: #e6f3ff; text-align: right; } .gm-night-mode .gm-ai-user { background: #1a237e; } .gm-ai-assistant-response { background: #f0f0f0; } .gm-night-mode .gm-ai-assistant-response { background: #424242; } .gm-ai-typing { font-style: italic; color: #888; } .gm-night-mode .gm-ai-typing { color: #aaa; } .gm-api-error { color: #d9534f; font-weight: bold; } .gm-api-success { color: #5cb85c; font-weight: bold; } .gm-search-highlight { background-color: yellow !important; font-weight: bold; } `); } // 添加导航栏快捷功能 function addQuickFeatures() { $(document).ready(function() { // 添加快速导航按钮 if ($('.gm-enhancement-panel').length === 0) { const quickPanel = `
DCMS增强功能:
`; $('body').prepend(quickPanel); // 绑定快速导航事件 $('#gm-go-home').click(function() { window.location.href = '/'; }); $('#gm-go-forum').click(function() { window.location.href = '/forum/'; }); $('#gm-go-chat').click(function() { window.location.href = '/chat/'; }); $('#gm-go-notes').click(function() { window.location.href = '/plugins/notes/'; }); $('#gm-go-guest').click(function() { window.location.href = '/guest/'; }); $('#gm-go-downloads').click(function() { window.location.href = '/down/'; }); $('#gm-go-users').click(function() { window.location.href = '/user/users.php'; }); $('#gm-go-night-mode').click(function() { toggleNightMode(); }); $('#gm-go-settings').click(function() { showSettingsPanel(); }); $('#gm-go-ai-assistant').click(function() { toggleAIAssistant(); }); } }); } // 增强论坛页面功能 function enhanceForum() { $(document).ready(function() { const settings = getSettings(); // 添加快速发帖按钮 const quickPostBtn = `
`; // 找到合适的插入位置 const container = $('body').find('table').first(); if (container.length > 0) { container.before(quickPostBtn); } else { $('body').prepend(quickPostBtn); } // 绑定事件 $('#gm-new-topic').click(function() { window.location.href = '/forum/new_t.php'; }); $('#gm-expand-all-posts').click(function() { // 展开所有帖子内容(模拟点击"»"链接) $('a:contains("»")').each(function() { if (!$(this).hasClass('gm-expanded')) { $(this).addClass('gm-expanded'); // 显示可能被隐藏的内容 $(this).closest('div').nextAll().show(); } }); }); $('#gm-collapse-all-posts').click(function() { $('.gm-expanded').removeClass('gm-expanded'); $('a:contains("»")').closest('div').nextAll().hide(); }); $('#gm-filter-by-user').click(function() { const username = $('#gm-user-filter-input').val(); if (username) { filterPostsByUser(username); } }); $('#gm-highlight-own').click(function() { settings.highlightOwnPosts = !settings.highlightOwnPosts; saveSettings(settings); if (settings.highlightOwnPosts) { $(this).addClass('gm-btn-success'); highlightOwnPosts(); } else { $(this).removeClass('gm-btn-success'); $('.gm-highlighted').removeClass('gm-highlighted'); } }); // 添加帖子列表增强功能 addForumListEnhancements(); // 如果设置了自动高亮,应用它 if (settings.highlightOwnPosts) { setTimeout(highlightOwnPosts, 500); // 延迟执行以确保内容已加载 } }); } // 高亮自己的帖子 function highlightOwnPosts() { const username = getCurrentUsername(); if (username) { // 查找所有包含用户名的元素,并高亮其最近的容器 $('a, span, div, p').each(function() { if ($(this).text().includes(username) && !$(this).parents('.gm-hidden-content').length) { const container = $(this).closest('div, table, tr, p').first(); if (container.length > 0) { container.addClass('gm-highlighted'); } else { $(this).addClass('gm-highlighted'); } } }); } } // 获取当前用户名 function getCurrentUsername() { // 检查页面中可能包含用户名的元素 const usernamePatterns = [ // 检查退出按钮附近的用户名 $('a:contains("退出")').siblings('a').first(), // 检查顶部用户链接 $('a[href*="/user/info.php"]').first(), // 检查页面顶部的其他用户相关信息 $('.user-info a').first(), // 检查页面顶部导航 $('nav a[href*="/user/info.php"]').last() ]; for (let i = 0; i < usernamePatterns.length; i++) { if (usernamePatterns[i] && usernamePatterns[i].length > 0) { const text = usernamePatterns[i].text().trim(); if (text && text.length > 0 && text.length < 30 && !text.includes('用户') && !text.includes('退出') && !text.includes('登录') && !text.includes('注册')) { return text; } } } // 如果以上方法都没找到,尝试从URL参数中获取用户ID,然后猜测用户名 return null; } // 过滤特定用户的帖子 function filterPostsByUser(username) { // 先显示所有内容 $('.gm-hidden-content').removeClass('gm-hidden-content'); // 查找包含用户名的元素,并隐藏不包含用户名的相邻内容 $('body').find('a, span, div, p').each(function() { const element = $(this); const text = element.text(); if (text.includes(username)) { // 包含用户名的元素,确保其容器可见 const container = element.closest('div, table, tr, p'); container.removeClass('gm-hidden-content'); } }); // 隐藏不包含用户名的条目 $('body').find('div, table, tr, p').each(function() { const elem = $(this); if (!elem.text().includes(username) && elem.find('a:contains("' + username + '")').length === 0 && elem.text().length > 10) { // 避免隐藏太短的元素 // 检查是否已经有用户链接,如果没有则隐藏 let hasUserLink = false; elem.find('a').each(function() { if ($(this).attr('href') && $(this).attr('href').includes('/user/info.php')) { if ($(this).text().includes(username)) { hasUserLink = true; } } }); if (!hasUserLink) { elem.addClass('gm-hidden-content'); } } }); } // 增强论坛列表页面 function addForumListEnhancements() { $(document).ready(function() { // 添加一键复制帖子链接功能 $('a[href*="/forum/"]').each(function() { if ($(this).attr('href').match(/\/forum\/\d+\/\d+\/\d+\//)) { const link = $(this).attr('href'); const postId = extractPostId(link); if (postId) { const copyBtn = ``; $(this).after(copyBtn); } } }); // 绑定复制链接事件 $(document).on('click', '.gm-copy-link', function() { const link = $(this).data('link'); navigator.clipboard.writeText(window.location.origin + link); alert('链接已复制到剪贴板!'); }); }); } // 提取帖子ID function extractPostId(url) { const match = url.match(/\/forum\/\d+\/\d+\/(\d+)\//); return match ? match[1] : null; } // 增强聊天室功能 function enhanceChat() { $(document).ready(function() { const settings = getSettings(); // 添加聊天室增强功能 const chatEnhancement = `
聊天室增强功能:
`; $('body').prepend(chatEnhancement); // 绑定事件 $('#gm-chat-refresh').click(function() { location.reload(); }); $('#gm-chat-auto-refresh').click(function() { settings.autoRefresh = !settings.autoRefresh; saveSettings(settings); if (settings.autoRefresh) { $(this).addClass('gm-btn-success').text('停止刷新'); startAutoRefresh(); } else { $(this).removeClass('gm-btn-success').text('自动刷新'); stopAutoRefresh(); } }); $('#gm-chat-filter').click(function() { const keyword = $('#gm-chat-filter-input').val(); if (keyword) { filterChatMessages(keyword); } }); $('#gm-chat-highlight-own').click(function() { settings.highlightOwnMessages = !settings.highlightOwnMessages; saveSettings(settings); if (settings.highlightOwnMessages) { $(this).addClass('gm-btn-success'); highlightOwnChatMessages(); } else { $(this).removeClass('gm-btn-success'); $('.gm-highlighted').removeClass('gm-highlighted'); } }); $('#gm-chat-hide-old').click(function() { settings.autoHideOldMessages = !settings.autoHideOldMessages; saveSettings(settings); if (settings.autoHideOldMessages) { $(this).addClass('gm-btn-success'); // 隐藏较早的消息(保留最新的50条) const allMessages = $('body').find('a[href*="/chat/room/"]').closest('div, p'); if (allMessages.length > 50) { allMessages.slice(0, allMessages.length - 50).addClass('gm-hidden-content'); } } else { $(this).removeClass('gm-btn-success'); $('.gm-hidden-content').removeClass('gm-hidden-content'); } }); // 如果开启了自动刷新,启动定时器 if (settings.autoRefresh) { startAutoRefresh(); $('#gm-chat-auto-refresh').addClass('gm-btn-success').text('停止刷新'); } // 如果设置了自动高亮,应用它 if (settings.highlightOwnMessages) { setTimeout(highlightOwnChatMessages, 500); } }); } // 启动自动刷新 function startAutoRefresh() { const settings = getSettings(); stopAutoRefresh(); // 先停止之前的定时器 window.chatRefreshInterval = setInterval(function() { location.reload(); }, settings.refreshInterval * 1000); } // 停止自动刷新 function stopAutoRefresh() { if (window.chatRefreshInterval) { clearInterval(window.chatRefreshInterval); window.chatRefreshInterval = null; } } // 高亮聊天室中自己的消息 function highlightOwnChatMessages() { const username = getCurrentUsername(); if (username) { $('a, span, div, p').each(function() { if ($(this).text().includes(username) && !$(this).parents('.gm-hidden-content').length) { const container = $(this).closest('div, p').first(); if (container.length > 0) { container.addClass('gm-highlighted'); } else { $(this).addClass('gm-highlighted'); } } }); } } // 过滤聊天消息 function filterChatMessages(keyword) { // 先显示所有内容 $('.gm-hidden-content').removeClass('gm-hidden-content'); $('body').find('a, div, span, p').each(function() { if ($(this).text().toLowerCase().includes(keyword.toLowerCase())) { // 包含关键词的消息保留 $(this).closest('div, p').removeClass('gm-hidden-content'); } else { // 不包含关键词的消息隐藏 $(this).closest('div, p').addClass('gm-hidden-content'); } }); } // 增强日记页面功能 function enhanceNotes() { $(document).ready(function() { // 添加日记增强功能 const notesEnhancement = `
日记增强功能:
`; $('body').prepend(notesEnhancement); // 修正写日记按钮的链接 $('#gm-new-note').click(function() { window.location.href = '/plugins/notes/add.php'; // 修正URL }); // 绑定事件 $('#gm-notes-filter').click(function() { const user = $('#gm-notes-user-filter').val(); if (user) { filterNotesByUser(user); } }); $('#gm-notes-search').click(function() { const keyword = $('#gm-notes-search-input').val(); if (keyword) { searchNotes(keyword); } }); $('#gm-notes-highlight-own').click(function() { highlightOwnNotes(); }); }); } // 过滤指定用户的日记 function filterNotesByUser(username) { // 先显示所有内容 $('.gm-hidden-content').removeClass('gm-hidden-content'); // 为每个日志条目检查是否包含指定用户名 $('body').find('a').each(function() { const link = $(this); if (link.attr('href') && link.attr('href').includes('/user/info.php')) { if (link.text().includes(username)) { // 找到包含用户名的用户链接,显示其相关内容 const container = link.closest('div, p, table').first(); if (container.length > 0) { container.removeClass('gm-hidden-content'); } } else { // 不包含用户名的用户链接,检查是否需要隐藏 const container = link.closest('div, p, table').first(); if (container.length > 0) { container.addClass('gm-hidden-content'); } } } }); } // 搜索日记内容(页面内搜索) function searchNotes(keyword) { if (!keyword) return; // 先移除之前的高亮和隐藏 $('.gm-search-highlight').removeClass('gm-search-highlight'); $('.gm-hidden-content').removeClass('gm-hidden-content'); let foundAny = false; // 在页面中搜索关键词 $('body').find('a, div, p, span').each(function() { const element = $(this); const text = element.text(); if (text.toLowerCase().includes(keyword.toLowerCase())) { // 高亮包含关键词的元素 element.addClass('gm-search-highlight'); foundAny = true; // 确保包含关键词的容器可见 const container = element.closest('div, p, table'); if (container.length > 0) { container.removeClass('gm-hidden-content'); } } }); // 如果没有找到结果,显示提示 if (!foundAny) { alert(`未找到包含"${keyword}"的内容`); } } // 高亮自己的日记 function highlightOwnNotes() { const username = getCurrentUsername(); if (username) { $('body').find('a, span, div, p').each(function() { if ($(this).text().includes(username) && !$(this).parents('.gm-hidden-content').length && ($(this).closest('div, p').find('a[href*="/plugins/notes/list.php"]').length > 0 || $(this).closest('div, p').find('a[href*="/user/info.php"]').length > 0)) { const container = $(this).closest('div, p').first(); if (container.length > 0) { container.addClass('gm-highlighted'); } else { $(this).addClass('gm-highlighted'); } } }); } } // 增强留言板功能 function enhanceGuestbook() { $(document).ready(function() { const settings = getSettings(); // 添加留言板增强功能 const guestbookEnhancement = `
留言板增强功能:
`; $('body').prepend(guestbookEnhancement); // 绑定事件 $('#gm-guest-hide-replies').click(function() { hideOwnReplies(); }); $('#gm-guest-highlight').click(function() { settings.highlightOwnMessages = !settings.highlightOwnMessages; saveSettings(settings); if (settings.highlightOwnMessages) { $(this).addClass('gm-btn-success'); highlightOwnGuestbookMessages(); } else { $(this).removeClass('gm-btn-success'); $('.gm-highlighted').removeClass('gm-highlighted'); } }); $('#gm-guest-filter-users').click(function() { const user = $('#gm-guest-filter-user').val(); if (user) { filterGuestbookByUser(user); } }); $('#gm-guest-refresh').click(function() { location.reload(); }); // 如果启用了相关功能,立即应用 if (settings.highlightOwnMessages) { setTimeout(highlightOwnGuestbookMessages, 500); } }); } // 隐藏自己的回复 function hideOwnReplies() { const username = getCurrentUsername(); if (username) { $('body').find('a, span, div, p').each(function() { if ($(this).text().includes(username) && !$(this).parents('.gm-hidden-content').length) { // 找到包含用户留言的容器并隐藏 const container = $(this).closest('div, p').first(); if (container.length > 0) { container.addClass('gm-hidden-content'); } } }); } } // 高亮留言板中的自己的留言 function highlightOwnGuestbookMessages() { const username = getCurrentUsername(); if (username) { $('body').find('a, span, div, p').each(function() { if ($(this).text().includes(username) && !$(this).parents('.gm-hidden-content').length) { const container = $(this).closest('div, p').first(); if (container.length > 0) { container.addClass('gm-highlighted'); } else { $(this).addClass('gm-highlighted'); } } }); } } // 过滤留言板中的指定用户 function filterGuestbookByUser(username) { // 先显示所有内容 $('.gm-hidden-content').removeClass('gm-hidden-content'); let foundUserEntries = false; $('body').find('a').each(function() { const link = $(this); if (link.attr('href') && link.attr('href').includes('/user/info.php')) { if (link.text().includes(username)) { // 找到指定用户名,显示其留言 const container = link.closest('div, p, table').first(); if (container.length > 0) { container.removeClass('gm-hidden-content'); foundUserEntries = true; } } else { // 不是目标用户,隐藏其留言 const container = link.closest('div, p, table').first(); if (container.length > 0) { container.addClass('gm-hidden-content'); } } } }); if (!foundUserEntries) { alert(`未找到用户"${username}"的留言`); } } // 增强用户个人页面 function enhanceUserProfile() { $(document).ready(function() { // 添加用户页面增强功能 const profileEnhancement = `
用户页面增强功能:
`; $('body').prepend(profileEnhancement); // 绑定事件 $('#gm-profile-send-msg').click(function() { const userId = getParameterByName('id'); if (userId) { window.location.href = `/user/mail.php?id=${userId}`; } else { alert('无法获取用户ID'); } }); $('#gm-profile-block-user').click(function() { const userId = getParameterByName('id'); if (userId) { if (confirm('确定要屏蔽此用户吗?')) { // 保存到本地存储中 const settings = getSettings(); if (!settings.blockedUsers.includes(userId)) { settings.blockedUsers.push(userId); saveSettings(settings); alert('用户已屏蔽'); } else { alert('用户已在屏蔽列表中'); } } } else { alert('无法获取用户ID'); } }); }); } // 添加快速搜索功能 function addQuickSearch() { const searchBox = ` `; $('body').append(searchBox); // 添加搜索按钮 const searchBtn = ` 🔍 `; $('body').append(`
${searchBtn}
`); // 绑定事件 $('#gm-search-trigger').click(function() { $('#gm-quick-search').show(); $('#gm-search-input').focus(); }); $('#gm-search-close').click(function() { $('#gm-quick-search').hide(); }); $('#gm-search-submit').click(function() { const query = $('#gm-search-input').val(); const type = $('#gm-search-type').val(); if (query) { performSearch(query, type); } }); // 回车键搜索 $('#gm-search-input').keypress(function(e) { if (e.which === 13) { $('#gm-search-submit').click(); } }); } // 执行搜索 function performSearch(query, type) { const resultsDiv = $('#gm-search-results'); resultsDiv.html('

搜索中...

'); switch(type) { case 'forum': window.location.href = `/forum/search.php?search=${encodeURIComponent(query)}`; break; case 'notes': // 在当前页面搜索日记 resultsDiv.html(`

已在当前日记页面执行搜索,请使用页面上的搜索功能。

`); break; case 'users': window.location.href = `/user/users.php?search=${encodeURIComponent(query)}`; break; case 'guest': // 在当前页面搜索留言板 resultsDiv.html(`

已在当前留言板页面执行搜索,请使用页面上的搜索功能。

`); break; case 'ai': queryAI(query, (response) => { resultsDiv.html(`
${response}
`); }); break; } } // 添加夜间模式 function addNightMode() { // 检查是否已启用夜间模式 const settings = getSettings(); if (settings.nightMode) { toggleNightMode(true); } } // 切换夜间模式 function toggleNightMode(forceOn = null) { const settings = getSettings(); const isOn = forceOn !== null ? forceOn : !settings.nightMode; settings.nightMode = isOn; saveSettings(settings); if (isOn) { $('body').addClass('gm-night-mode'); } else { $('body').removeClass('gm-night-mode'); } } // 添加一键清空浏览历史功能 function addClearHistoryButton() { // 添加到快速操作面板 const clearBtn = ` 🗑️ `; $('.gm-quick-actions').append(clearBtn); $('#gm-clear-history').click(function() { if (confirm('确定要清空浏览历史记录吗?')) { // 清空GM值中的浏览历史相关数据 const keys = GM_listValues(); for (let i = 0; i < keys.length; i++) { if (keys[i].includes('history') || keys[i].includes('visited')) { GM_deleteValue(keys[i]); } } // 也清空设置中的浏览相关设置 const settings = getSettings(); settings.aiChatHistory = []; saveSettings(settings); alert('浏览历史已清空'); } }); } // 显示设置面板 function showSettingsPanel() { const settings = getSettings(); const settingsPanel = ` `; // 移除现有的设置面板(如果存在) $('#gm-settings-panel').remove(); $('body').append(settingsPanel); // 绑定事件 $('#gm-settings-save').click(function() { const newSettings = getSettings(); newSettings.nightMode = $('#gm-setting-night-mode').is(':checked'); newSettings.autoRefresh = $('#gm-setting-auto-refresh').is(':checked'); newSettings.refreshInterval = parseInt($('#gm-setting-refresh-interval').val()) || 30; newSettings.autoExpandPosts = $('#gm-setting-auto-expand').is(':checked'); newSettings.highlightOwnPosts = $('#gm-setting-highlight-own').is(':checked'); newSettings.autoHideOldMessages = $('#gm-setting-hide-old').is(':checked'); const blockedUsers = $('#gm-setting-blocked-users').val().split(',').map(id => id.trim()).filter(id => id); newSettings.blockedUsers = blockedUsers; saveSettings(newSettings); // 应用新的设置 if (newSettings.nightMode !== settings.nightMode) { toggleNightMode(newSettings.nightMode); } if (newSettings.autoRefresh !== settings.autoRefresh) { if (newSettings.autoRefresh) { startAutoRefresh(); } else { stopAutoRefresh(); } } alert('设置已保存,部分设置需刷新页面生效'); }); $('#gm-settings-reset').click(function() { if (confirm('确定要重置所有设置吗?')) { GM_deleteValue('settings'); initializeStorage(); alert('设置已重置,刷新页面生效'); location.reload(); } }); $('#gm-settings-close').click(function() { $('#gm-settings-panel').remove(); }); } // 应用已保存的设置 function applySavedSettings() { const settings = getSettings(); // 应用夜间模式 if (settings.nightMode) { $('body').addClass('gm-night-mode'); } // 应用自动刷新 if (settings.autoRefresh && window.location.href.includes('/chat/')) { startAutoRefresh(); } } // 应用用户过滤 function applyUserFilters() { const settings = getSettings(); if (settings.blockedUsers && settings.blockedUsers.length > 0) { // 根据不同页面应用过滤 if (window.location.href.includes('/guest/')) { // 过滤留言板中的屏蔽用户 settings.blockedUsers.forEach(userId => { $(`a[href*="/user/info.php?id=${userId}"]`).closest('div, p, table').addClass('gm-hidden-content'); }); } } } // 添加页面特定功能 function addPageSpecificFeatures() { // 在论坛帖子页面添加AI助手按钮 if (window.location.href.match(/\/forum\/\d+\/\d+\/\d+\//)) { const postContent = $('body').find('div, p').filter(function() { return $(this).text().length > 50; // 找到可能的帖子内容 }).first(); if (postContent.length > 0) { const aiAnalyzeBtn = ` `; postContent.before(aiAnalyzeBtn); $('#gm-ai-analyze-post').click(function() { const postText = postContent.text().substring(0, 500); // 限制长度 if (postText.length > 10) { queryAI(`请分析以下帖子内容:${postText}`, (response) => { showAIResponse(`AI分析结果:\n${response}`); }); } }); } } } // 添加AI助手功能 function addAIAssistant() { const aiAssistant = `
AI助手
您好!我是您的AI助手,可以回答关于DCMS论坛的问题,帮助您分析内容,或进行聊天。
`; $('body').append(aiAssistant); // 绑定AI助手事件 $('#gm-ai-send').click(function() { const question = $('#gm-ai-input').val().trim(); if (question) { addAIUserMessage(question); $('#gm-ai-input').val(''); queryAI(question, (response) => { addAIResponseMessage(response); // 保存到历史记录 const settings = getSettings(); settings.aiChatHistory.push({ question: question, response: response, timestamp: new Date().toISOString() }); saveSettings(settings); }); } }); $('#gm-ai-close').click(function() { $('#gm-ai-assistant').hide(); }); $('#gm-ai-clear').click(function() { $('#gm-ai-content').html('
对话已清空
'); // 清空历史记录 const settings = getSettings(); settings.aiChatHistory = []; saveSettings(settings); }); // 回车键发送 $('#gm-ai-input').keypress(function(e) { if (e.which === 13) { $('#gm-ai-send').click(); } }); // 拖拽功能 dragElement(document.getElementById('gm-ai-assistant'), document.querySelector('#gm-ai-assistant .gm-ai-header')); } // 显示AI响应 function showAIResponse(response) { const aiDiv = $('#gm-ai-assistant'); if (aiDiv.is(':hidden')) { aiDiv.show(); } addAIResponseMessage(response); } // 添加AI用户消息 function addAIUserMessage(message) { const contentDiv = $('#gm-ai-content'); const userMessage = `
您: ${message}
`; contentDiv.append(userMessage); contentDiv.scrollTop(contentDiv[0].scrollHeight); } // 添加AI响应消息 function addAIResponseMessage(message) { const contentDiv = $('#gm-ai-content'); const responseMessage = `
AI: ${message}
`; contentDiv.append(responseMessage); contentDiv.scrollTop(contentDiv[0].scrollHeight); } // AI聊天查询(使用讯飞AI API) function queryAI(question, callback) { // 使用讯飞AI API const apiUrl = 'https://xiaodi.jujukai.cn/Api/xfai.php'; const qqId = 'gm_user_' + (new Date().getTime()); // 创建唯一的记忆ID // 准备请求参数 const params = { msg: encodeURIComponent(question), qq: qqId, type: 'text' }; const queryString = Object.keys(params).map(key => `${key}=${params[key]}`).join('&'); const url = `${apiUrl}?${queryString}`; // 添加错误处理 GM_xmlhttpRequest({ method: 'GET', url: url, timeout: 15000, // 15秒超时 onload: function(response) { if (response.status === 200) { let result = response.responseText.trim(); // 尝试解析JSON,如果不是JSON则直接使用文本 try { const jsonResult = JSON.parse(result); if (jsonResult && jsonResult.content) { result = jsonResult.content; } } catch (e) { // 不是JSON格式,使用原始文本 } // 检查返回内容是否有效 if (result && result.length > 0 && !result.includes('error') && !result.includes('Error') && !result.includes('404')) { callback(result); } else { console.log('API返回错误内容:', result); callback('AI助手暂时无法响应:API返回错误内容'); } } else { console.log('API请求失败,状态码:', response.status); callback('AI助手暂时无法响应:HTTP错误 ' + response.status); } }, onerror: function(error) { console.error('AI API请求错误:', error); callback('AI助手暂时无法响应:网络请求失败'); }, ontimeout: function() { console.log('API请求超时'); callback('AI助手暂时无法响应:请求超时'); } }); } // 切换AI助手显示 function toggleAIAssistant() { const aiDiv = $('#gm-ai-assistant'); if (aiDiv.is(':hidden')) { aiDiv.show(); } else { aiDiv.hide(); } } // 拖拽元素功能 function dragElement(elmnt, header) { let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; if (header) { header.onmousedown = dragMouseDown; } else { elmnt.onmousedown = dragMouseDown; } function dragMouseDown(e) { e = e || window.event; e.preventDefault(); pos3 = e.clientX; pos4 = e.clientY; document.onmouseup = closeDragElement; document.onmousemove = elementDrag; } function elementDrag(e) { e = e || window.event; e.preventDefault(); pos1 = pos3 - e.clientX; pos2 = pos4 - e.clientY; pos3 = e.clientX; pos4 = e.clientY; elmnt.style.top = (elmnt.offsetTop - pos2) + "px"; elmnt.style.left = (elmnt.offsetLeft - pos1) + "px"; } function closeDragElement() { document.onmouseup = null; document.onmousemove = null; } } // 获取URL参数 function getParameterByName(name, url = window.location.href) { name = name.replace(/[\[\]]/g, '\\$&'); const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'); const results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, ' ')); } // 等待jQuery加载完成 waitForJQuery(); })();