| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- const $ = id => document.getElementById(id);
- let settings;
- const phases = { disabled: '已停止', idle: '等待页面', discovering: '发现聊天', opening: '打开聊天', settling: '等待加载', capturing: '保存消息', history: '加载历史', waiting: '等待下一轮', paused: '已暂停', error: '遇到错误' };
- function showAutomation(config, status) {
- const auto = status.automation || {};
- $('automation-state').textContent = config.automation.enabled ? phases[auto.phase] || '准备启动' : '已关闭';
- $('automation-progress').textContent = `${auto.visited || 0} / ${auto.discovered || 0}`;
- $('automation-next').textContent = auto.nextRunAt ? new Date(auto.nextRunAt).toLocaleTimeString('zh-CN') : '—';
- $('automation-coverage').textContent = auto.error || auto.coverage || '';
- $('automation-toggle').textContent = config.automation.enabled ? '停止自动遍历' : '启动自动遍历';
- }
- async function call(type, extra = {}) {
- const result = await chrome.runtime.sendMessage({ type, ...extra });
- if (!result?.ok) throw new Error(result?.error || '扩展未响应');
- return result;
- }
- async function refresh() {
- const { config, status, teamsTabCount, readyTabCount } = await call('GET_STATE'); settings = config;
- $('state').textContent = !config.enabled ? '采集已暂停' : !teamsTabCount ? '采集已开启 · 尚未打开 Teams' : !readyTabCount ? '采集已开启 · 页面脚本未连接' : '采集已开启 · 页面脚本已就绪';
- $('pages').textContent = `${readyTabCount} / ${teamsTabCount}`;
- $('account').textContent = config.accountId || '尚未配置';
- $('last-success').textContent = status.lastSuccess ? new Date(status.lastSuccess).toLocaleString('zh-CN') : '尚未连接';
- $('counts').textContent = `${status.delivered || 0} / ${status.pending || 0}`;
- $('visible').textContent = `${status.visibleCounts?.message || 0} / ${status.visibleCounts?.event || 0}`;
- $('toggle').textContent = config.enabled ? '暂停采集' : '开启采集';
- showAutomation(config, status);
- $('hint').textContent = !teamsTabCount ? '请打开 Teams 网页并完成 Microsoft 登录。' : !readyTabCount ? '请点击“重新连接页面”;若仍失败,请检查扩展的网站访问权限并刷新 Teams。' : !status.lastSuccess ? '页面脚本已就绪,正在等待首次页面心跳。请保持本地服务运行。' : status.signedIn !== true ? '已收到页面心跳,尚未识别聊天或日历。请打开相关内容,并在设置中预览当前匹配。' : '已观察到 Teams 内容;请保持页面和本地服务打开。';
- $('error').textContent = [...new Set([status.connectionError, status.runtimeError, status.lastError, status.diagnosticsError, ...(status.parserErrors || []), status.overflow ? '待发队列已满,部分新内容未入队。请恢复本地服务并重新打开相关内容。' : ''].filter(Boolean))].join('\n');
- }
- function run(action) { void action().catch(error => { $('error').textContent = error.message; }); }
- $('toggle').addEventListener('click', () => run(async () => {
- if (!settings?.accountId || !settings?.token) { await chrome.runtime.openOptionsPage(); return; }
- await call('SAVE_CONFIG', { config: { ...settings, enabled: !settings.enabled } }); await refresh();
- }));
- $('options').addEventListener('click', () => run(() => chrome.runtime.openOptionsPage()));
- $('automation-toggle').addEventListener('click', () => run(async () => {
- if (!settings?.enabled) { await chrome.runtime.openOptionsPage(); return; }
- await call(settings.automation.enabled ? 'STOP_AUTOMATION' : 'START_AUTOMATION');
- await refresh();
- }));
- $('reconnect').addEventListener('click', () => run(async () => { $('hint').textContent = '正在重新连接 Teams 页面…'; await call('RECONNECT'); await refresh(); }));
- $('login').addEventListener('click', () => run(() => call('OPEN_LOGIN')));
- $('dashboard').addEventListener('click', () => run(() => call('OPEN_DASHBOARD')));
- run(refresh);
- chrome.storage.onChanged.addListener((changes, area) => {
- if (area !== 'local' || !settings) return;
- if (changes.config?.newValue) settings = changes.config.newValue;
- if (changes.status) showAutomation(settings, changes.status.newValue || {});
- });
|