// Content script on Teams web: logs active speaker, shares, captions, chat, hands, presence while recording. // Events go via the offscreen document (it has host_permissions for 127.0.0.1). Never clicks or types in the call. const SEL = { duration: '[data-tid="call-duration"]', nametag: '[data-tid="participant-info-nametag"]', info: '[data-tid="participant-info"]', speaking: '[data-tid="voice-level-stream-outline"][data-is-speaking="true"]', self: '[data-tid="myself-video"]', shareAria: /^(Contenuto condiviso da|Content shared by) (.+)$/, caption: '[data-tid="author"],[data-tid="closed-caption-text"]', // DOM order; author only on first row of a speaker's run chatMsg: null, chatAuthor: null, chatText: null, // discovery step hand: null, // discovery step }; let buf = [], timers = [], seenCap = new WeakMap(), capAuthor = "", seenChat = new WeakSet(), lastShare = "", raised = new Set(), lastTitle = null; const norm = raw => { raw = (raw || "").trim(); const m = raw.match(/^([^,]+),\s*([^(]+?)\s*(\(.*)?$/); if (!m) return raw; return `${m[2]} ${m[1]}`.split(/\s+/).map(w => (w.length > 1 && w === w.toUpperCase() ? w[0] + w.slice(1).toLowerCase() : w)).join(" "); }; const tileOf = el => { while (el && !el.querySelector(SEL.nametag)) el = el.parentElement; return el; }; const keyOf = tile => { for (let e = tile; e; e = e.parentElement) if ((e.dataset?.tid || "").includes("@")) return e.dataset.tid; return tile.querySelector(SEL.nametag)?.textContent.trim() || ""; }; const nameOf = tile => norm(tile.querySelector(SEL.nametag)?.textContent); const selfName = () => { const s = document.querySelector(SEL.self); return s ? nameOf(s) : ""; }; const emit = (kind, who_key = "", who = "", text = "") => buf.push({ ts: Date.now(), kind, who_key, who, text }); const guard = f => () => { try { f(); } catch (e) { console.error("[teams-whisper]", e); } }; const send = () => { if (buf.length) chrome.runtime.sendMessage({ to: "off", cmd: "events", events: buf.splice(0) }).catch(() => { }); }; const tiles = () => [...document.querySelectorAll(SEL.nametag)].map(tileOf).filter(Boolean); function meta() { const title = document.title.replace(/^[^|]*\|\s*/, "").replace(/\s*\|\s*Microsoft Teams$/, ""); if (title === lastTitle) return; lastTitle = title; emit("meta", "", "", JSON.stringify({ title, call_elapsed: document.querySelector(SEL.duration)?.textContent.trim() || "", url: location.href })); } function present() { const seen = new Map(); for (const t of tiles()) seen.set(keyOf(t), { key: keyOf(t), who: t.closest(SEL.self) ? selfName() : nameOf(t) }); emit("present", "", "", JSON.stringify([...seen.values()])); } function active() { for (const el of document.querySelectorAll(SEL.speaking)) { const t = tileOf(el); if (!t) continue; if (t.closest(SEL.self) || t.matches(SEL.self)) emit("active", "self", selfName()); else emit("active", keyOf(t), nameOf(t)); } } function share() { const names = tiles().map(nameOf); let cur = ""; for (const el of document.querySelectorAll("[aria-label]")) { const m = el.getAttribute("aria-label").match(SEL.shareAria); if (m) { cur = names.find(n => n && m[2].startsWith(n)) || m[2]; break; } } if (cur === lastShare) return; if (lastShare) emit("share_stop", "", lastShare); if (cur) emit("share_start", "", cur); lastShare = cur; } function messages() { if (SEL.caption) for (const el of document.querySelectorAll(SEL.caption)) { if (el.getAttribute("data-tid") === "author") { capAuthor = norm(el.textContent); continue; } // caption text grows while spoken: emit once it was unchanged for a full tick const text = el.textContent.trim(), s = seenCap.get(el) || {}; if (s.done) continue; if (text && s.text === text) { s.done = true; emit("caption", "", capAuthor, text); } s.text = text; seenCap.set(el, s); } if (SEL.chatMsg) for (const el of document.querySelectorAll(SEL.chatMsg)) { if (seenChat.has(el)) continue; seenChat.add(el); const text = el.querySelector(SEL.chatText)?.textContent.trim(); if (text) emit("chat", "", norm(el.querySelector(SEL.chatAuthor)?.textContent), text); } if (SEL.hand) { const now = new Set(); for (const el of document.querySelectorAll(SEL.hand)) { const t = tileOf(el); if (!t) continue; const k = keyOf(t); now.add(k); if (!raised.has(k)) emit("hand", k, nameOf(t)); } raised = now; } } function stop() { timers.forEach(clearInterval); timers = []; send(); if (lastShare) { emit("share_stop", "", lastShare); lastShare = ""; } send(); } function start() { stop(); buf = []; seenCap = new WeakMap(); capAuthor = ""; seenChat = new WeakSet(); raised = new Set(); lastTitle = null; lastShare = ""; meta(); present(); timers = [ setInterval(guard(() => { meta(); active(); }), 500), setInterval(guard(share), 1000), setInterval(guard(messages), 1000), setInterval(guard(present), 30000), setInterval(guard(send), 5000), ]; } chrome.runtime.onMessage.addListener(m => { if (m.cmd === "cap-start") start(); if (m.cmd === "cap-stop") stop(); });