ext: /health check blocks recording when the backend is offline, strobing offline alarm in popup
This commit is contained in:
+17
-6
@@ -6,16 +6,24 @@ const sendToOffscreen = message => chrome.runtime.sendMessage({ to: "off", ...me
|
||||
// Format milliseconds as m:ss.
|
||||
const formatElapsed = milliseconds => { const totalSeconds = Math.floor(milliseconds / 1000); return `${Math.floor(totalSeconds / 60)}:${String(totalSeconds % 60).padStart(2, "0")}`; };
|
||||
// Last error shown when there is no offscreen document to report it.
|
||||
let recorderState = { state: "idle" }, lastError = "";
|
||||
let recorderState = { state: "idle" }, lastError = "", serverOk = null;
|
||||
// Backend liveness (server.py /health); false on any error or after 1.5 s.
|
||||
const serverUp = () => fetch("http://127.0.0.1:18765/health", { signal: AbortSignal.timeout(1500) }).then(response => response.ok, () => false);
|
||||
|
||||
// Refresh the button and status line from state recorderState.
|
||||
function render() {
|
||||
const goButton = byId("go"), statusLine = byId("st");
|
||||
document.body.dataset.state = recorderState.state;
|
||||
// Backend down and not mid-recording: starting is impossible, the offline banner replaces the status line.
|
||||
const offline = serverOk === false && recorderState.state !== "recording";
|
||||
document.body.dataset.state = offline ? "offline" : recorderState.state;
|
||||
byId("off").hidden = !offline;
|
||||
// Pipeline busy after stop: no new start until it finishes.
|
||||
goButton.disabled = recorderState.state === "transcribing";
|
||||
goButton.disabled = offline || recorderState.state === "transcribing";
|
||||
// Backend indicator under the button.
|
||||
byId("srv").dataset.ok = serverOk ?? "";
|
||||
byId("srv").textContent = serverOk == null ? "server: checking..." : serverOk ? "server: online" : "server: OFFLINE";
|
||||
// Button doubles as Stop while recording.
|
||||
goButton.textContent = recorderState.state === "recording" ? "Stop + transcribe" : "Start";
|
||||
goButton.textContent = offline ? "Recording blocked: server offline" : recorderState.state === "recording" ? "Stop + transcribe" : "Start";
|
||||
// Status line text per recorder state.
|
||||
statusLine.textContent = {
|
||||
idle: "idle - open a Teams tab and press Start",
|
||||
@@ -30,8 +38,9 @@ function render() {
|
||||
|
||||
// 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()) recorderState = (await sendToOffscreen({ cmd: "status" })) || recorderState; else recorderState = lastError ? { state: "error", text: lastError } : { state: "idle" };
|
||||
// A failed Start wins; else ask the offscreen doc (if any) for its state, or show idle.
|
||||
recorderState = lastError ? { state: "error", text: lastError } : (await chrome.offscreen.hasDocument()) ? (await sendToOffscreen({ cmd: "status" })) || recorderState : { state: "idle" };
|
||||
serverOk = await serverUp();
|
||||
render();
|
||||
}
|
||||
|
||||
@@ -54,6 +63,8 @@ byId("go").onclick = async () => {
|
||||
if (activeTab.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 streamId = await chrome.tabCapture.getMediaStreamId({ targetTabId: activeTab.id });
|
||||
// Health check before recording: backend down = nothing would be saved, so show the offline banner and stop here.
|
||||
if (!(serverOk = await serverUp())) return render();
|
||||
// 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" });
|
||||
|
||||
Reference in New Issue
Block a user