parser.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Pure DOM parsing: no network, storage, cookie, token, or input value access.
  2. export const DEFAULT_SELECTORS = Object.freeze({
  3. message: '[data-message-id], [data-tid="chat-pane-message"], [data-tid="message-wrapper"], [data-tid="message-pane-list-run"] [data-item-id], [data-tid="chat-pane-list"] [data-item-id], [role="log"] [role="article"]',
  4. event: '[data-event-id], [data-tid="calendar-event"], [data-tid="calendar-event-item"], [data-tid="calendar-item"], [data-tid="event-card"], [role="gridcell"] [data-tid="event"]',
  5. activity: '[data-tid="activity-feed-item"], [data-tid="notification-list-item"]'
  6. });
  7. const SKIP = 'script,style,noscript,input,textarea,select,option,[contenteditable]:not([contenteditable="false"]),[role="textbox"]';
  8. const BLOCKED_ROOT = 'html,body,main,nav,form,[role="main"],[role="navigation"],[role="log"],[role="grid"]';
  9. const BODY_SELECTORS = '[data-tid="message-body"], [data-tid="message-content"], [data-tid="chat-pane-message-content"], [data-tid="messageBody"]';
  10. const TITLE_SELECTORS = '[data-tid="chat-header-title"], [data-tid="conversation-title"], [data-tid="chat-title"], [data-tid="channel-title"]';
  11. export function isVisible(element) {
  12. if (!element || element.nodeType !== 1 || element.closest(SKIP)) return false;
  13. for (let current = element; current; current = current.parentElement) {
  14. if (current.hidden || current.getAttribute('aria-hidden') === 'true') return false;
  15. const style = current.ownerDocument?.defaultView?.getComputedStyle(current);
  16. if (style?.display === 'none' || style?.visibility === 'hidden' || style?.visibility === 'collapse' || style?.opacity === '0') return false;
  17. }
  18. return true;
  19. }
  20. export function visibleText(element, limit = 16000, { includeImages = false } = {}) {
  21. if (!isVisible(element)) return '';
  22. const parts = [];
  23. let size = 0;
  24. function visit(node, root = false) {
  25. if (size >= limit) return;
  26. if (node.nodeType === 3) {
  27. const text = node.nodeValue || '';
  28. parts.push(text.slice(0, limit - size));
  29. size += text.length;
  30. return;
  31. }
  32. if (node.nodeType !== 1 || !isVisible(node) || (!root && node.matches('button,[role="menu"],[role="toolbar"]'))) return;
  33. // Emoji rendered as images have no text node. Only include media inside a
  34. // verified message body, so avatars and surrounding controls stay excluded.
  35. if (includeImages && (node.tagName === 'IMG' || node.getAttribute('role') === 'img')) {
  36. const label = (node.getAttribute('alt') || node.getAttribute('aria-label') || node.getAttribute('title') || '').trim().slice(0, 200);
  37. if (label || node.tagName === 'IMG') {
  38. let text = label || '[图片]';
  39. if (/^https?:\/\/schema\.skype\.com\/CustomEmoji$/.test(node.getAttribute('itemtype') || '')) {
  40. const id = (node.getAttribute('itemid')?.split(';')[0] || label).replace(/^:+|:+$/g, '').trim();
  41. text = `:${/^[\p{L}\p{N}_+.-]{1,200}$/u.test(id) ? id : 'custom_emoji'}:`;
  42. }
  43. parts.push(text.slice(0, limit - size)); size += text.length;
  44. return;
  45. }
  46. }
  47. const block = /^(DIV|P|LI|BR|SECTION|ARTICLE|TIME|H[1-6])$/.test(node.tagName);
  48. if (block) parts.push('\n');
  49. for (const child of node.childNodes) visit(child);
  50. if (block) parts.push('\n');
  51. }
  52. visit(element, true);
  53. return parts.join('').replace(/[\t\f\r ]+/g, ' ').replace(/ *\n */g, '\n').replace(/\n{3,}/g, '\n\n').trim().slice(0, limit);
  54. }
  55. export function stableHash(text) {
  56. let hash = 14695981039346656037n;
  57. for (const char of String(text)) {
  58. hash ^= BigInt(char.codePointAt(0));
  59. hash = BigInt.asUintN(64, hash * 1099511628211n);
  60. }
  61. return hash.toString(16).padStart(16, '0');
  62. }
  63. export function safePageUrl(input) {
  64. try {
  65. const url = new URL(input);
  66. if (url.protocol !== 'https:' || !['teams.microsoft.com', 'teams.cloud.microsoft'].includes(url.hostname)) return '';
  67. // Queries/fragments may contain authentication data; never forward them.
  68. return `${url.origin}${url.pathname}`;
  69. } catch { return ''; }
  70. }
  71. function firstVisible(root, selectors) {
  72. return [...root.querySelectorAll(selectors)].find(isVisible) || null;
  73. }
  74. function contextFor(document, node, pageUrl, override) {
  75. const scope = node.closest('[data-conversation-id],[data-chat-id],[data-thread-id]');
  76. const id = scope?.getAttribute('data-conversation-id') || scope?.getAttribute('data-chat-id') || scope?.getAttribute('data-thread-id');
  77. const title = (typeof override?.title === 'string' ? override.title.slice(0, 300) : '') || visibleText(firstVisible(document, TITLE_SELECTORS), 300);
  78. return { id: id?.slice(0, 500) || (typeof override?.id === 'string' && override.id ? override.id.slice(0, 500) : `view:${stableHash(`${pageUrl}|${title}`)}`), title,
  79. automated: !id && Boolean(override?.id) };
  80. }
  81. function timestampFor(node) {
  82. const time = (node.matches('time') && isVisible(node) ? node : firstVisible(node, 'time,[data-tid="message-timestamp"],[data-tid="event-time"],[data-tid="timestamp"]'));
  83. const value = time?.getAttribute('datetime') || node.getAttribute('data-start-time') || '';
  84. const timeText = time ? visibleText(time, 160) : '';
  85. // A localized clock/day label has no timezone or full date. Preserve it, don't invent an instant.
  86. const fullIso = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/i.test(value);
  87. const parsed = fullIso ? Date.parse(value) : NaN;
  88. return { occurredAt: Number.isFinite(parsed) ? new Date(parsed).toISOString() : null, timeText: timeText || value.slice(0, 160), timestampResolution: Number.isFinite(parsed) ? 'explicit-datetime' : 'unresolved' };
  89. }
  90. function stableId(node, kind) {
  91. const attributes = kind === 'message' ? ['data-message-id', 'data-item-id'] : kind === 'event' ? ['data-event-id', 'data-item-id'] : ['data-notification-id', 'data-item-id'];
  92. for (const name of attributes) {
  93. const value = node.getAttribute(name) || firstVisible(node, `[${name}]`)?.getAttribute(name);
  94. if (value && value.length < 1000) return value;
  95. }
  96. const id = node.getAttribute('id');
  97. return id && new RegExp(`^(?:${kind}|chat-message)[-_:]`, 'i').test(id) ? id.slice(0, 1000) : null;
  98. }
  99. export function collectVisible(document, { url = document.location?.href || '', selectors = {}, includeActivity = false, maxRecords = 200, contextOverride, captureRoot } = {}) {
  100. const pageUrl = safePageUrl(url);
  101. const records = [];
  102. const errors = [];
  103. const counts = { message: 0, event: 0, activity: 0 };
  104. const identities = new Set();
  105. const automatic = captureRoot !== undefined;
  106. const validRoot = !automatic || (captureRoot?.ownerDocument === document && captureRoot.isConnected && isVisible(captureRoot));
  107. if (!validRoot) errors.push('已验证的消息列表已失效,自动采集已跳过本页');
  108. for (const kind of validRoot ? automatic ? ['message'] : ['message', 'event', ...(includeActivity ? ['activity'] : [])] : []) {
  109. const selector = selectors[kind]?.trim() || DEFAULT_SELECTORS[kind];
  110. let nodes;
  111. try { nodes = [...(captureRoot || document).querySelectorAll(selector)]; }
  112. catch { errors.push(`${kind}: CSS 选择器无效`); continue; }
  113. if (nodes.length > 2000) { errors.push(`${kind}: 匹配范围过大,请选择具体消息/事件容器`); continue; }
  114. const unsafeContainer = node => node.matches(BLOCKED_ROOT) || Boolean(node.querySelector('main,nav,[role="main"],[role="navigation"],[role="log"],[role="grid"]'));
  115. const candidates = nodes.filter(node => !unsafeContainer(node) && !node.closest('nav,[role="navigation"],[data-tid="chat-list"],[data-tid="chat-list-item"]') && isVisible(node));
  116. // Nested selector alternatives describe the same item; keep the outer semantic record.
  117. const uniqueNodes = candidates.filter(node => !candidates.some(other => other !== node && other.contains(node)));
  118. if (nodes.some(unsafeContainer)) errors.push(`${kind}: 已忽略整页或列表容器,请选择具体消息/事件元素`);
  119. if (uniqueNodes.length + records.length > Math.min(maxRecords, 200)) errors.push(`${kind}: 当前批次最多 200 条,请滚动到未采集的内容`);
  120. for (const node of uniqueNodes) {
  121. if (records.length >= Math.min(maxRecords, 200)) break;
  122. const content = kind === 'message' ? firstVisible(node, BODY_SELECTORS) || node : node;
  123. const text = visibleText(content, 16000, { includeImages: kind === 'message' && content !== node });
  124. const label = kind === 'event' && !text ? node.getAttribute('aria-label')?.slice(0, 2000) || '' : '';
  125. if (!text && !label) continue;
  126. const context = contextFor(document, node, pageUrl, contextOverride);
  127. const timing = timestampFor(node);
  128. const domId = stableId(node, kind);
  129. const externalId = domId ? `dom:${kind}:${stableHash(context.id)}:${domId}` : `fallback:${kind}:${stableHash(`${context.id}|${text || label}|${timing.timeText}`)}`;
  130. if (identities.has(externalId)) continue;
  131. identities.add(externalId);
  132. const author = kind === 'message' ? visibleText(firstVisible(node, '[data-tid="message-author-name"],[data-tid="message-author"],[data-tid="author"]'), 200) : '';
  133. const eventTitle = kind === 'event' ? visibleText(firstVisible(node, '[data-tid="event-title"],[data-tid="calendar-event-title"]'), 500) : '';
  134. records.push({
  135. kind, externalId, ...(kind === 'message' ? { parentId: context.id } : {}),
  136. title: kind === 'message' ? context.title || '聊天消息' : eventTitle || (text || label).split('\n')[0].slice(0, 500),
  137. text: text || label, occurredAt: timing.occurredAt, webUrl: pageUrl,
  138. raw: { captureMethod: 'visible-dom', selector, identityStrategy: domId ? 'dom-id' : 'content-context-hash', timestampResolution: timing.timestampResolution, timeText: timing.timeText, ...(author ? { author } : {}), context: context.id, ...(context.automated ? { automationContext: true } : {}) }
  139. });
  140. counts[kind]++;
  141. }
  142. }
  143. const views = Object.entries(counts).filter(([, count]) => count > 0).map(([kind]) => kind);
  144. const authenticatedShell = firstVisible(document, '[data-tid="app-bar"],[data-tid="app-layout"],[data-tid="chat-header-title"],[data-tid="calendar-header"]');
  145. const signedIn = records.length > 0 || authenticatedShell ? true : null;
  146. return {
  147. records, errors, counts,
  148. page: { url: pageUrl, title: 'Microsoft Teams', signedIn, view: views.join(',') || 'unknown', coverage: 'visible-loaded-dom-only' }
  149. };
  150. }