config.js 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. export const DEFAULT_AUTOMATION = Object.freeze({ enabled: false, intervalSeconds: 120, dwellSeconds: 4, maxChats: 50, listScrollPasses: 8, historyScrollPasses: 2, chatItemSelector: '', chatListSelector: '', messageListSelector: '' });
  2. export const DEFAULT_CONFIG = Object.freeze({ enabled: false, accountId: '', token: '', port: 4318, includeActivity: false, selectors: { message: '', event: '', activity: '' }, automation: DEFAULT_AUTOMATION });
  3. export function validateConfig(input) {
  4. if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error('配置格式无效');
  5. const port = Number(input.port);
  6. if (!Number.isInteger(port) || port < 1024 || port > 65535) throw new Error('端口必须为 1024–65535 的整数');
  7. const accountId = typeof input.accountId === 'string' ? input.accountId.trim() : '';
  8. const token = typeof input.token === 'string' ? input.token.trim() : '';
  9. if (accountId.length > 100 || /[\u0000-\u001f\u007f]/.test(accountId)) throw new Error('账号标签最多 100 个字符,不能包含控制字符');
  10. if (token.length > 512 || /[\s\u0000-\u001f\u007f]/.test(token)) throw new Error('配对令牌格式无效');
  11. if (input.enabled && (!accountId || !token)) throw new Error('启用前请填写唯一账号标签和本地服务配对令牌');
  12. const selectors = {};
  13. for (const kind of ['message', 'event', 'activity']) {
  14. const value = input.selectors?.[kind] ?? '';
  15. if (typeof value !== 'string' || value.length > 2000) throw new Error('选择器必须为不超过 2000 字符的文本');
  16. selectors[kind] = value.trim();
  17. }
  18. const supplied = input.automation ?? {};
  19. if (!supplied || typeof supplied !== 'object' || Array.isArray(supplied)) throw new Error('自动采集配置格式无效');
  20. const automation = { enabled: supplied.enabled === true };
  21. for (const [key, min, max] of [['intervalSeconds', 30, 3600], ['dwellSeconds', 2, 30], ['maxChats', 1, 200], ['listScrollPasses', 0, 30], ['historyScrollPasses', 0, 20]]) {
  22. const value = Number(supplied[key] ?? DEFAULT_AUTOMATION[key]);
  23. if (!Number.isInteger(value) || value < min || value > max) throw new Error(`自动采集 ${key} 必须为 ${min}–${max} 的整数`);
  24. automation[key] = value;
  25. }
  26. for (const key of ['chatItemSelector', 'chatListSelector', 'messageListSelector']) {
  27. const value = supplied[key] ?? '';
  28. if (typeof value !== 'string' || value.length > 2000) throw new Error('自动采集选择器必须为不超过 2000 字符的文本');
  29. automation[key] = value.trim();
  30. }
  31. return { enabled: input.enabled === true, accountId, token, port, includeActivity: input.includeActivity === true, selectors, automation };
  32. }
  33. export function publicConfig(config) {
  34. return { enabled: config.enabled, accountId: config.accountId, includeActivity: config.includeActivity, selectors: config.selectors, automation: { ...DEFAULT_AUTOMATION, ...config.automation, assigned: false, collectorTab: false } };
  35. }
  36. export function isTeamsUrl(input) {
  37. try {
  38. const url = new URL(input);
  39. return url.protocol === 'https:' && ['teams.microsoft.com', 'teams.cloud.microsoft'].includes(url.hostname) && !url.port && !url.username && !url.password;
  40. } catch { return false; }
  41. }
  42. export function isTrustedPage(sender, extensionId, baseUrl) {
  43. return sender.id === extensionId && ['options.html', 'popup.html'].some(path => sender.url === `${baseUrl}${path}`);
  44. }
  45. export function isTeamsSender(sender, extensionId) {
  46. return sender.id === extensionId && sender.frameId === 0 && Number.isInteger(sender.tab?.id) && isTeamsUrl(sender.url) && isTeamsUrl(sender.tab.url);
  47. }
  48. export function validateCapture(input, config) {
  49. if (!config.enabled) throw new Error('采集已暂停');
  50. if (input.accountId !== config.accountId || !config.accountId || !config.token) throw new Error('账号配置已变更,请刷新 Teams 页面');
  51. if (!Array.isArray(input.records) || input.records.length > 200) throw new Error('单批记录数量超限');
  52. const payload = { accountId: config.accountId, records: input.records, page: input.page };
  53. const json = JSON.stringify(payload);
  54. if (new TextEncoder().encode(json).length > 900000) throw new Error('单批数据大小超限');
  55. if (!input.page || !isTeamsUrl(input.page.url)) throw new Error('来源页面无效');
  56. for (const record of input.records) {
  57. if (!record || !['message', 'event', ...(config.includeActivity ? ['activity'] : [])].includes(record.kind) || typeof record.externalId !== 'string' || !record.externalId || record.externalId.length > 1200 || typeof record.text !== 'string' || record.text.length > 20000) throw new Error('记录格式无效');
  58. }
  59. return json;
  60. }
  61. export async function deliverWithRetry(send, { attempts = 3, sleep = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds)) } = {}) {
  62. let lastError;
  63. for (let attempt = 0; attempt < attempts; attempt++) {
  64. try { return await send(); }
  65. catch (error) {
  66. lastError = error;
  67. if (error.permanent || attempt === attempts - 1) break;
  68. await sleep([300, 1000, 2000][Math.min(attempt, 2)]);
  69. }
  70. }
  71. throw lastError;
  72. }