const LIVE_HUB_MIN_INTERVAL_MS = 1800; let lastSentAt = 0; let lastSignature = ""; function cleanText(value, max = 260) { return String(value || "") .replace(/\s+/g, " ") .trim() .slice(0, max); } function pageText() { return cleanText(document.body?.innerText || "", 6000); } function parseCompactNumber(raw) { const text = cleanText(raw, 40).replace(/,/g, ""); const match = text.match(/(\d+(?:\.\d+)?)([kKmMwW万]*)/); if (!match) return null; let value = Number(match[1]); const unit = match[2].toLowerCase(); if (unit === "k") value *= 1000; if (unit === "m") value *= 1000000; if (unit === "w" || unit === "万") value *= 10000; return Number.isFinite(value) ? Math.round(value) : null; } function firstNumberByPatterns(text, patterns) { for (const pattern of patterns) { const match = text.match(pattern); if (match) { const parsed = parseCompactNumber(match[1]); if (parsed != null) return parsed; } } return null; } function detectLiveStatus(text) { const lower = text.toLowerCase(); if (/stream ended|not live|offline|直播已结束|未在直播/.test(lower)) return "ended"; if (/you'?re live|streaming|excellent connection|stream is healthy|直播中|正在直播|已开始直播/.test(lower)) return "live"; if (/waiting|preview|scheduled|等待|预定|未开始/.test(lower)) return "waiting"; if (/error|failed|copyright|policy|异常|错误|受限|违规/.test(lower)) return "attention"; return ""; } function detectHealth(text) { const lower = text.toLowerCase(); if (/excellent connection|stream is healthy|health excellent|连接状况极佳|状况良好/.test(lower)) return "excellent"; if (/good connection|healthy|良好/.test(lower)) return "good"; if (/unstable|poor|buffer|warning|警告|不稳定|不佳|卡顿/.test(lower)) return "warning"; if (/error|failed|disconnect|异常|错误|断开/.test(lower)) return "error"; return ""; } function collectPopupTexts() { const selectors = [ "[role='dialog']", "[aria-live='assertive']", "[aria-live='polite']", "ytcp-toast", "tp-yt-paper-toast", "paper-dialog", "ytcp-alert", "ytcp-ve", ]; const out = []; for (const node of document.querySelectorAll(selectors.join(","))) { const rect = node.getBoundingClientRect?.(); if (rect && rect.width === 0 && rect.height === 0) continue; const text = cleanText(node.innerText || node.textContent || "", 500); if (text && !out.includes(text)) out.push(text); if (out.length >= 8) break; } return out; } function collectStreamTitle() { const selectors = [ "input[aria-label*='Title']", "textarea[aria-label*='Title']", "ytcp-social-suggestions-textbox", "ytcp-video-title", "h1", ]; for (const selector of selectors) { const node = document.querySelector(selector); const value = node?.value || node?.innerText || node?.textContent; const text = cleanText(value, 180); if (text) return text; } return cleanText(document.title, 180); } function collectTelemetry() { const text = pageText(); const viewerCount = firstNumberByPatterns(text, [ /([\d,.]+[kKmMwW万]?)\s*(?:watching|viewers|concurrent viewers)/i, /([\d,.]+[kKmMwW万]?)\s*(?:人正在观看|正在观看|观看|在线)/, ]); const likeCount = firstNumberByPatterns(text, [ /([\d,.]+[kKmMwW万]?)\s*(?:likes|like count)/i, /([\d,.]+[kKmMwW万]?)\s*(?:点赞|赞)/, ]); const popups = collectPopupTexts(); const payload = { captured_at: Date.now() / 1000, source: "chrome_extension", url: location.href, page_title: document.title, stream_title: collectStreamTitle(), live_status: detectLiveStatus(text), health: detectHealth(text), viewer_count: viewerCount, like_count: likeCount, popups, text: text.slice(0, 2200), signals: { has_dialog: popups.length > 0, visibility: document.visibilityState, focused: document.hasFocus(), path: location.pathname, }, }; return payload; } function signatureOf(payload) { return JSON.stringify([ payload.url, payload.live_status, payload.health, payload.viewer_count, payload.like_count, payload.popups, payload.stream_title, ]); } function sendTelemetry(force = false) { const now = Date.now(); if (!force && now - lastSentAt < LIVE_HUB_MIN_INTERVAL_MS) return; const payload = collectTelemetry(); const sig = signatureOf(payload); if (!force && sig === lastSignature && now - lastSentAt < 10000) return; lastSentAt = now; lastSignature = sig; chrome.runtime.sendMessage({ type: "live-hub-youtube-telemetry", payload }, () => { void chrome.runtime.lastError; }); } const observer = new MutationObserver(() => sendTelemetry(false)); observer.observe(document.documentElement, { subtree: true, childList: true, characterData: true }); window.addEventListener("focus", () => sendTelemetry(true)); document.addEventListener("visibilitychange", () => sendTelemetry(true)); setInterval(() => sendTelemetry(false), 3000); sendTelemetry(true);