| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- export const DEFAULT_AUTOMATION = Object.freeze({ enabled: false, intervalSeconds: 120, dwellSeconds: 4, maxChats: 50, listScrollPasses: 8, historyScrollPasses: 2, chatItemSelector: '', chatListSelector: '', messageListSelector: '' });
- export const DEFAULT_CONFIG = Object.freeze({ enabled: false, accountId: '', token: '', port: 4318, includeActivity: false, selectors: { message: '', event: '', activity: '' }, automation: DEFAULT_AUTOMATION });
- export function validateConfig(input) {
- if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error('配置格式无效');
- const port = Number(input.port);
- if (!Number.isInteger(port) || port < 1024 || port > 65535) throw new Error('端口必须为 1024–65535 的整数');
- const accountId = typeof input.accountId === 'string' ? input.accountId.trim() : '';
- const token = typeof input.token === 'string' ? input.token.trim() : '';
- if (accountId.length > 100 || /[\u0000-\u001f\u007f]/.test(accountId)) throw new Error('账号标签最多 100 个字符,不能包含控制字符');
- if (token.length > 512 || /[\s\u0000-\u001f\u007f]/.test(token)) throw new Error('配对令牌格式无效');
- if (input.enabled && (!accountId || !token)) throw new Error('启用前请填写唯一账号标签和本地服务配对令牌');
- const selectors = {};
- for (const kind of ['message', 'event', 'activity']) {
- const value = input.selectors?.[kind] ?? '';
- if (typeof value !== 'string' || value.length > 2000) throw new Error('选择器必须为不超过 2000 字符的文本');
- selectors[kind] = value.trim();
- }
- const supplied = input.automation ?? {};
- if (!supplied || typeof supplied !== 'object' || Array.isArray(supplied)) throw new Error('自动采集配置格式无效');
- const automation = { enabled: supplied.enabled === true };
- for (const [key, min, max] of [['intervalSeconds', 30, 3600], ['dwellSeconds', 2, 30], ['maxChats', 1, 200], ['listScrollPasses', 0, 30], ['historyScrollPasses', 0, 20]]) {
- const value = Number(supplied[key] ?? DEFAULT_AUTOMATION[key]);
- if (!Number.isInteger(value) || value < min || value > max) throw new Error(`自动采集 ${key} 必须为 ${min}–${max} 的整数`);
- automation[key] = value;
- }
- for (const key of ['chatItemSelector', 'chatListSelector', 'messageListSelector']) {
- const value = supplied[key] ?? '';
- if (typeof value !== 'string' || value.length > 2000) throw new Error('自动采集选择器必须为不超过 2000 字符的文本');
- automation[key] = value.trim();
- }
- return { enabled: input.enabled === true, accountId, token, port, includeActivity: input.includeActivity === true, selectors, automation };
- }
- export function publicConfig(config) {
- return { enabled: config.enabled, accountId: config.accountId, includeActivity: config.includeActivity, selectors: config.selectors, automation: { ...DEFAULT_AUTOMATION, ...config.automation, assigned: false, collectorTab: false } };
- }
- export function isTeamsUrl(input) {
- try {
- const url = new URL(input);
- return url.protocol === 'https:' && ['teams.microsoft.com', 'teams.cloud.microsoft'].includes(url.hostname) && !url.port && !url.username && !url.password;
- } catch { return false; }
- }
- export function isTrustedPage(sender, extensionId, baseUrl) {
- return sender.id === extensionId && ['options.html', 'popup.html'].some(path => sender.url === `${baseUrl}${path}`);
- }
- export function isTeamsSender(sender, extensionId) {
- return sender.id === extensionId && sender.frameId === 0 && Number.isInteger(sender.tab?.id) && isTeamsUrl(sender.url) && isTeamsUrl(sender.tab.url);
- }
- export function validateCapture(input, config) {
- if (!config.enabled) throw new Error('采集已暂停');
- if (input.accountId !== config.accountId || !config.accountId || !config.token) throw new Error('账号配置已变更,请刷新 Teams 页面');
- if (!Array.isArray(input.records) || input.records.length > 200) throw new Error('单批记录数量超限');
- const payload = { accountId: config.accountId, records: input.records, page: input.page };
- const json = JSON.stringify(payload);
- if (new TextEncoder().encode(json).length > 900000) throw new Error('单批数据大小超限');
- if (!input.page || !isTeamsUrl(input.page.url)) throw new Error('来源页面无效');
- for (const record of input.records) {
- 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('记录格式无效');
- }
- return json;
- }
- export async function deliverWithRetry(send, { attempts = 3, sleep = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds)) } = {}) {
- let lastError;
- for (let attempt = 0; attempt < attempts; attempt++) {
- try { return await send(); }
- catch (error) {
- lastError = error;
- if (error.permanent || attempt === attempts - 1) break;
- await sleep([300, 1000, 2000][Math.min(attempt, 2)]);
- }
- }
- throw lastError;
- }
|