// ==UserScript== // @name 抖音续火花自动发送助手-集成一言API和TXTAPI-支持多用户 // @namespace http://tampermonkey.net/ // @version 2.2 // @description 每天自动发送续火消息,支持自定义时间,集成一言API和TXTAPI,支持多目标用户 // @author 飔梦 / 阚泥 / xiaohe123awa // @match https://creator.douyin.com/creator-micro/data/following/chat // @icon https://free.picui.cn/free/2025/11/23/69226264aca4e.png // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @grant GM_notification // @grant GM_listValues // @grant GM_deleteValue // @grant GM_xmlhttpRequest // @connect hitokoto.cn // @connect self // ==/UserScript== (function() { 'use strict'; // 默认配置 const DEFAULT_CONFIG = { baseMessage: "续火", sendTime: "00:01:00", checkInterval: 1000, maxWaitTime: 30000, maxRetryCount: 3, hitokotoTimeout: 60000, txtApiTimeout: 60000, useHitokoto: true, useTxtApi: true, txtApiMode: "manual", txtApiManualRandom: true, customMessage: "—————每日续火—————\n\n[TXTAPI]\n\n—————每日一言—————\n\n[API]\n", hitokotoFormat: "{hitokoto}\n—— {from}{from_who}", fromFormat: "{from}", fromWhoFormat: "「{from_who}」", txtApiUrl: "https://v1.hitokoto.cn/?encode=text", txtApiManualText: "文本1\n文本2\n文本3", enableTargetUser: false, targetUsernames: "", userSearchTimeout: 10000, maxHistoryLogs: 200, searchDebounceDelay: 500, searchThrottleDelay: 1000, clickMethod: "direct", pageLoadWaitTime: 5000, chatInputCheckInterval: 1000, multiUserMode: "sequential", multiUserRetrySame: false }; // 状态变量 let isProcessing = false; let retryCount = 0; let countdownInterval = null; let isScriptCat = false; let userConfig = {}; let nextSendTime = null; let currentState = "idle"; let chatObserver = null; let searchTimeout = null; let lastSearchTime = 0; let searchDebounceTimer = null; let chatInputCheckTimer = null; // 多用户相关变量 let currentUserIndex = -1; let sentUsersToday = []; let allTargetUsers = []; // 拖动相关变量 let isDragging = false; let dragOffsetX = 0; let dragOffsetY = 0; let currentPanel = null; // ==================== 核心功能函数 ==================== // 检测是否是ScriptCat function detectScriptCat() { return typeof ScriptCat !== 'undefined' || (typeof GM_info !== 'undefined' && GM_info.scriptHandler === 'ScriptCat'); } // 初始化配置 function initConfig() { const savedConfig = GM_getValue('userConfig'); userConfig = savedConfig ? {...DEFAULT_CONFIG, ...savedConfig} : {...DEFAULT_CONFIG}; for (const key in DEFAULT_CONFIG) { if (userConfig[key] === undefined) { userConfig[key] = DEFAULT_CONFIG[key]; } } if (!GM_getValue('txtApiManualSentIndexes')) { GM_setValue('txtApiManualSentIndexes', []); } if (!GM_getValue('historyLogs')) { GM_setValue('historyLogs', []); } // 初始化多用户数据 if (!GM_getValue('sentUsersToday')) { GM_setValue('sentUsersToday', []); } sentUsersToday = GM_getValue('sentUsersToday', []); if (!GM_getValue('currentUserIndex')) { GM_setValue('currentUserIndex', -1); } currentUserIndex = GM_getValue('currentUserIndex', -1); // 解析目标用户列表 parseTargetUsers(); GM_setValue('userConfig', userConfig); return userConfig; } // 解析目标用户列表 function parseTargetUsers() { if (!userConfig.targetUsernames || !userConfig.targetUsernames.trim()) { allTargetUsers = []; return; } const rawText = userConfig.targetUsernames.trim(); allTargetUsers = rawText.split(/[,|\n]/) .map(user => user.trim()) .filter(user => user.length > 0); addHistoryLog(`解析到 ${allTargetUsers.length} 个目标用户: ${allTargetUsers.join(', ')}`, 'info'); } // 获取下一个目标用户 function getNextTargetUser() { if (allTargetUsers.length === 0) { return null; } const unsentUsers = allTargetUsers.filter(user => !sentUsersToday.includes(user)); if (unsentUsers.length === 0) { addHistoryLog('所有目标用户今日都已发送', 'info'); return null; } let nextUser; if (userConfig.multiUserMode === 'random') { const randomIndex = Math.floor(Math.random() * unsentUsers.length); nextUser = unsentUsers[randomIndex]; } else { if (currentUserIndex < 0 || currentUserIndex >= allTargetUsers.length) { currentUserIndex = 0; } let found = false; for (let i = 0; i < allTargetUsers.length; i++) { const index = (currentUserIndex + i) % allTargetUsers.length; const user = allTargetUsers[index]; if (!sentUsersToday.includes(user)) { nextUser = user; currentUserIndex = index; found = true; break; } } if (!found) { return null; } } return nextUser; } // 标记用户为已发送 function markUserAsSent(username) { if (!sentUsersToday.includes(username)) { sentUsersToday.push(username); GM_setValue('sentUsersToday', sentUsersToday); } const index = allTargetUsers.indexOf(username); if (index !== -1) { currentUserIndex = (index + 1) % allTargetUsers.length; GM_setValue('currentUserIndex', currentUserIndex); } addHistoryLog(`用户 ${username} 已标记为今日已发送`, 'success'); updateUserStatusDisplay(); } // 保存配置 function saveConfig() { GM_setValue('userConfig', userConfig); } // 添加历史日志 function addHistoryLog(message, type = 'info') { const logs = GM_getValue('historyLogs', []); const logEntry = { timestamp: new Date().toISOString(), message: message, type: type }; logs.unshift(logEntry); if (logs.length > userConfig.maxHistoryLogs) { logs.splice(userConfig.maxHistoryLogs); } GM_setValue('historyLogs', logs); addLog(message, type); } // 获取历史日志 function getHistoryLogs() { return GM_getValue('historyLogs', []); } // 清空历史日志 function clearHistoryLogs() { GM_setValue('historyLogs', []); addHistoryLog('历史日志已清空', 'info'); } // 导出历史日志 function exportHistoryLogs() { const logs = getHistoryLogs(); const logText = logs.map(log => `${new Date(log.timestamp).toLocaleString()} [${log.type.toUpperCase()}] ${log.message}` ).join('\n'); const blob = new Blob([logText], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `抖音续火助手日志_${new Date().toISOString().split('T')[0]}.txt`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); addHistoryLog('日志已导出', 'success'); } // 添加实时日志 function addLog(message, type = 'info') { const now = new Date(); const timeString = now.toLocaleTimeString(); const logEntry = document.createElement('div'); logEntry.style.color = type === 'success' ? '#00d8b8' : type === 'error' ? '#ff2c54' : '#ffc107'; logEntry.style.padding = '5px 0'; logEntry.style.borderBottom = '1px solid rgba(255,255,255,0.05)'; logEntry.textContent = `${timeString} - ${message}`; const logContainer = document.getElementById('dy-fire-log'); if (logContainer) { logContainer.prepend(logEntry); if (logContainer.children.length > 8) { logContainer.removeChild(logContainer.lastChild); } logContainer.scrollTop = 0; } } // 更新重试计数显示 function updateRetryCount() { const retryEl = document.getElementById('dy-fire-retry'); if (retryEl) { retryEl.textContent = `${retryCount}/${userConfig.maxRetryCount}`; } } // 更新一言状态显示 function updateHitokotoStatus(status, isSuccess = true) { const statusEl = document.getElementById('dy-fire-hitokoto'); if (statusEl) { statusEl.textContent = status; statusEl.style.color = isSuccess ? '#00d8b8' : '#ff2c54'; } } // 更新TXTAPI状态显示 function updateTxtApiStatus(status, isSuccess = true) { const statusEl = document.getElementById('dy-fire-txtapi'); if (statusEl) { statusEl.textContent = status; statusEl.style.color = isSuccess ? '#00d8b8' : '#ff2c54'; } } // 初始化聊天列表观察器 function initChatObserver() { if (chatObserver) { chatObserver.disconnect(); chatObserver = null; } if (!userConfig.enableTargetUser || currentState !== 'searching') { return; } chatObserver = new MutationObserver(function(mutations) { clearTimeout(searchDebounceTimer); searchDebounceTimer = setTimeout(() => { const now = Date.now(); if (now - lastSearchTime < userConfig.searchThrottleDelay) { return; } lastSearchTime = now; findAndClickTargetUser(); }, userConfig.searchDebounceDelay); }); const chatContainer = findChatContainer(); if (chatContainer) { chatObserver.observe(chatContainer, { childList: true, subtree: true, attributes: false, characterData: false }); addHistoryLog('聊天列表观察器已启动', 'info'); } else { addHistoryLog('未找到聊天列表容器,将使用备用查找策略', 'warn'); chatObserver.observe(document.body, { childList: true, subtree: false, attributes: false, characterData: false }); } } // 查找聊天容器 function findChatContainer() { const possibleSelectors = [ '.chat-list-container', '.semi-list', '[role="list"]', '.conversation-list', '.message-list' ]; for (const selector of possibleSelectors) { const container = document.querySelector(selector); if (container) { return container; } } const sampleUser = document.querySelector('.item-header-name-vL_79m'); if (sampleUser) { let parent = sampleUser; for (let i = 0; i < 10; i++) { parent = parent.parentElement; if (parent && parent.children.length > 5) { return parent; } if (!parent) break; } } return null; } // 停止聊天观察器 function stopChatObserver() { if (chatObserver) { chatObserver.disconnect(); chatObserver = null; } clearTimeout(searchDebounceTimer); addHistoryLog('聊天列表观察器已停止', 'info'); } // 安全地创建鼠标事件 function createSafeMouseEvent(type, options = {}) { try { const safeOptions = { bubbles: true, cancelable: true, view: window, ...options }; return new MouseEvent(type, safeOptions); } catch (error) { try { const safeOptions = { bubbles: true, cancelable: true, ...options }; delete safeOptions.view; return new MouseEvent(type, safeOptions); } catch (error2) { addHistoryLog(`创建鼠标事件失败: ${error2.message}`, 'error'); return null; } } } // 查找并点击目标用户 function findAndClickTargetUser() { if (!userConfig.enableTargetUser || allTargetUsers.length === 0) { updateUserStatus('配置错误', false); return false; } if (currentState !== 'searching') { return false; } let currentTargetUser; if (userConfig.multiUserRetrySame && retryCount > 1) { const lastSentUser = GM_getValue('lastTargetUser', ''); if (lastSentUser && allTargetUsers.includes(lastSentUser)) { currentTargetUser = lastSentUser; } else { currentTargetUser = getNextTargetUser(); } } else { currentTargetUser = getNextTargetUser(); } if (!currentTargetUser) { addHistoryLog('没有可发送的目标用户', 'info'); updateUserStatus('无目标用户', false); stopChatObserver(); isProcessing = false; return false; } GM_setValue('lastTargetUser', currentTargetUser); addHistoryLog(`查找目标用户: ${currentTargetUser}`, 'info'); updateUserStatus(`寻找: ${currentTargetUser}`, null); const userElements = document.querySelectorAll('.item-header-name-vL_79m'); let targetElement = null; for (let element of userElements) { if (element.textContent.trim() === currentTargetUser) { targetElement = element; break; } } if (targetElement) { addHistoryLog(`找到目标用户: ${currentTargetUser}`, 'success'); updateUserStatus(`已找到: ${currentTargetUser}`, true); stopChatObserver(); let clickSuccess = false; if (userConfig.clickMethod === 'direct') { try { targetElement.click(); addHistoryLog('使用直接点击方法成功', 'success'); clickSuccess = true; } catch (error) { addHistoryLog(`直接点击失败: ${error.message}`, 'error'); } } else { try { const clickEvent = createSafeMouseEvent('click'); if (clickEvent) { targetElement.dispatchEvent(clickEvent); addHistoryLog('使用事件触发方法成功', 'success'); clickSuccess = true; } else { targetElement.click(); addHistoryLog('事件创建失败,使用直接点击成功', 'success'); clickSuccess = true; } } catch (error) { addHistoryLog(`事件触发失败: ${error.message}`, 'error'); } } if (clickSuccess) { currentState = 'found'; waitForPageLoad().then(() => { addHistoryLog('页面加载完成,开始查找聊天输入框', 'info'); tryFindChatInput(); }).catch(error => { addHistoryLog(`等待页面加载超时: ${error.message}`, 'error'); tryFindChatInput(); }); return true; } else { try { let clickableParent = targetElement; for (let i = 0; i < 5; i++) { clickableParent = clickableParent.parentElement; if (!clickableParent) break; const style = window.getComputedStyle(clickableParent); if (style.cursor === 'pointer' || clickableParent.onclick) { clickableParent.click(); addHistoryLog('通过父元素点击成功', 'success'); currentState = 'found'; waitForPageLoad().then(() => { addHistoryLog('页面加载完成,开始查找聊天输入框', 'info'); tryFindChatInput(); }).catch(error => { addHistoryLog(`等待页面加载超时: ${error.message}`, 'error'); tryFindChatInput(); }); return true; } } } catch (error) { addHistoryLog(`父元素点击也失败: ${error.message}`, 'error'); } updateUserStatus('点击失败', false); return false; } } else { addHistoryLog(`未找到目标用户: ${currentTargetUser}`, 'warn'); updateUserStatus(`寻找: ${currentTargetUser}`, null); return false; } } // 等待页面加载完成 function waitForPageLoad() { return new Promise((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error(`页面加载等待超时 (${userConfig.pageLoadWaitTime}ms)`)); }, userConfig.pageLoadWaitTime); if (document.readyState === 'complete') { clearTimeout(timeout); resolve(); return; } window.addEventListener('load', function onLoad() { clearTimeout(timeout); window.removeEventListener('load', onLoad); resolve(); }); let checkCount = 0; const maxChecks = userConfig.pageLoadWaitTime / 100; const checkInterval = setInterval(() => { checkCount++; const chatInput = document.querySelector('.chat-input-dccKiL'); if (chatInput) { clearTimeout(timeout); clearInterval(checkInterval); resolve(); return; } if (checkCount >= maxChecks) { clearTimeout(timeout); clearInterval(checkInterval); reject(new Error('页面DOM变化检查超时')); } }, 100); }); } // 发送消息函数 async function sendMessage() { if (isProcessing) { addHistoryLog('已有任务正在进行中', 'error'); return; } if (userConfig.enableTargetUser && allTargetUsers.length > 0) { const unsentUsers = allTargetUsers.filter(user => !sentUsersToday.includes(user)); if (unsentUsers.length === 0) { addHistoryLog('所有目标用户今日都已发送', 'info'); return; } } else { const lastSentDate = GM_getValue('lastSentDate', ''); const today = new Date().toDateString(); if (lastSentDate === today) { addHistoryLog('今天已经发送过消息', 'info'); return; } } isProcessing = true; retryCount = 0; currentState = 'idle'; updateRetryCount(); addHistoryLog('开始发送流程...', 'info'); executeSendProcess(); } // 执行发送流程 async function executeSendProcess() { retryCount++; updateRetryCount(); if (retryCount > userConfig.maxRetryCount) { addHistoryLog(`已达到最大重试次数 (${userConfig.maxRetryCount})`, 'error'); isProcessing = false; currentState = 'idle'; stopChatObserver(); return; } addHistoryLog(`尝试发送 (${retryCount}/${userConfig.maxRetryCount})`, 'info'); if (userConfig.enableTargetUser && allTargetUsers.length > 0) { currentState = 'searching'; const searchTimeoutId = setTimeout(() => { if (currentState === 'searching') { addHistoryLog('用户查找超时', 'error'); updateUserStatus('查找超时', false); stopChatObserver(); setTimeout(executeSendProcess, 2000); } }, userConfig.userSearchTimeout); initChatObserver(); const found = findAndClickTargetUser(); if (!found) { // 用户查找失败,观察器会继续工作 } } else { setTimeout(tryFindChatInput, 1000); } } // 尝试查找聊天输入框并发送消息 let chatInputRetryCount = 0; async function tryFindChatInput() { if (chatInputCheckTimer) { clearTimeout(chatInputCheckTimer); } const input = document.querySelector('.chat-input-dccKiL'); if (input) { chatInputRetryCount = 0; // 重置重试计数 addHistoryLog('找到聊天输入框', 'info'); let messageToSend; try { messageToSend = await getMessageContent(); addHistoryLog('消息内容准备完成', 'success'); } catch (error) { addHistoryLog(`消息获取失败: ${error.message}`, 'error'); messageToSend = `${userConfig.baseMessage} | 消息获取失败~`; } currentState = 'sending'; input.textContent = ''; input.focus(); const lines = messageToSend.split('\n'); for (let i = 0; i < lines.length; i++) { document.execCommand('insertText', false, lines[i]); if (i < lines.length - 1) { document.execCommand('insertLineBreak'); } } input.dispatchEvent(new Event('input', { bubbles: true })); setTimeout(() => { const sendBtn = document.querySelector('.chat-btn'); if (sendBtn && !sendBtn.disabled) { addHistoryLog('正在发送消息...', 'info'); sendBtn.click(); setTimeout(() => { addHistoryLog('消息发送成功!', 'success'); if (userConfig.enableTargetUser && allTargetUsers.length > 0) { const currentTargetUser = GM_getValue('lastTargetUser', ''); if (currentTargetUser) { markUserAsSent(currentTargetUser); } } else { const today = new Date().toDateString(); GM_setValue('lastSentDate', today); // 修复:单用户模式下发送成功后更新进度显示 updateUserStatusDisplay(); } updateStatus(true); isProcessing = false; currentState = 'idle'; stopChatObserver(); if (userConfig.enableTargetUser && allTargetUsers.length > 0) { const unsentUsers = allTargetUsers.filter(user => !sentUsersToday.includes(user)); if (unsentUsers.length > 0) { addHistoryLog(`还有 ${unsentUsers.length} 个用户待发送,继续下一个用户`, 'info'); setTimeout(sendMessage, 2000); } else { addHistoryLog('所有用户发送完成!', 'success'); } } if (typeof GM_notification !== 'undefined') { try { GM_notification({ title: '抖音续火助手', text: '续火消息发送成功!', timeout: 3000 }); } catch (e) { GM_notification('续火消息发送成功!', '抖音续火助手'); } } }, 1000); } else { addHistoryLog('发送按钮不可用', 'error'); setTimeout(executeSendProcess, 2000); } }, 500); } else { chatInputRetryCount++; addHistoryLog(`未找到输入框,继续查找中... (${chatInputRetryCount}/${userConfig.maxRetryCount})`, 'info'); // 检查是否超过最大重试次数 if (chatInputRetryCount >= userConfig.maxRetryCount) { addHistoryLog(`查找聊天输入框超过最大重试次数 (${userConfig.maxRetryCount}),触发重试流程`, 'error'); chatInputRetryCount = 0; setTimeout(executeSendProcess, 2000); return; } chatInputCheckTimer = setTimeout(() => { tryFindChatInput(); }, userConfig.chatInputCheckInterval); } } // 获取消息内容 async function getMessageContent() { let customMessage = userConfig.customMessage || userConfig.baseMessage; let hitokotoContent = ''; if (userConfig.useHitokoto) { try { addHistoryLog('正在获取一言内容...', 'info'); hitokotoContent = await getHitokoto(); addHistoryLog('一言内容获取成功', 'success'); } catch (error) { addHistoryLog(`一言获取失败: ${error.message}`, 'error'); hitokotoContent = '一言获取失败~'; } } let txtApiContent = ''; if (userConfig.useTxtApi) { try { addHistoryLog('正在获取TXTAPI内容...', 'info'); txtApiContent = await getTxtApiContent(); addHistoryLog('TXTAPI内容获取成功', 'success'); } catch (error) { addHistoryLog(`TXTAPI获取失败: ${error.message}`, 'error'); txtApiContent = 'TXTAPI获取失败~'; } } if (customMessage.includes('[API]')) { customMessage = customMessage.replace('[API]', hitokotoContent); } else if (userConfig.useHitokoto) { customMessage += ` | ${hitokotoContent}`; } if (customMessage.includes('[TXTAPI]')) { customMessage = customMessage.replace('[TXTAPI]', txtApiContent); } else if (userConfig.useTxtApi) { customMessage += ` | ${txtApiContent}`; } return customMessage; } // 获取一言内容 function getHitokoto() { return new Promise((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error('一言API请求超时')); }, userConfig.hitokotoTimeout); GM_xmlhttpRequest({ method: 'GET', url: 'https://v1.hitokoto.cn/', responseType: 'json', onload: function(response) { clearTimeout(timeout); if (response.status === 200) { try { const data = response.response; let message = formatHitokoto(userConfig.hitokotoFormat, data); updateHitokotoStatus('获取成功'); resolve(message); } catch (e) { updateHitokotoStatus('解析失败', false); reject(new Error('一言API响应解析失败')); } } else { updateHitokotoStatus('请求失败', false); reject(new Error(`一言API请求失败: ${response.status}`)); } }, onerror: function(error) { clearTimeout(timeout); updateHitokotoStatus('网络错误', false); reject(new Error('一言API网络错误')); }, ontimeout: function() { clearTimeout(timeout); updateHitokotoStatus('请求超时', false); reject(new Error('一言API请求超时')); } }); }); } // 格式化一言内容 function formatHitokoto(format, data) { let result = format.replace(/{hitokoto}/g, data.hitokoto || ''); let fromFormatted = ''; if (data.from) { fromFormatted = userConfig.fromFormat.replace(/{from}/g, data.from); } result = result.replace(/{from}/g, fromFormatted); let fromWhoFormatted = ''; if (data.from_who) { fromWhoFormatted = userConfig.fromWhoFormat.replace(/{from_who}/g, data.from_who); } result = result.replace(/{from_who}/g, fromWhoFormatted); return result; } // 获取TXTAPI内容 function getTxtApiContent() { return new Promise((resolve, reject) => { if (userConfig.txtApiMode === 'api') { const timeout = setTimeout(() => { reject(new Error('TXTAPI请求超时')); }, userConfig.txtApiTimeout); GM_xmlhttpRequest({ method: 'GET', url: userConfig.txtApiUrl, onload: function(response) { clearTimeout(timeout); if (response.status === 200) { try { updateTxtApiStatus('获取成功'); resolve(response.responseText.trim()); } catch (e) { updateTxtApiStatus('解析失败', false); reject(new Error('TXTAPI响应解析失败')); } } else { updateTxtApiStatus('请求失败', false); reject(new Error(`TXTAPI请求失败: ${response.status}`)); } }, onerror: function(error) { clearTimeout(timeout); updateTxtApiStatus('网络错误', false); reject(new Error('TXTAPI网络错误')); }, ontimeout: function() { clearTimeout(timeout); updateTxtApiStatus('请求超时', false); reject(new Error('TXTAPI请求超时')); } }); } else { try { const lines = userConfig.txtApiManualText.split('\n').filter(line => line.trim()); if (lines.length === 0) { updateTxtApiStatus('无内容', false); reject(new Error('手动文本内容为空')); return; } let sentIndexes = GM_getValue('txtApiManualSentIndexes', []); if (userConfig.txtApiManualRandom) { let availableIndexes = []; for (let i = 0; i < lines.length; i++) { if (!sentIndexes.includes(i)) { availableIndexes.push(i); } } if (availableIndexes.length === 0) { sentIndexes = []; availableIndexes = Array.from({length: lines.length}, (_, i) => i); GM_setValue('txtApiManualSentIndexes', []); } const randomIndex = Math.floor(Math.random() * availableIndexes.length); const selectedIndex = availableIndexes[randomIndex]; const selectedText = lines[selectedIndex].trim(); sentIndexes.push(selectedIndex); GM_setValue('txtApiManualSentIndexes', sentIndexes); updateTxtApiStatus('获取成功'); resolve(selectedText); } else { let nextIndex = 0; if (sentIndexes.length > 0) { nextIndex = (sentIndexes[sentIndexes.length - 1] + 1) % lines.length; } const selectedText = lines[nextIndex].trim(); sentIndexes.push(nextIndex); GM_setValue('txtApiManualSentIndexes', sentIndexes); updateTxtApiStatus('获取成功'); resolve(selectedText); } } catch (e) { updateTxtApiStatus('解析失败', false); reject(new Error('手动文本解析失败')); } } }); } // 解析时间字符串为日期对象 function parseTimeString(timeStr) { const [hours, minutes, seconds] = timeStr.split(':').map(Number); const now = new Date(); const targetTime = new Date(now); targetTime.setHours(hours, minutes, seconds || 0, 0); if (targetTime <= now) { targetTime.setDate(targetTime.getDate() + 1); } return targetTime; } // 更新状态 function updateStatus(isSent) { const statusEl = document.getElementById('dy-fire-status'); if (statusEl) { if (isSent) { statusEl.textContent = '已发送'; statusEl.style.color = '#00d8b8'; } else { statusEl.textContent = '未发送'; statusEl.style.color = '#dc3545'; autoSendIfNeeded(); } } const now = new Date(); if (isSent) { nextSendTime = parseTimeString(userConfig.sendTime); const tomorrow = new Date(now); tomorrow.setDate(tomorrow.getDate() + 1); if (nextSendTime.getDate() !== tomorrow.getDate()) { nextSendTime.setDate(tomorrow.getDate()); } } else { nextSendTime = parseTimeString(userConfig.sendTime); if (nextSendTime <= now) { nextSendTime.setDate(nextSendTime.getDate() + 1); } } const nextEl = document.getElementById('dy-fire-next'); if (nextEl) { nextEl.textContent = nextSendTime.toLocaleString(); } startCountdown(nextSendTime); } // 检查是否需要自动发送 function autoSendIfNeeded() { const now = new Date(); const today = new Date().toDateString(); if (userConfig.enableTargetUser && allTargetUsers.length > 0) { const unsentUsers = allTargetUsers.filter(user => !sentUsersToday.includes(user)); if (unsentUsers.length > 0 && !isProcessing) { const [targetHour, targetMinute, targetSecond] = userConfig.sendTime.split(':').map(Number); const targetTimeToday = new Date(); targetTimeToday.setHours(targetHour, targetMinute, targetSecond || 0, 0); if (now >= targetTimeToday) { addHistoryLog(`检测到有${unsentUsers.length}个用户未发送且已过${userConfig.sendTime},自动发送`, 'info'); sendMessage(); } } } else { const lastSentDate = GM_getValue('lastSentDate', ''); const [targetHour, targetMinute, targetSecond] = userConfig.sendTime.split(':').map(Number); if (lastSentDate !== today) { const targetTimeToday = new Date(); targetTimeToday.setHours(targetHour, targetMinute, targetSecond || 0, 0); if (now >= targetTimeToday && !isProcessing) { addHistoryLog(`检测到今日未发送且已过${userConfig.sendTime},自动发送`, 'info'); sendMessage(); } } } } // 开始倒计时 function startCountdown(targetTime) { if (countdownInterval) { clearInterval(countdownInterval); } function update() { const now = new Date(); const diff = targetTime - now; if (diff <= 0) { const countdownEl = document.getElementById('dy-fire-countdown'); if (countdownEl) { countdownEl.textContent = '00:00:00'; } if (userConfig.enableTargetUser && allTargetUsers.length > 0) { const unsentUsers = allTargetUsers.filter(user => !sentUsersToday.includes(user)); if (unsentUsers.length > 0) { if (!isProcessing) { addHistoryLog('倒计时结束,开始发送给未发送的用户', 'info'); sendMessage(); } } else { nextSendTime = parseTimeString(userConfig.sendTime); const tomorrow = new Date(now); tomorrow.setDate(tomorrow.getDate() + 1); if (nextSendTime.getDate() !== tomorrow.getDate()) { nextSendTime.setDate(tomorrow.getDate()); } startCountdown(nextSendTime); } } else { const lastSentDate = GM_getValue('lastSentDate', ''); const today = new Date().toDateString(); if (lastSentDate === today) { nextSendTime = parseTimeString(userConfig.sendTime); const tomorrow = new Date(now); tomorrow.setDate(tomorrow.getDate() + 1); if (nextSendTime.getDate() !== tomorrow.getDate()) { nextSendTime.setDate(tomorrow.getDate()); } startCountdown(nextSendTime); } else { if (!isProcessing) { GM_setValue('lastSentDate', ''); updateStatus(false); addHistoryLog('已清空发送记录,准备发送新消息', 'info'); sendMessage(); } } } return; } const hours = Math.floor(diff / (1000 * 60 * 60)); const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((diff % (1000 * 60)) / 1000); const countdownEl = document.getElementById('dy-fire-countdown'); if (countdownEl) { countdownEl.textContent = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; } } update(); countdownInterval = setInterval(update, 1000); } // 清空数据 function clearData() { GM_setValue('lastSentDate', ''); GM_setValue('txtApiManualSentIndexes', []); GM_setValue('lastTargetUser', ''); resetTodaySentUsers(); addHistoryLog('发送记录已清空', 'info'); updateStatus(false); retryCount = 0; updateRetryCount(); updateHitokotoStatus('未获取'); updateTxtApiStatus('未获取'); updateUserStatusDisplay(); stopChatObserver(); if (chatInputCheckTimer) { clearTimeout(chatInputCheckTimer); } } // 重置所有配置 function resetAllConfig() { if (typeof GM_listValues !== 'undefined' && typeof GM_deleteValue !== 'undefined') { try { const values = GM_listValues(); values.forEach(key => { GM_deleteValue(key); }); } catch (e) { GM_setValue('lastSentDate', ''); GM_setValue('userConfig', ''); GM_setValue('txtApiManualSentIndexes', []); GM_setValue('historyLogs', []); GM_setValue('sentUsersToday', []); GM_setValue('currentUserIndex', -1); GM_setValue('lastTargetUser', ''); } } else { GM_setValue('lastSentDate', ''); GM_setValue('userConfig', ''); GM_setValue('txtApiManualSentIndexes', []); GM_setValue('historyLogs', []); GM_setValue('sentUsersToday', []); GM_setValue('currentUserIndex', -1); GM_setValue('lastTargetUser', ''); } initConfig(); addHistoryLog('所有配置已重置', 'info'); updateStatus(false); retryCount = 0; updateRetryCount(); updateHitokotoStatus('未获取'); updateTxtApiStatus('未获取'); updateUserStatusDisplay(); stopChatObserver(); if (chatInputCheckTimer) { clearTimeout(chatInputCheckTimer); } if (typeof GM_notification !== 'undefined') { try { GM_notification({ title: '抖音续火助手', text: '所有配置已重置!', timeout: 3000 }); } catch (e) { GM_notification('所有配置已重置!', '抖音续火助手'); } } } // ==================== UI相关函数 ==================== // 更新用户状态显示 function updateUserStatusDisplay() { const statusEl = document.getElementById('dy-fire-user-status'); const progressEl = document.getElementById('dy-fire-user-progress'); if (!statusEl || !progressEl) return; if (!userConfig.enableTargetUser || allTargetUsers.length === 0) { const lastSentDate = GM_getValue('lastSentDate', ''); const today = new Date().toDateString(); const isSentToday = lastSentDate === today; const progressText = isSentToday ? '1/1' : '0/1'; progressEl.textContent = progressText; if (isSentToday) { statusEl.textContent = '已完成'; statusEl.style.color = '#00d8b8'; } else { statusEl.textContent = '未开始'; statusEl.style.color = '#999'; } return; } const sentCount = sentUsersToday.length; const totalCount = allTargetUsers.length; const progressText = `${sentCount}/${totalCount}`; progressEl.textContent = progressText; if (sentCount >= totalCount) { statusEl.textContent = '全部完成'; statusEl.style.color = '#00d8b8'; } else { statusEl.textContent = `进行中 ${progressText}`; statusEl.style.color = '#ff2c54'; } } // 重置今日发送记录 function resetTodaySentUsers() { sentUsersToday = []; GM_setValue('sentUsersToday', []); currentUserIndex = -1; GM_setValue('currentUserIndex', -1); GM_setValue('lastSentDate', ''); addHistoryLog('今日发送记录已重置', 'info'); updateUserStatusDisplay(); } // 更新用户状态显示 function updateUserStatus(status, isSuccess = null) { const statusEl = document.getElementById('dy-fire-user-status'); if (!statusEl) return; if (status) { statusEl.textContent = status; } if (isSuccess === true) { statusEl.style.color = '#00d8b8'; } else if (isSuccess === false) { statusEl.style.color = '#ff2c54'; } else { statusEl.style.color = '#999'; } } // 创建UI控制面板 function createControlPanel() { const existingPanel = document.getElementById('dy-fire-helper'); if (existingPanel) { existingPanel.remove(); } const panel = document.createElement('div'); panel.id = 'dy-fire-helper'; panel.style.cssText = ` position: fixed; top: 20px; right: 20px; width: 450px; background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%); border-radius: 16px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(255, 255, 255, 0.1); z-index: 9999; font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif; padding: 0; color: #fff; transition: all 0.3s ease; max-height: 1000px; overflow: hidden; backdrop-filter: blur(10px); user-select: none; `; panel.innerHTML = `