// Bounded DOM navigation for the extension-owned collector tab. No network or Chrome API access. import { DEFAULT_SELECTORS, isVisible, stableHash, visibleText } from './parser.js'; const LIST = '[data-tid="chat-list"],[data-tid="chat-list-container"],[data-tid="chat-list-viewport"],[data-tid="chat-list-scrollable-container"],[data-tid="chat-tree"],[data-tid="chat-list-tree"]'; const KNOWN_ROW = '[data-tid="chat-list-item"],[data-tid="chat-list-item-container"],[data-tid="chat-list-item-wrapper"],[data-tid="chat-list-item-row"],[data-tid="chat-item"],[data-tid="chat-tree-item"]'; const ROW = `${KNOWN_ROW},[role="treeitem"],[role="listitem"],[role="option"]`; const MESSAGE_LIST = '[data-tid="chat-pane-list"],[data-tid="message-pane-list-run"],[data-tid="message-pane-list"],[data-tid="chat-pane-message-list"],[role="log"]'; const TITLE = '[data-tid="chat-header-title"],[data-tid="conversation-title"],[data-tid="chat-title"]'; const ROW_TITLE = '[data-tid="chat-list-item-title"],[data-tid="chat-list-item-name"],[data-tid="chat-list-item-topic"],[data-tid="chat-title"]'; const SCOPE = '[data-conversation-id],[data-chat-id],[data-thread-id]'; const EXCLUDED = 'nav,form,[role="navigation"],[role="menu"],[role="menubar"],[role="toolbar"],[data-tid="app-bar"],[data-tid="message-editor"],[data-tid="message-compose"],input,textarea,select,[contenteditable]:not([contenteditable="false"]),[role="textbox"]'; const STRUCTURAL = 'html,body,main,nav,form,header,footer,[role="main"],[role="navigation"]'; const DEFAULTS = Object.freeze({ enabled: false, assigned: false, accountId: '', intervalSeconds: 120, dwellSeconds: 4, maxChats: 50, listScrollPasses: 8, historyScrollPasses: 2, chatItemSelector: '', chatListSelector: '', messageListSelector: '', messageSelector: '' }); const CANCELLED = Symbol('crawler-cancelled'); const compact = value => String(value || '').replace(/\s+/g, ' ').trim().slice(0, 300); const scopeId = node => node?.getAttribute('data-conversation-id') || node?.getAttribute('data-chat-id') || node?.getAttribute('data-thread-id') || ''; function optionsFor(input = {}) { const result = { ...DEFAULTS, enabled: input.enabled === true, assigned: input.assigned === true, accountId: String(input.accountId || '').slice(0, 200) }; for (const [key, min, max] of [['intervalSeconds', 30, 3600], ['dwellSeconds', 2, 30], ['maxChats', 1, 200], ['listScrollPasses', 0, 30], ['historyScrollPasses', 0, 20]]) { result[key] = Number.isInteger(input[key]) ? Math.min(max, Math.max(min, input[key])) : DEFAULTS[key]; } for (const key of ['chatItemSelector', 'chatListSelector', 'messageListSelector', 'messageSelector']) result[key] = typeof input[key] === 'string' ? input[key].trim().slice(0, 2000) : ''; return result; } export function createChatCrawler({ window, document, capture, report = () => {}, wait, now = () => Date.now() }) { if (typeof capture !== 'function') throw new TypeError('capture callback is required'); let options = optionsFor(), fingerprint = JSON.stringify(options), active = null, paused = false, disposed = false, navigating = false; let verified = null, uncertainTransition = null; let state = { running: false, phase: 'disabled', visited: 0, discovered: 0, pass: 0, nextRunAt: null, error: '', coverage: '' }; const enabled = () => options.enabled && options.assigned && !disposed; const status = () => ({ ...state }); function publish(update = {}) { state = { ...state, ...update }; try { Promise.resolve(report(status())).catch(() => {}); } catch { /* Diagnostics must not interfere with cancellation. */ } } function check(run) { if (disposed || run.controller.signal.aborted || active !== run) throw CANCELLED; } function abortable(promise, run, cleanup = () => {}) { check(run); return new Promise((resolve, reject) => { const signal = run.controller.signal; const abort = () => { cleanup(); signal.removeEventListener('abort', abort); reject(CANCELLED); }; signal.addEventListener('abort', abort, { once: true }); Promise.resolve(promise).then(value => { signal.removeEventListener('abort', abort); resolve(value); }, error => { signal.removeEventListener('abort', abort); reject(error); }); if (signal.aborted) abort(); }); } async function delay(ms, run) { check(run); let timer; const pending = wait ? wait(ms, { signal: run.controller.signal }) : new Promise(resolve => { timer = window.setTimeout(resolve, ms); }); await abortable(pending, run, () => { if (timer !== undefined) window.clearTimeout(timer); }); check(run); } function select(root, selector, label, errors) { try { const nodes = [...root.querySelectorAll(selector)]; if (nodes.length > 2000) { errors.push(`${label}匹配超过 2000 个元素,请缩小选择器范围`); return []; } return nodes; } catch { errors.push(`${label} CSS 选择器无效`); return []; } } const safeContainer = node => node && !node.matches(STRUCTURAL) && !node.closest(EXCLUDED) && isVisible(node); function findList(errors = [], settings = options) { const candidates = select(document, settings.chatListSelector || LIST, '聊天列表', errors); const safe = candidates.filter(safeContainer); if (candidates.length && !safe.length) errors.push('聊天列表选择器指向整页、导航或编辑区域,已忽略'); const withRows = safe.filter(node => select(node, settings.chatItemSelector || ROW, '聊天行', errors).length); return withRows.find(node => scrollMax(node) > 0) || withRows[0] || safe[0] || null; } function routeIdentity(row) { const anchors = row.matches('a[href]') ? [row] : [...row.querySelectorAll('a[href]')]; for (const anchor of anchors) { if (anchor.closest('[role="menu"],[role="toolbar"]')) continue; try { const url = new URL(anchor.getAttribute('href'), document.location.href); if (url.protocol !== 'https:' || !['teams.microsoft.com', 'teams.cloud.microsoft'].includes(url.hostname)) continue; // Inspect only chat route identifiers; never store arbitrary queries/fragments. const direct = url.pathname.match(/^\/(?:l\/)?chat\/([^/]+)(?:\/|$)/i); if (direct && direct[1] !== '0') return { key: `conversation:${decodeURIComponent(direct[1])}`, conversationId: decodeURIComponent(direct[1]) }; if (!/^\/v2\/?$/i.test(url.pathname)) continue; for (const key of ['chatId', 'threadId', 'conversationId']) { const id = url.searchParams.get(key); if (id && id.length <= 500) return { key: `conversation:${id}`, conversationId: id }; } } catch { /* A malformed link is not a navigation target. */ } } return null; } function identity(row) { const id = scopeId(row); if (id && id.length <= 500) return { key: `conversation:${id}`, conversationId: id }; const itemId = row.getAttribute('data-item-id'); if (itemId && itemId.length <= 500 && !/^\d+$/.test(itemId)) return { key: `item:${itemId}`, conversationId: '' }; const route = routeIdentity(row); if (route) return route; const domId = row.id; if (/^(?:chat|thread|conversation)[-_:]/i.test(domId) && domId.length <= 500) return { key: `element:${domId}`, conversationId: '' }; return null; // Display names and virtual-list indices are not stable identities. } function safeRow(row, list) { if (!list || !row.isConnected || row === list || !list.contains(row) || !isVisible(row) || row.matches(STRUCTURAL) || row.closest(EXCLUDED)) return false; if (row.matches('input,textarea,select,summary,[role="menuitem"],[role="tab"],[aria-haspopup]')) return false; if (row.matches('button,[role="button"]') && !row.matches(KNOWN_ROW)) return false; if (row.matches('button[type="submit"]') || row.querySelector('form,main,nav,[role="main"],[role="navigation"]')) return false; // A grouping tree node can expand/collapse many conversations; never activate it. if (row.querySelector('[role="treeitem"]')) return false; if (row.matches('a[href]') && !routeIdentity(row)) return false; return true; } function rows(list, errors = [], settings = options) { if (!list) return []; const nodes = select(list, settings.chatItemSelector || ROW, '聊天行', errors); const found = new Map(); let unidentified = 0, unsafe = 0; for (const row of nodes) { if (!safeRow(row, list)) { unsafe++; continue; } const id = identity(row); if (!id) { unidentified++; continue; } const title = [...row.querySelectorAll(ROW_TITLE)].find(isVisible); const label = compact(visibleText(title, 300) || row.getAttribute('aria-label') || visibleText(row, 300)); const item = { ...id, label, row }; const previous = found.get(item.key); if (!previous || previous.row.contains(row)) found.set(item.key, item); } if (unidentified) errors.push(`${unidentified} 个聊天行缺少稳定聊天标识,已忽略(名称不能用于区分同名聊天)`); if (unsafe) errors.push(`${unsafe} 个匹配元素属于按钮、分组或不安全区域,已忽略`); return [...found.values()]; } function preview(overrides) { const settings = optionsFor({ ...options, ...overrides }); const errors = [], list = findList(errors, settings), found = rows(list, errors, settings); if (!list) errors.push('未识别聊天列表;请在采集页打开 Teams 聊天列表,或配置聊天列表选择器'); else if (!found.length) errors.push('未识别带稳定标识的聊天行;请检查聊天行选择器'); return { count: found.length, labels: found.slice(0, 20).map(item => item.label), errors: [...new Set(errors)] }; } const scrollMax = node => Math.max(0, Number(node.scrollHeight) - Number(node.clientHeight)); function findMessageList(errors = []) { const nodes = select(document, options.messageListSelector || MESSAGE_LIST, '消息列表', errors).filter(node => safeContainer(node) && !node.closest(LIST)); const withMessages = nodes.filter(node => select(node, options.messageSelector || DEFAULT_SELECTORS.message, '消息', errors).length); return withMessages.find(node => scrollMax(node) > 0) || withMessages[0] || nodes[0] || null; } function snapshot() { const errors = [], pane = findMessageList(errors); const candidates = pane ? select(pane, options.messageSelector || DEFAULT_SELECTORS.message, '消息', errors).filter(isVisible) : []; const unique = candidates.filter(node => !candidates.some(other => other !== node && other.contains(node))).slice(0, 200); const messages = unique.map(node => { const id = node.getAttribute('data-message-id') || node.getAttribute('data-item-id') || node.id || ''; const hash = stableHash(visibleText(node, 16000)); return { key: id ? `id:${id}` : `text:${hash}`, hash }; }); const keys = messages.map(message => message.key); const titles = [...document.querySelectorAll(TITLE)].filter(isVisible).map(node => compact(visibleText(node, 300))); const scopes = [...new Set(unique.map(node => scopeId(node.closest(SCOPE))).filter(Boolean))]; const paneScope = scopeId(pane?.closest(SCOPE)); const conversationId = scopes.length === 1 ? scopes[0] : paneScope; const mixedScopes = scopes.length > 1 || Boolean(paneScope && scopes.some(id => id !== paneScope)); return { pane, keys, titles, conversationId, mixedScopes, errors, signature: stableHash(JSON.stringify([messages, titles, conversationId, mixedScopes])) }; } const rowSelected = row => Boolean(row && (row.matches('[aria-selected="true"],[aria-current="true"],[aria-current="page"],[data-is-selected="true"]') || row.querySelector('[aria-selected="true"],[data-is-selected="true"]'))); function selected(target) { const list = findList(); const row = rows(list).find(item => item.key === target.key)?.row; return rowSelected(row); } function pointsAt(target, view) { if (view.mixedScopes || (target.conversationId && view.conversationId && target.conversationId !== view.conversationId)) return false; return selected(target) || Boolean(target.conversationId && view.conversationId === target.conversationId) || Boolean(target.label && view.titles.includes(target.label)); } function previouslyVerified(target, view) { return Boolean(verified && verified.target.key === target.key && verified.pane === view.pane && verified.signature === view.signature && pointsAt(target, view)); } const explicitScope = (target, view) => Boolean(target.conversationId && !view.mixedScopes && view.conversationId === target.conversationId && // Remember an interrupted transition even when resume clears the paused flag: // its pane metadata may have changed before the old message nodes disappear. (!uncertainTransition || uncertainTransition.key !== target.key || view.keys.every(key => !uncertainTransition.keys.has(key)))); function captureContext() { if (!verified || navigating || disposed) return null; const view = snapshot(); if (view.pane !== verified.pane || !view.keys.length || view.titles.join('\n') !== verified.titles.join('\n') || view.conversationId !== verified.paneId || !pointsAt(verified.target, view)) return null; return { ...verified.context, root: view.pane }; } function remember(target, view, previousContext) { const context = previousContext ? { id: previousContext.id, title: previousContext.title } : { id: view.conversationId || target.conversationId || `auto-row:${stableHash(target.key)}`, title: view.titles[0] || target.label || '聊天消息' }; verified = { target, titles: view.titles, pane: view.pane, paneId: view.conversationId, signature: view.signature, context }; if (uncertainTransition?.key === target.key) uncertainTransition = null; } async function settle(target, before, run, { alreadySelected = false, history = false } = {}) { const start = now(), dwell = options.dwellSeconds * 1000, timeout = Math.max(15000, dwell * 3 + 4000); let lastSignature = '', stableSince = null; while (now() - start <= timeout) { check(run); const view = snapshot(); if (view.errors.length) throw new Error(view.errors[0]); const old = new Set(before.keys); // A changed header/selected row or relabelled pane may coexist with old messages. // Require *all* old message fingerprints to disappear before capturing a new chat. const fresh = alreadySelected || history || (view.keys.length > 0 && view.keys.every(key => !old.has(key))); const sameHistory = !history || (view.titles.join('\n') === before.titles.join('\n') && view.conversationId === before.conversationId); const titleChangedToTarget = target.label && view.titles.includes(target.label) && view.titles.join('\n') !== before.titles.join('\n'); const selectionEvidence = alreadySelected || history || selected(target) || Boolean(target.conversationId && view.conversationId === target.conversationId) || titleChangedToTarget; const ready = view.pane && view.keys.length > 0 && fresh && sameHistory && selectionEvidence && pointsAt(target, view); if (ready && view.signature === lastSignature) { if (stableSince !== null && now() - stableSince >= dwell) return view; } else { lastSignature = view.signature; stableSince = ready ? now() : null; } await delay(250, run); } throw new Error(history ? '消息列表滚动后未能确认同一聊天稳定显示,自动采集已暂停' : '聊天切换后未能确认新消息已加载(可能为空聊天或页面结构未识别),已暂停以避免保存上一聊天内容'); } async function drain(run, requireVerified = false) { publish({ phase: 'capturing' }); // Queue batches are smaller than a full DOM scan. Retry draining without changing chats. for (let attempts = 0; attempts < 5; attempts++) { check(run); if (requireVerified && !captureContext()) throw new Error('消息列表已离开当前聊天,自动采集已暂停'); let result; try { result = await abortable(capture(), run); } catch (error) { if (error === CANCELLED) throw error; throw new Error('本地数据交付失败,已暂停;请检查本地服务后重新连接'); } check(run); if (!result || result.ok !== true || !Number.isInteger(result.pending) || result.pending < 0) throw new Error('本地数据交付失败,已暂停;请检查本地服务后重新连接'); if (result.pending === 0) return; await delay(250, run); } throw new Error('待保存队列未能排空,已暂停自动切换聊天;请检查本地服务后重新连接'); } async function discover(run) { publish({ phase: 'discovering' }); navigating = true; const found = new Map(), errors = []; let list = findList(errors); if (!list) throw new Error(errors[0] || '未识别聊天列表;请在采集页打开 Teams 聊天列表,或配置聊天列表选择器'); if (list.scrollTop !== 0 && scrollMax(list) > 0) { list.scrollTop = 0; await delay(350, run); } let scrolls = 0, capped = false; for (;;) { check(run); list = findList(errors); if (!list) throw new Error('聊天列表在采集过程中消失,已暂停'); for (const item of rows(list, errors)) { if (!found.has(item.key) && found.size >= options.maxChats) { capped = true; break; } // Retain identity and label only: a virtualized row is not a lasting DOM reference. found.set(item.key, { key: item.key, conversationId: item.conversationId, label: item.label, wasSelected: rowSelected(item.row) || found.get(item.key)?.wasSelected || false }); } publish({ discovered: found.size }); const max = scrollMax(list), next = Math.min(max, Number(list.scrollTop) + Math.max(Number(list.clientHeight) * 0.8, 200)); if (found.size >= options.maxChats) { capped ||= next > list.scrollTop; break; } if (scrolls >= options.listScrollPasses || next <= list.scrollTop) break; list.scrollTop = next; scrolls++; await delay(350, run); } if (!found.size) throw new Error(errors[0] || '未识别带稳定标识的聊天行;请检查聊天行选择器'); const limited = capped || scrollMax(list) > list.scrollTop; run.notes = [`本轮最多 ${options.maxChats} 个聊天;列表滚动最多 ${options.listScrollPasses} 次;每个聊天向上滚动最多 ${options.historyScrollPasses} 次`, '仅已发现且可加载的消息,不保证完整历史', ...(limited ? ['已达到聊天数或列表滚动范围限制'] : []), ...[...new Set(errors)].slice(0, 2)]; navigating = false; return [...found.values()]; } async function refind(target, run) { let list = findList(); let match = rows(list).find(item => item.key === target.key); if (match) return match.row; if (!list) throw new Error('聊天列表在采集过程中消失,已暂停'); if (list.scrollTop !== 0 && scrollMax(list) > 0) { list.scrollTop = 0; await delay(350, run); } for (let pass = 0; pass <= options.listScrollPasses; pass++) { check(run); list = findList(); if (!list) throw new Error('聊天列表在采集过程中消失,已暂停'); match = rows(list).find(item => item.key === target.key); if (match) return match.row; if (pass === options.listScrollPasses) break; const next = Math.min(scrollMax(list), Number(list.scrollTop) + Math.max(Number(list.clientHeight) * 0.8, 200)); if (next <= list.scrollTop) break; list.scrollTop = next; await delay(350, run); } return null; } async function traverse(run) { const targets = await discover(run); const initial = snapshot(); // The initial selected row might still contain the previous chat's DOM after a // cancelled transition. Visit another conversation first to get fresh evidence. const uncertain = target => (target.wasSelected || selected(target)) && !explicitScope(target, initial) && !previouslyVerified(target, initial); targets.sort((left, right) => Number(uncertain(left)) - Number(uncertain(right))); for (const target of targets) { check(run); // Flush the preceding, still verified view before any navigation. await drain(run); navigating = true; const row = await refind(target, run); check(run); if (!row || !row.isConnected || identity(row)?.key !== target.key || !safeRow(row, findList())) { run.notes.push('部分聊天行在虚拟列表中无法重新定位,已跳过'); navigating = false; continue; } const before = snapshot(), alreadySelected = selected(target) && before.keys.length > 0 && (explicitScope(target, before) || previouslyVerified(target, before)); verified = null; publish({ phase: 'opening' }); if (!alreadySelected) { uncertainTransition = { key: target.key, keys: new Set(before.keys) }; row.click(); } check(run); publish({ phase: 'settling' }); const loaded = await settle(target, before, run, { alreadySelected }); remember(target, loaded); // A previous pass can leave this chat scrolled into history. Always load the // newest viewport first; this does not consume the bounded history budget. if (scrollMax(loaded.pane) > 0 && loaded.pane.scrollTop < scrollMax(loaded.pane)) { loaded.pane.scrollTop = scrollMax(loaded.pane); await delay(350, run); const latest = await settle(target, loaded, run, { history: true }); remember(target, latest, verified.context); } navigating = false; await drain(run, true); for (let historyPass = 0; historyPass < options.historyScrollPasses; historyPass++) { check(run); const view = snapshot(), pane = view.pane; if (!pane || pane.scrollTop <= 0 || scrollMax(pane) <= 0) break; const context = captureContext(); if (!context) throw new Error('消息列表已离开当前聊天,自动采集已暂停'); navigating = true; verified = null; publish({ phase: 'history' }); pane.scrollTop = Math.max(0, Number(pane.scrollTop) - Math.max(Number(pane.clientHeight) * 0.8, 200)); // Give asynchronous scroll handlers a turn before beginning the stability window. await delay(350, run); const historical = await settle(target, view, run, { history: true }); remember(target, historical, context); navigating = false; await drain(run, true); } publish({ visited: state.visited + 1, coverage: [...new Set(run.notes)].join(';').slice(0, 500) }); } } async function tick() { if (!enabled() || paused || active || (state.nextRunAt && now() < Date.parse(state.nextRunAt))) return status(); const run = { controller: new window.AbortController(), notes: [] }; active = run; publish({ running: true, phase: 'discovering', visited: 0, discovered: 0, pass: state.pass + 1, nextRunAt: null, error: '', coverage: '' }); try { await traverse(run); check(run); publish({ running: false, phase: 'waiting', nextRunAt: new Date(now() + options.intervalSeconds * 1000).toISOString(), coverage: [...new Set(run.notes)].join(';').slice(0, 500) }); } catch (error) { if (error !== CANCELLED && active === run) { paused = true; verified = null; publish({ running: false, phase: 'error', nextRunAt: null, error: String(error.message || error).slice(0, 500), coverage: [...new Set(run.notes)].join(';').slice(0, 500) }); } } finally { if (active === run) { active = null; navigating = false; } } return status(); } function stop(reason = '自动采集已暂停') { paused = true; active?.controller.abort(); navigating = false; verified = null; publish({ running: false, phase: enabled() ? 'paused' : 'disabled', nextRunAt: null, error: String(reason || '').slice(0, 500) }); } function configure(input) { const next = optionsFor(input), nextFingerprint = JSON.stringify(next); if (nextFingerprint === fingerprint || disposed) return status(); active?.controller.abort(); verified = null; navigating = false; options = next; fingerprint = nextFingerprint; paused = false; publish({ running: false, phase: enabled() ? 'idle' : 'disabled', visited: 0, discovered: 0, nextRunAt: null, error: '', coverage: '' }); return status(); } // Called only for a user-requested reconnect/configuration action, never a heartbeat. function resume() { if (!disposed && (!active || active.controller.signal.aborted) && enabled()) { paused = false; publish({ phase: 'idle', running: false, nextRunAt: null, error: '' }); } return status(); } const onInteraction = event => { if (event.isTrusted && enabled()) stop('检测到你在采集页操作,自动采集已暂停;请重新连接后继续'); }; document.addEventListener('pointerdown', onInteraction, true); document.addEventListener('keydown', onInteraction, true); function dispose() { if (disposed) return; disposed = true; stop(''); document.removeEventListener('pointerdown', onInteraction, true); document.removeEventListener('keydown', onInteraction, true); } return { configure, tick, stop, resume, status, isNavigating: () => navigating, captureContext, dispose, preview }; }