/** * index.js — Popup logic for the Goon Plugin. * Runs inside the popup; scans the current tab for videos and opens the viewer. */ const STORAGE_KEY = 'goon_videos'; const findBtn = document.getElementById('find-btn'); const clearBtn = document.getElementById('clear-btn'); const openBtn = document.getElementById('open-btn'); const statusEl = document.getElementById('status'); const listEl = document.getElementById('video-list'); const btnRow = document.getElementById('btn-row'); listEl.style.wordBreak = 'break-word'; listEl.style.whiteSpace = 'pre-wrap'; function setStatus(msg) { statusEl.textContent = msg; console.log('[goon]', msg); } /** Load the current video list from storage. */ function loadVideos() { return chrome.storage.local.get(STORAGE_KEY).then(data => data[STORAGE_KEY] || []); } /** Render the video list UI from stored URLs. */ function renderList(urls) { listEl.innerHTML = ''; if (!urls || urls.length === 0) { listEl.style.display = 'none'; return; } listEl.style.display = 'block'; for (const url of urls) { const item = document.createElement('a'); item.className = 'video-item'; item.href = url; item.textContent = url; item.target = '_blank'; listEl.appendChild(item); } // Copy all URLs button const copyBtn = document.createElement('button'); copyBtn.className = 'copy-btn'; copyBtn.textContent = 'Copy All URLs'; copyBtn.addEventListener('click', () => { navigator.clipboard.writeText(urls.join('\n')).then(() => { copyBtn.textContent = 'Copied!'; setTimeout(() => { copyBtn.textContent = 'Copy All URLs'; }, 1500); }); }); listEl.appendChild(copyBtn); } /** Show the main button row. */ function showActions() { btnRow.style.display = 'flex'; loadVideos().then(urls => { if (urls.length > 0) { renderList(urls); setStatus(`Using ${urls.length} video(s) from last search.`); } else { setStatus(''); } }); } /* === "Find Videos" — scan the current tab === */ findBtn.addEventListener('click', async () => { setStatus('Scanning current tab…'); const tabs = await chrome.tabs.query({ active: true, currentWindow: true }); const tab = tabs[0]; if (!tab || !tab.url) { setStatus('Cannot access the current tab.'); return; } // Skip chrome-internal pages if (tab.url.startsWith('chrome://') || tab.url.startsWith('chrome-extension://') || tab.url.startsWith('edge://') || tab.url.startsWith('about:') || tab.url.startsWith('view-source:')) { setStatus('This page cannot be scanned.'); return; } try { // Check the main frame first, then iframes const results = await chrome.scripting.executeScript({ target: { tabId: tab.id, allFrames: true }, func: () => { // Count both