popup.js 4.1 KB

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