import { DEFAULT_SELECTORS } from './parser.js'; const $ = id => document.getElementById(id); const automationFields = { intervalSeconds: 'automation-interval', dwellSeconds: 'automation-dwell', maxChats: 'automation-max', listScrollPasses: 'automation-list-scroll', historyScrollPasses: 'automation-history-scroll' }; const automationSelectors = { chatItemSelector: 'chat-item-selector', chatListSelector: 'chat-list-selector', messageListSelector: 'message-list-selector' }; const phases = { disabled: '已停止', idle: '等待页面', discovering: '发现聊天', opening: '打开聊天', settling: '等待加载', capturing: '保存消息', history: '加载历史', waiting: '等待下一轮', paused: '已暂停', error: '遇到错误' }; const feedback = (text, error = false) => { $('feedback').textContent = text; $('feedback').className = error ? 'error' : 'success'; }; function showConnection(connection, enabled, prefix = '') { const description = !connection.teamsTabCount ? '尚未打开 Teams 页面。请打开 Teams 并进入聊天或日历。' : connection.error ? connection.error : `页面脚本已就绪(${connection.readyTabCount}/${connection.teamsTabCount}),${enabled ? '正在等待页面心跳。' : '采集已暂停。'}`; $('connection').textContent = description; if (prefix) feedback(`${prefix}${description}`, Boolean(connection.error)); } async function call(type, extra = {}) { const result = await chrome.runtime.sendMessage({ type, ...extra }); if (!result?.ok) throw new Error(result?.error || '扩展未响应,请重新打开设置页'); return result; } function draft() { return { accountId: $('account').value, token: $('token').value, port: Number($('port').value), enabled: $('enabled').checked, includeActivity: $('activity').checked, selectors: Object.fromEntries(['message', 'event', 'activity'].map(kind => [kind, $(`${kind}-selector`).value])), automation: { enabled: $('automation-enabled').checked, ...Object.fromEntries(Object.entries(automationFields).map(([key, id]) => [key, Number($(id).value)])), ...Object.fromEntries(Object.entries(automationSelectors).map(([key, id]) => [key, $(id).value])) } }; } function showAutomation(config, status = {}) { const state = status.automation || {}; const next = state.nextRunAt ? `;下轮 ${new Date(state.nextRunAt).toLocaleTimeString('zh-CN')}` : ''; $('automation-status').textContent = `${config.automation.enabled ? phases[state.phase] || '准备启动' : '自动采集已停止'} · 本轮 ${state.visited || 0}/${state.discovered || 0} 个聊天${next}${state.coverage ? `。${state.coverage}` : ''}${state.error ? `。${state.error}` : ''}`; $('automation-status').className = state.error ? 'error' : ''; } function validateDraft(config) { validateSelectors(config.selectors); validateSelectors(Object.fromEntries(Object.keys(automationSelectors).map(key => [key, config.automation[key]]))); } function validateSelectors(selectors) { for (const [kind, selector] of Object.entries(selectors)) { if (!selector.trim()) continue; try { document.querySelector(selector); } catch { throw new Error(`${kind} CSS 选择器无效,请修正或留空`); } } } async function action(run) { try { await run(); } catch (error) { feedback(error.message, true); } } $('settings-form').addEventListener('submit', event => { event.preventDefault(); void action(async () => { const config = draft(); validateDraft(config); const result = await call('SAVE_CONFIG', { config }); const { connection } = result; $('automation-enabled').checked = result.config.automation.enabled; showAutomation(result.config, result.status); showConnection(connection, config.enabled, config.enabled ? '设置已保存并开启。' : '设置已保存。'); }); }); $('ping').addEventListener('click', () => { void action(async () => { feedback('正在连接…'); await call('PING', { config: draft() }); feedback('连接成功。请保存设置以应用配置。'); }); }); $('login').addEventListener('click', () => { void action(() => call('OPEN_LOGIN')); }); $('dashboard').addEventListener('click', () => { void action(() => call('OPEN_DASHBOARD')); }); $('automation-start').addEventListener('click', () => { void action(async () => { const config = draft(); validateDraft(config); config.automation.enabled = false; await call('SAVE_CONFIG', { config }); const result = await call('START_AUTOMATION'); $('automation-enabled').checked = result.config.automation.enabled; showAutomation(result.config, result.status); feedback('自动采集已启动。请保持新建的采集标签和本地服务运行。'); }); }); $('automation-stop').addEventListener('click', () => { void action(async () => { const result = await call('STOP_AUTOMATION'); $('automation-enabled').checked = false; showAutomation(result.config, result.status); feedback('自动采集已停止;采集标签和待发队列保留,重新启动后继续。'); }); }); $('preview-chats').addEventListener('click', () => { void action(async () => { const config = draft(); validateDraft(config); $('chat-preview-result').textContent = '正在检查聊天列表…'; const result = await call('PREVIEW_CHATS', { config }); const preview = result.preview || result; $('chat-preview-result').textContent = JSON.stringify({ 能发现的聊天数: preview.count, 聊天名称样本: preview.labels, 提示: preview.errors }, null, 2); }); }); $('reconnect').addEventListener('click', () => { void action(async () => { $('connection').textContent = '正在重新连接 Teams 页面…'; const { config, connection } = await call('RECONNECT'); showConnection(connection, config.enabled, '重新连接检查完成。'); }); }); $('preview').addEventListener('click', () => { void action(async () => { $('preview-result').textContent = '正在读取 Teams 页面…'; const config = draft(); validateSelectors(config.selectors); const result = await call('PREVIEW', { config }); $('preview-result').textContent = JSON.stringify({ 可见条目: result.counts, 提示: result.errors, 页面: result.page, 样本: result.sample }, null, 2); }); }); void action(async () => { const { config, connection, status } = await call('GET_STATE'); $('account').value = config.accountId; $('token').value = config.token; $('port').value = config.port; $('enabled').checked = config.enabled; $('activity').checked = config.includeActivity; $('automation-enabled').checked = config.automation.enabled; for (const [key, id] of Object.entries(automationFields)) $(id).value = config.automation[key]; for (const [key, id] of Object.entries(automationSelectors)) $(id).value = config.automation[key]; for (const kind of ['message', 'event', 'activity']) { $(`${kind}-selector`).value = config.selectors[kind] || ''; $(`${kind}-selector`).placeholder = DEFAULT_SELECTORS[kind]; } showConnection(connection, config.enabled); showAutomation(config, status); if (status.lastError || status.runtimeError || status.diagnosticsError) feedback(status.runtimeError || status.lastError || status.diagnosticsError, true); }); chrome.storage.onChanged.addListener((changes, area) => { if (area !== 'local') return; if (changes.config?.newValue) $('automation-enabled').checked = changes.config.newValue.automation?.enabled === true; if (changes.status) showAutomation({ automation: { enabled: $('automation-enabled').checked } }, changes.status.newValue || {}); });