sh2/d2ypp2/services/neko/live-ops-extension/background.js
2026-06-03 10:53:02 -05:00

46 lines
1.4 KiB
JavaScript

const API_BASES = [
"http://host.docker.internal:8001",
"http://127.0.0.1:8001",
"http://localhost:8001",
"http://10.0.0.237:8001",
];
async function postTelemetry(payload) {
const body = JSON.stringify({
...payload,
source: payload.source || "chrome_extension",
received_at: Date.now() / 1000,
});
let lastError = "";
for (const base of API_BASES) {
try {
const res = await fetch(`${base}/hub/youtube/live/ingest`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body,
cache: "no-store",
});
if (res.ok) {
const data = await res.json().catch(() => ({}));
await chrome.storage.local.set({
liveHubLastOk: Date.now(),
liveHubLastBase: base,
liveHubLastEvent: data?.event?.id || null,
});
return { ok: true, base, eventId: data?.event?.id || null };
}
lastError = `${base} HTTP ${res.status}`;
} catch (err) {
lastError = `${base} ${err?.message || String(err)}`;
}
}
await chrome.storage.local.set({ liveHubLastError: lastError, liveHubLastErrorAt: Date.now() });
return { ok: false, error: lastError };
}
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (!message || message.type !== "live-hub-youtube-telemetry") return false;
postTelemetry(message.payload || {}).then(sendResponse);
return true;
});