62 lines
3.5 KiB
JavaScript
62 lines
3.5 KiB
JavaScript
// Make Teams see Edge instead of Opera: rewrite UA + client-hint request headers.
|
|
// Edge UA built from the browser's own UA: the Opera OPR/ token is removed and an Edg/ token is added.
|
|
const userAgent = navigator.userAgent;
|
|
// Chrome major version taken from the UA; falls back to 130 if the UA has no Chrome token.
|
|
const chromeMajor = (userAgent.match(/Chrome\/(\d+)/) || [])[1] || "130";
|
|
const edgeUA = userAgent.replace(/ OPR\/[\d.]+/, "") + ` Edg/${chromeMajor}.0.0.0`;
|
|
// Sec-CH-UA brand list matching the Edge UA; "Not_A Brand" is a GREASE placeholder brand.
|
|
const brands = `"Microsoft Edge";v="${chromeMajor}", "Chromium";v="${chromeMajor}", "Not_A Brand";v="24"`;
|
|
// Helper: builds one declarativeNetRequest header operation.
|
|
const headerSet = (header, value) => ({ header, operation: "set", value });
|
|
|
|
// Dynamic rules persist across service-worker restarts, so id 1 is replaced on every start.
|
|
chrome.declarativeNetRequest.updateDynamicRules({
|
|
removeRuleIds: [1],
|
|
addRules: [{
|
|
id: 1,
|
|
// Higher than default so this rule wins over other extensions' header rules.
|
|
priority: 1,
|
|
// Rewrite the outgoing request headers.
|
|
action: {
|
|
type: "modifyHeaders",
|
|
requestHeaders: [headerSet("User-Agent", edgeUA), headerSet("Sec-CH-UA", brands)],
|
|
},
|
|
condition: {
|
|
// Only Teams hosts: teams.microsoft.com and teams.cloud.microsoft.
|
|
regexFilter: "^https://teams\\.(cloud\\.)?microsoft(\\.com)?/",
|
|
// Every request type Teams uses, including websocket handshakes.
|
|
resourceTypes: ["main_frame", "sub_frame", "xmlhttprequest", "script", "stylesheet", "image", "font", "websocket", "other"],
|
|
},
|
|
}],
|
|
});
|
|
|
|
// Toolbar icon per recorder state, drawn at runtime (no PNG assets). offscreen.js pushes {to:"bg", state} every second.
|
|
const ICON = {
|
|
idle: { bg: "#64748b", badge: "" },
|
|
recording: { bg: "#dc2626", badge: "REC" },
|
|
transcribing: { bg: "#2563eb", badge: "..." },
|
|
done: { bg: "#16a34a", badge: "OK" },
|
|
error: { bg: "#d97706", badge: "!" },
|
|
};
|
|
function drawIcon(state) {
|
|
const { bg, badge } = ICON[state] || ICON.idle;
|
|
const images = {};
|
|
for (const size of [16, 32, 48]) {
|
|
const canvas = new OffscreenCanvas(size, size), ctx = canvas.getContext("2d"), c = size / 2;
|
|
ctx.fillStyle = bg; ctx.beginPath(); ctx.arc(c, c, c, 0, 2 * Math.PI); ctx.fill();
|
|
ctx.fillStyle = ctx.strokeStyle = "#fff"; ctx.lineWidth = size / 8; ctx.lineCap = "round";
|
|
if (state === "recording") { ctx.beginPath(); ctx.arc(c, c, size * 0.22, 0, 2 * Math.PI); ctx.fill(); }
|
|
else if (state === "error") { ctx.fillRect(c - size * 0.07, size * 0.2, size * 0.14, size * 0.38); ctx.fillRect(c - size * 0.07, size * 0.68, size * 0.14, size * 0.14); }
|
|
else if (state === "done") { ctx.beginPath(); ctx.moveTo(size * 0.27, c); ctx.lineTo(size * 0.44, size * 0.68); ctx.lineTo(size * 0.74, size * 0.32); ctx.stroke(); }
|
|
else if (state === "transcribing") { for (const dx of [-0.24, 0, 0.24]) { ctx.beginPath(); ctx.arc(c + dx * size, c, size * 0.07, 0, 2 * Math.PI); ctx.fill(); } }
|
|
else { ctx.beginPath(); ctx.moveTo(size * 0.4, size * 0.28); ctx.lineTo(size * 0.72, c); ctx.lineTo(size * 0.4, size * 0.72); ctx.closePath(); ctx.fill(); }
|
|
images[size] = ctx.getImageData(0, 0, size, size);
|
|
}
|
|
chrome.action.setIcon({ imageData: images });
|
|
chrome.action.setBadgeText({ text: badge });
|
|
chrome.action.setBadgeBackgroundColor({ color: bg });
|
|
chrome.action.setTitle({ title: `Teams Whisper - ${state}` });
|
|
}
|
|
drawIcon("idle");
|
|
chrome.runtime.onMessage.addListener(message => { if (message.to === "bg") drawIcon(message.state); });
|