const APT_URL = 'http://127.0.0.1:18247/cookie'; const APT_TOKEN = 'apt-local-bridge-token'; const tabInfo = document.getElementById('tabInfo'); const sendBtn = document.getElementById('sendBtn'); const copyBtn = document.getElementById('copyBtn'); const statusMsg = document.getElementById('statusMsg'); let detectedOrigin = null; function showStatus(message, type) { statusMsg.textContent = message; statusMsg.className = 'status-msg ' + type; } function setActionButtonsDisabled(disabled) { sendBtn.disabled = disabled; copyBtn.disabled = disabled; } async function getVaCookieValue() { if (!detectedOrigin) { throw new Error('No Alta deployment detected.'); } const cookie = await chrome.cookies.get({ url: detectedOrigin, name: 'va' }); if (!cookie || !cookie.value) { throw new Error('No "va" session cookie found. Are you logged in?'); } if (cookie.expirationDate && cookie.expirationDate < Date.now() / 1000) { throw new Error('Session cookie has expired. Please log in again.'); } return cookie.value; } // Check the active tab on popup open chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { if (!tabs || tabs.length === 0) { tabInfo.textContent = 'No active tab found.'; tabInfo.className = 'tab-info not-detected'; return; } const tab = tabs[0]; let url; try { url = new URL(tab.url); } catch { tabInfo.textContent = 'Cannot read this tab URL.'; tabInfo.className = 'tab-info not-detected'; return; } const hostname = url.hostname; const isAlta = hostname.endsWith('.avasecurity.com') || hostname.endsWith('.avigilon.com'); if (!isAlta) { tabInfo.textContent = 'This tab is not an Alta deployment.'; tabInfo.className = 'tab-info not-detected'; return; } detectedOrigin = url.origin; tabInfo.textContent = 'Detected: ' + hostname; tabInfo.className = 'tab-info detected'; setActionButtonsDisabled(false); }); // Send cookie on button click sendBtn.addEventListener('click', async () => { if (!detectedOrigin) return; setActionButtonsDisabled(true); showStatus('Retrieving VA token...', 'info'); try { const cookieValue = await getVaCookieValue(); showStatus('Sending to Alta Proxy Tool...', 'info'); const response = await fetch(APT_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-APT-Token': APT_TOKEN }, body: JSON.stringify({ deploymentUrl: detectedOrigin, cookieValue }) }); const data = await response.json(); if (data.success) { showStatus('VA token sent successfully!', 'success'); } else { showStatus('Error: ' + (data.message || 'Unknown error'), 'error'); } } catch (err) { if (err.message && err.message.includes('Failed to fetch')) { showStatus('Alta Proxy Tool is not running.', 'error'); } else { showStatus('Error: ' + err.message, 'error'); } } finally { setActionButtonsDisabled(false); } }); // Copy VA token on button click copyBtn.addEventListener('click', async () => { if (!detectedOrigin) return; setActionButtonsDisabled(true); showStatus('Retrieving VA token...', 'info'); try { const cookieValue = await getVaCookieValue(); await navigator.clipboard.writeText(cookieValue); showStatus('VA token copied to clipboard!', 'success'); } catch (err) { showStatus('Error: ' + err.message, 'error'); } finally { setActionButtonsDisabled(false); } });