Apply ruff lint and rename cryptic identifiers to readable names

This commit is contained in:
Stefano Zini
2026-11-05 09:09:20 +01:00
parent 19de4654ff
commit ead33657c7
24 changed files with 4459 additions and 1856 deletions
+32 -32
View File
@@ -28,26 +28,26 @@ let buf = [], timers = [], seenCap = new WeakMap(), capAuthor = "", seenChat = n
const norm = raw => {
raw = (raw || "").trim();
// Matches "Last, First" with an optional "(...)" suffix; anything else is returned trimmed.
const m = raw.match(/^([^,]+),\s*([^(]+?)\s*(\(.*)?$/);
if (!m) return raw;
const match = raw.match(/^([^,]+),\s*([^(]+?)\s*(\(.*)?$/);
if (!match) return raw;
// Reorder to "First Last" and title-case words that are fully uppercase.
return `${m[2]} ${m[1]}`.split(/\s+/).map(w => (w.length > 1 && w === w.toUpperCase() ? w[0] + w.slice(1).toLowerCase() : w)).join(" ");
return `${match[2]} ${match[1]}`.split(/\s+/).map(word => (word.length > 1 && word === word.toUpperCase() ? word[0] + word.slice(1).toLowerCase() : word)).join(" ");
};
// Walk up from an element to the participant tile (the ancestor containing a name tag).
const tileOf = el => { while (el && !el.querySelector(SEL.nametag)) el = el.parentElement; return el; };
const tileOf = element => { while (element && !element.querySelector(SEL.nametag)) element = element.parentElement; return element; };
// Stable participant key: the nearest ancestor data-tid containing "@" (an id), else the name text.
const keyOf = tile => {
for (let e = tile; e; e = e.parentElement) if ((e.dataset?.tid || "").includes("@")) return e.dataset.tid;
for (let node = tile; node; node = node.parentElement) if ((node.dataset?.tid || "").includes("@")) return node.dataset.tid;
return tile.querySelector(SEL.nametag)?.textContent.trim() || "";
};
// Display name from a tile's name tag.
const nameOf = tile => norm(tile.querySelector(SEL.nametag)?.textContent);
// Name of the local user, read from the self-video tile.
const selfName = () => { const s = document.querySelector(SEL.self); return s ? nameOf(s) : ""; };
const selfName = () => { const selfTile = document.querySelector(SEL.self); return selfTile ? nameOf(selfTile) : ""; };
// Queue an event with a wall-clock timestamp; sent later by send().
const emit = (kind, who_key = "", who = "", text = "") => buf.push({ ts: Date.now(), kind, who_key, who, text });
// Wrap timer callbacks so one exception does not kill the interval.
const guard = f => () => { try { f(); } catch (e) { console.error("[teams-whisper]", e); } };
const guard = callback => () => { try { callback(); } catch (error) { console.error("[teams-whisper]", error); } };
// Send queued events to the offscreen document; errors ignored (the recorder may be closed).
const send = () => { if (buf.length) chrome.runtime.sendMessage({ to: "off", cmd: "events", events: buf.splice(0) }).catch(() => { }); };
// All participant tiles currently in the DOM.
@@ -68,18 +68,18 @@ function present() {
// Dedupe by key (a participant can have more than one tile).
const seen = new Map();
// The self tile's name comes from the self-video element.
for (const t of tiles()) seen.set(keyOf(t), { key: keyOf(t), who: t.closest(SEL.self) ? selfName() : nameOf(t) });
for (const tile of tiles()) seen.set(keyOf(tile), { key: keyOf(tile), who: tile.closest(SEL.self) ? selfName() : nameOf(tile) });
// Roster snapshot payload.
emit("present", "", "", JSON.stringify([...seen.values()]));
}
// Emit one "active" event for each participant whose speaking outline is on.
function active() {
// Speaking outlines in the DOM.
for (const el of document.querySelectorAll(SEL.speaking)) {
const t = tileOf(el); if (!t) continue;
for (const element of document.querySelectorAll(SEL.speaking)) {
const tile = tileOf(element); if (!tile) continue;
// The local user speaking is recorded under the key "self".
if (t.closest(SEL.self) || t.matches(SEL.self)) emit("active", "self", selfName());
else emit("active", keyOf(t), nameOf(t));
if (tile.closest(SEL.self) || tile.matches(SEL.self)) emit("active", "self", selfName());
else emit("active", keyOf(tile), nameOf(tile));
}
}
// Screen share: detect the sharer from the banner aria-label and emit start/stop on change.
@@ -88,10 +88,10 @@ function share() {
const names = tiles().map(nameOf);
let cur = "";
// Scan all aria-labels for the share banner.
for (const el of document.querySelectorAll("[aria-label]")) {
const m = el.getAttribute("aria-label").match(SEL.shareAria);
for (const element of document.querySelectorAll("[aria-label]")) {
const match = element.getAttribute("aria-label").match(SEL.shareAria);
// Prefer a roster name the banner text starts with (labels can be truncated); else use the raw text.
if (m) { cur = names.find(n => n && m[2].startsWith(n)) || m[2]; break; }
if (match) { cur = names.find(name => name && match[2].startsWith(name)) || match[2]; break; }
}
// No change since last check.
if (cur === lastShare) return;
@@ -102,33 +102,33 @@ function share() {
}
// Captions, chat and raised hands, each emitted once.
function messages() {
if (SEL.caption) for (const el of document.querySelectorAll(SEL.caption)) {
if (SEL.caption) for (const element of document.querySelectorAll(SEL.caption)) {
// Author row sets the speaker for the text rows that follow it.
if (el.getAttribute("data-tid") === "author") { capAuthor = norm(el.textContent); continue; }
if (element.getAttribute("data-tid") === "author") { capAuthor = norm(element.textContent); continue; }
// caption text grows while spoken: emit once it was unchanged for a full tick
// Caption text of this row.
const text = el.textContent.trim(), s = seenCap.get(el) || {};
const text = element.textContent.trim(), seenState = seenCap.get(element) || {};
// Already emitted this row.
if (s.done) continue;
if (seenState.done) continue;
// Unchanged since last tick: treat as final and emit once.
if (text && s.text === text) { s.done = true; emit("caption", "", capAuthor, text); }
s.text = text; seenCap.set(el, s);
if (text && seenState.text === text) { seenState.done = true; emit("caption", "", capAuthor, text); }
seenState.text = text; seenCap.set(element, seenState);
}
// Chat messages (inactive while SEL.chatMsg is null); each element is counted once.
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.chatMsg) for (const element of document.querySelectorAll(SEL.chatMsg)) {
if (seenChat.has(element)) continue; seenChat.add(element);
const text = element.querySelector(SEL.chatText)?.textContent.trim();
if (text) emit("chat", "", norm(element.querySelector(SEL.chatAuthor)?.textContent), text);
}
// Hand raises (inactive while SEL.hand is null); emit only new raises.
if (SEL.hand) {
// Hands raised this tick.
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);
for (const element of document.querySelectorAll(SEL.hand)) {
const tile = tileOf(element); if (!tile) continue;
const participantKey = keyOf(tile); now.add(participantKey);
// New raise since the last tick.
if (!raised.has(k)) emit("hand", k, nameOf(t));
if (!raised.has(participantKey)) emit("hand", participantKey, nameOf(tile));
}
// Keep only hands still up.
raised = now;
@@ -160,7 +160,7 @@ function start() {
}
// Start/stop commands sent by popup.js directly to this tab.
chrome.runtime.onMessage.addListener(m => {
if (m.cmd === "cap-start") start();
if (m.cmd === "cap-stop") stop();
chrome.runtime.onMessage.addListener(message => {
if (message.cmd === "cap-start") start();
if (message.cmd === "cap-stop") stop();
});