import { getVersion } from "../lib/aria2.js"; const els = { endpoint: document.getElementById('rpc-endpoint'), secret: document.getElementById('rpc-secret'), btnSave: document.getElementById('btn-save'), btnReset: document.getElementById('btn-reset'), btnTest: document.getElementById('btn-test'), status: document.getElementById('status'), }; async function restore() { const { aria2 } = await browser.storage.local.get('aria2'); els.endpoint.value = aria2?.endpoint || 'http://localhost:6800/jsonrpc'; els.secret.value = aria2?.secret || ''; } async function save() { const endpoint = els.endpoint.value.trim(); const secret = els.secret.value.trim(); await browser.storage.local.set({ aria2: { endpoint, secret } }); els.status.textContent = 'Saved.'; } async function reset() { await browser.storage.local.remove('aria2'); await restore(); els.status.textContent = 'Reset to defaults.'; } async function testConnection() { const endpoint = els.endpoint.value.trim(); const secret = els.secret.value.trim(); // Heads-up for Firefox HTTPS-Only Mode when using non-local HTTP try { const u = new URL(endpoint); const isHttp = u.protocol === 'http:'; const isLoopback = ['localhost', '127.0.0.1', '[::1]'].includes(u.hostname); if (isHttp && !isLoopback) { els.status.textContent = 'Testing… (note: Firefox HTTPS-Only may upgrade this)'; } else { els.status.textContent = 'Testing…'; } } catch { els.status.textContent = 'Testing…'; } try { const res = await getVersion({ endpoint, secret }); const version = res?.result?.version || JSON.stringify(res?.result || res); els.status.textContent = `OK: aria2 version ${version}`; } catch (e) { // Provide a more actionable hint when HTTPS-Only likely interferes let hint = ''; try { const u = new URL(endpoint); const isHttp = u.protocol === 'http:'; const isLoopback = ['localhost', '127.0.0.1', '[::1]'].includes(u.hostname); if (isHttp && !isLoopback) { hint = ' — if Firefox HTTPS-Only Mode is on, use https, add a site exception, or disable HTTPS-Only temporarily.'; } } catch {} els.status.textContent = `Error: ${e?.message || e}${hint}`; } } function wire() { els.btnSave.addEventListener('click', save); els.btnReset.addEventListener('click', reset); els.btnTest.addEventListener('click', testConnection); } document.addEventListener('DOMContentLoaded', async () => { wire(); await restore(); });