| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- // Pure DOM parsing: no network, storage, cookie, token, or input value access.
- export const DEFAULT_SELECTORS = Object.freeze({
- 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"]',
- 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"]',
- activity: '[data-tid="activity-feed-item"], [data-tid="notification-list-item"]'
- });
- const SKIP = 'script,style,noscript,input,textarea,select,option,[contenteditable]:not([contenteditable="false"]),[role="textbox"]';
- const BLOCKED_ROOT = 'html,body,main,nav,form,[role="main"],[role="navigation"],[role="log"],[role="grid"]';
- const BODY_SELECTORS = '[data-tid="message-body"], [data-tid="message-content"], [data-tid="chat-pane-message-content"], [data-tid="messageBody"]';
- const TITLE_SELECTORS = '[data-tid="chat-header-title"], [data-tid="conversation-title"], [data-tid="chat-title"], [data-tid="channel-title"]';
- export function isVisible(element) {
- if (!element || element.nodeType !== 1 || element.closest(SKIP)) return false;
- for (let current = element; current; current = current.parentElement) {
- if (current.hidden || current.getAttribute('aria-hidden') === 'true') return false;
- const style = current.ownerDocument?.defaultView?.getComputedStyle(current);
- if (style?.display === 'none' || style?.visibility === 'hidden' || style?.visibility === 'collapse' || style?.opacity === '0') return false;
- }
- return true;
- }
- export function visibleText(element, limit = 16000, { includeImages = false } = {}) {
- if (!isVisible(element)) return '';
- const parts = [];
- let size = 0;
- function visit(node, root = false) {
- if (size >= limit) return;
- if (node.nodeType === 3) {
- const text = node.nodeValue || '';
- parts.push(text.slice(0, limit - size));
- size += text.length;
- return;
- }
- if (node.nodeType !== 1 || !isVisible(node) || (!root && node.matches('button,[role="menu"],[role="toolbar"]'))) return;
- // Emoji rendered as images have no text node. Only include media inside a
- // verified message body, so avatars and surrounding controls stay excluded.
- if (includeImages && (node.tagName === 'IMG' || node.getAttribute('role') === 'img')) {
- const label = (node.getAttribute('alt') || node.getAttribute('aria-label') || node.getAttribute('title') || '').trim().slice(0, 200);
- if (label || node.tagName === 'IMG') {
- let text = label || '[图片]';
- if (/^https?:\/\/schema\.skype\.com\/CustomEmoji$/.test(node.getAttribute('itemtype') || '')) {
- const id = (node.getAttribute('itemid')?.split(';')[0] || label).replace(/^:+|:+$/g, '').trim();
- text = `:${/^[\p{L}\p{N}_+.-]{1,200}$/u.test(id) ? id : 'custom_emoji'}:`;
- }
- parts.push(text.slice(0, limit - size)); size += text.length;
- return;
- }
- }
- const block = /^(DIV|P|LI|BR|SECTION|ARTICLE|TIME|H[1-6])$/.test(node.tagName);
- if (block) parts.push('\n');
- for (const child of node.childNodes) visit(child);
- if (block) parts.push('\n');
- }
- visit(element, true);
- return parts.join('').replace(/[\t\f\r ]+/g, ' ').replace(/ *\n */g, '\n').replace(/\n{3,}/g, '\n\n').trim().slice(0, limit);
- }
- export function stableHash(text) {
- let hash = 14695981039346656037n;
- for (const char of String(text)) {
- hash ^= BigInt(char.codePointAt(0));
- hash = BigInt.asUintN(64, hash * 1099511628211n);
- }
- return hash.toString(16).padStart(16, '0');
- }
- export function safePageUrl(input) {
- try {
- const url = new URL(input);
- if (url.protocol !== 'https:' || !['teams.microsoft.com', 'teams.cloud.microsoft'].includes(url.hostname)) return '';
- // Queries/fragments may contain authentication data; never forward them.
- return `${url.origin}${url.pathname}`;
- } catch { return ''; }
- }
- function firstVisible(root, selectors) {
- return [...root.querySelectorAll(selectors)].find(isVisible) || null;
- }
- function contextFor(document, node, pageUrl, override) {
- const scope = node.closest('[data-conversation-id],[data-chat-id],[data-thread-id]');
- const id = scope?.getAttribute('data-conversation-id') || scope?.getAttribute('data-chat-id') || scope?.getAttribute('data-thread-id');
- const title = (typeof override?.title === 'string' ? override.title.slice(0, 300) : '') || visibleText(firstVisible(document, TITLE_SELECTORS), 300);
- return { id: id?.slice(0, 500) || (typeof override?.id === 'string' && override.id ? override.id.slice(0, 500) : `view:${stableHash(`${pageUrl}|${title}`)}`), title,
- automated: !id && Boolean(override?.id) };
- }
- function timestampFor(node) {
- const time = (node.matches('time') && isVisible(node) ? node : firstVisible(node, 'time,[data-tid="message-timestamp"],[data-tid="event-time"],[data-tid="timestamp"]'));
- const value = time?.getAttribute('datetime') || node.getAttribute('data-start-time') || '';
- const timeText = time ? visibleText(time, 160) : '';
- // A localized clock/day label has no timezone or full date. Preserve it, don't invent an instant.
- const fullIso = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/i.test(value);
- const parsed = fullIso ? Date.parse(value) : NaN;
- return { occurredAt: Number.isFinite(parsed) ? new Date(parsed).toISOString() : null, timeText: timeText || value.slice(0, 160), timestampResolution: Number.isFinite(parsed) ? 'explicit-datetime' : 'unresolved' };
- }
- function stableId(node, kind) {
- const attributes = kind === 'message' ? ['data-message-id', 'data-item-id'] : kind === 'event' ? ['data-event-id', 'data-item-id'] : ['data-notification-id', 'data-item-id'];
- for (const name of attributes) {
- const value = node.getAttribute(name) || firstVisible(node, `[${name}]`)?.getAttribute(name);
- if (value && value.length < 1000) return value;
- }
- const id = node.getAttribute('id');
- return id && new RegExp(`^(?:${kind}|chat-message)[-_:]`, 'i').test(id) ? id.slice(0, 1000) : null;
- }
- export function collectVisible(document, { url = document.location?.href || '', selectors = {}, includeActivity = false, maxRecords = 200, contextOverride, captureRoot } = {}) {
- const pageUrl = safePageUrl(url);
- const records = [];
- const errors = [];
- const counts = { message: 0, event: 0, activity: 0 };
- const identities = new Set();
- const automatic = captureRoot !== undefined;
- const validRoot = !automatic || (captureRoot?.ownerDocument === document && captureRoot.isConnected && isVisible(captureRoot));
- if (!validRoot) errors.push('已验证的消息列表已失效,自动采集已跳过本页');
- for (const kind of validRoot ? automatic ? ['message'] : ['message', 'event', ...(includeActivity ? ['activity'] : [])] : []) {
- const selector = selectors[kind]?.trim() || DEFAULT_SELECTORS[kind];
- let nodes;
- try { nodes = [...(captureRoot || document).querySelectorAll(selector)]; }
- catch { errors.push(`${kind}: CSS 选择器无效`); continue; }
- if (nodes.length > 2000) { errors.push(`${kind}: 匹配范围过大,请选择具体消息/事件容器`); continue; }
- const unsafeContainer = node => node.matches(BLOCKED_ROOT) || Boolean(node.querySelector('main,nav,[role="main"],[role="navigation"],[role="log"],[role="grid"]'));
- const candidates = nodes.filter(node => !unsafeContainer(node) && !node.closest('nav,[role="navigation"],[data-tid="chat-list"],[data-tid="chat-list-item"]') && isVisible(node));
- // Nested selector alternatives describe the same item; keep the outer semantic record.
- const uniqueNodes = candidates.filter(node => !candidates.some(other => other !== node && other.contains(node)));
- if (nodes.some(unsafeContainer)) errors.push(`${kind}: 已忽略整页或列表容器,请选择具体消息/事件元素`);
- if (uniqueNodes.length + records.length > Math.min(maxRecords, 200)) errors.push(`${kind}: 当前批次最多 200 条,请滚动到未采集的内容`);
- for (const node of uniqueNodes) {
- if (records.length >= Math.min(maxRecords, 200)) break;
- const content = kind === 'message' ? firstVisible(node, BODY_SELECTORS) || node : node;
- const text = visibleText(content, 16000, { includeImages: kind === 'message' && content !== node });
- const label = kind === 'event' && !text ? node.getAttribute('aria-label')?.slice(0, 2000) || '' : '';
- if (!text && !label) continue;
- const context = contextFor(document, node, pageUrl, contextOverride);
- const timing = timestampFor(node);
- const domId = stableId(node, kind);
- const externalId = domId ? `dom:${kind}:${stableHash(context.id)}:${domId}` : `fallback:${kind}:${stableHash(`${context.id}|${text || label}|${timing.timeText}`)}`;
- if (identities.has(externalId)) continue;
- identities.add(externalId);
- const author = kind === 'message' ? visibleText(firstVisible(node, '[data-tid="message-author-name"],[data-tid="message-author"],[data-tid="author"]'), 200) : '';
- const eventTitle = kind === 'event' ? visibleText(firstVisible(node, '[data-tid="event-title"],[data-tid="calendar-event-title"]'), 500) : '';
- records.push({
- kind, externalId, ...(kind === 'message' ? { parentId: context.id } : {}),
- title: kind === 'message' ? context.title || '聊天消息' : eventTitle || (text || label).split('\n')[0].slice(0, 500),
- text: text || label, occurredAt: timing.occurredAt, webUrl: pageUrl,
- 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 } : {}) }
- });
- counts[kind]++;
- }
- }
- const views = Object.entries(counts).filter(([, count]) => count > 0).map(([kind]) => kind);
- const authenticatedShell = firstVisible(document, '[data-tid="app-bar"],[data-tid="app-layout"],[data-tid="chat-header-title"],[data-tid="calendar-header"]');
- const signedIn = records.length > 0 || authenticatedShell ? true : null;
- return {
- records, errors, counts,
- page: { url: pageUrl, title: 'Microsoft Teams', signedIn, view: views.join(',') || 'unknown', coverage: 'visible-loaded-dom-only' }
- };
- }
|