'use strict'; // The only entry point is an explicit toolbar click. Shared scripts are local // package files; opening the panel never calls engine.start(). const PANEL_FILES = Object.freeze(['log_store.js', 'file_log.js', 'nicknames.js', 'engine.js', 'panel.js', 'runtime.js']); const pendingTabs = new Set(); function officialBroadcastUrl(value) { try { const url = new URL(value); return url.protocol === 'https:' && url.hostname === 'play.sooplive.com' && url.port === '' && url.username === '' && url.password === '' && /^\/[a-z0-9_]{2,32}(?:\/|$)/.test(url.pathname); } catch (_) { return false; } } // executeScript serializes these functions, so neither closes over the worker. function verifyCurrentDocument() { return {ok:globalThis.top === globalThis && location.protocol === 'https:' && location.hostname === 'play.sooplive.com' && location.port === '' && /^\/[a-z0-9_]{2,32}(?:\/|$)/.test(location.pathname)}; } function openCurrentPanel() { if (globalThis.top !== globalThis || location.protocol !== 'https:' || location.hostname !== 'play.sooplive.com' || location.port !== '' || !/^\/[a-z0-9_]{2,32}(?:\/|$)/.test(location.pathname)) { return {ok:false, reason:'page_changed'}; } try { if (!globalThis.SoopAutoManagerRuntime || typeof globalThis.SoopAutoManagerRuntime.attach !== 'function') { throw new Error('runtime_missing'); } const api = globalThis.SoopAutoManagerRuntime.attach(globalThis); if (!api || typeof api.getStatus !== 'function' || !api.getStatus()) { throw new Error('status_unavailable'); } return {ok:true}; } catch (_) { // One bounded message for this explicit click; no auth/raw error readback. globalThis.alert('자동 관리 패널을 열지 못했습니다. SOOP 방송 채팅이 연결된 뒤 다시 눌러 주세요.'); return {ok:false, reason:'attach_failed'}; } } async function showResult(tabId, ok, message) { const jobs = [ chrome.action.setBadgeText({tabId, text:ok ? '' : '!'}), chrome.action.setTitle({tabId, title:message}), ]; if (!ok) jobs.push(chrome.action.setBadgeBackgroundColor({tabId, color:'#A33745'})); // A tab may close between the click and the badge update. await Promise.allSettled(jobs); } async function handleClick(tab) { const tabId = tab && tab.id; if (!Number.isInteger(tabId) || tabId < 0 || pendingTabs.has(tabId)) return; if (!officialBroadcastUrl(tab.url)) { await showResult(tabId, false, 'SOOP 공식 방송 페이지에서 눌러 주세요: https://play.sooplive.com/'); return; } pendingTabs.add(tabId); try { const checked = await chrome.scripting.executeScript({ target:{tabId, frameIds:[0]}, world:'MAIN', func:verifyCurrentDocument, }); const main = checked && checked.find(item => item.frameId === 0); if (!main || main.result?.ok !== true || typeof main.documentId !== 'string' || !main.documentId) { await showResult(tabId, false, '현재 방송 페이지를 확인하지 못했습니다. 방송 탭에서 다시 눌러 주세요.'); return; } // Pin subsequent calls to this document. A navigation cannot move the // original click's authority to a replacement page in the same tab. const target = {tabId, documentIds:[main.documentId]}; await chrome.scripting.executeScript({target, world:'MAIN', files:[...PANEL_FILES]}); const results = await chrome.scripting.executeScript({target, world:'MAIN', func:openCurrentPanel}); const opened = results && results.find(item => item.frameId === 0 && item.documentId === main.documentId); if (opened?.result?.ok !== true) { await showResult(tabId, false, '자동 관리 패널을 열지 못했습니다. 방송 채팅 연결 후 다시 눌러 주세요.'); return; } await showResult(tabId, true, 'SOOP 자동 관리 패널 열림 · 실행 여부는 패널에서 확인하세요'); } catch (_) { await showResult(tabId, false, '페이지가 바뀌었거나 확장 실행이 제한되었습니다. 방송 탭에서 다시 눌러 주세요.'); } finally { pendingTabs.delete(tabId); } } chrome.action.onClicked.addListener(handleClick);