// Popup controller: Start/Stop button, status line and transcript view; talks to the offscreen recorder. // Shorthand for getElementById. const $ = id => document.getElementById(id); // Forward a command to the offscreen document (addressed with to:"off"). const off = msg => chrome.runtime.sendMessage({ to: "off", ...msg }); // Format milliseconds as m:ss. const mmss = ms => { const s = Math.floor(ms / 1000); return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`; }; // Last error shown when there is no offscreen document to report it. let S = { state: "idle" }, lastErr = ""; // Refresh the button and status line from state S. function render() { const go = $("go"), st = $("st"); // Pipeline busy after stop: no new start until it finishes. go.disabled = S.state === "transcribing"; // Button doubles as Stop while recording. go.textContent = S.state === "recording" ? "Stop + transcribe" : "Start"; // Status line text per recorder state. st.textContent = { idle: "idle - open a Teams tab and press Start", recording: `REC ${mmss(Date.now() - S.t0)} | ${S.chunks} chunks uploaded${S.failed ? ` | ${S.failed} FAILED (server down?)` : ""} | mic ${S.mic ? "on" : "OFF (grant it)"} | eventi call: ${S.evSent || 0}`, transcribing: `invio alla pipeline...`, done: `inviato alla pipeline — stato e report su http://127.0.0.1:18765/`, error: "ERROR (see below)", }[S.state]; // Show the transcript/result text when there is one. if (S.text) $("txt").textContent = S.text; } // Poll the recorder state every second. async function poll() { // Offscreen doc exists: ask it for its state. Otherwise show the last error, or idle. if (await chrome.offscreen.hasDocument()) S = (await off({ cmd: "status" })) || S; else S = lastErr ? { state: "error", text: lastErr } : { state: "idle" }; render(); } // Start / Stop click handler. $("go").onclick = async () => { // Stop path: first ask teams.js to stop its scanners. if (S.state === "recording") { // Tell the Teams content script to stop (errors ignored: the tab may be gone). if (S.tabId != null) await chrome.tabs.sendMessage(S.tabId, { cmd: "cap-stop" }).catch(() => {}); // Give teams.js's final event batch time to reach the offscreen doc before the recorder stops. await new Promise(r => setTimeout(r, 300)); await off({ cmd: "stop" }); return poll(); } // Clear the previous error before a new attempt. lastErr = ""; try { // Tab the user is looking at when the button is pressed. const [t] = await chrome.tabs.query({ active: true, currentWindow: true }); // The popup itself is an extension page; it can't be recorded. if (t.url?.startsWith("chrome-extension:")) throw new Error("This is the extension's own page. Switch to the Teams tab, then click the icon."); // Stream id that the offscreen document later passes to getUserMedia. const id = await chrome.tabCapture.getMediaStreamId({ targetTabId: t.id }); // Create the offscreen document once; it stays alive while recording. if (!(await chrome.offscreen.hasDocument())) await chrome.offscreen.createDocument({ url: "offscreen.html", reasons: ["USER_MEDIA"], justification: "record tab" }); const r = await off({ cmd: "start", id, tabId: t.id }); if (r?.err) throw new Error(r.err); // Tell teams.js to start its scanners; ignored if the content script is not on this tab. chrome.tabs.sendMessage(t.id, { cmd: "cap-start" }).catch(() => {}); // Any failure is shown in the status line. } catch (e) { lastErr = String(e); } render(); poll(); }; // Keep status fresh while the popup is open. poll(); setInterval(poll, 1000);