2026-01-15 04:50:11 +00:00
|
|
|
|
const DEFAULT_PORT = 18792
|
|
|
|
|
|
|
|
|
|
|
|
function clampPort(value) {
|
|
|
|
|
|
const n = Number.parseInt(String(value || ''), 10)
|
|
|
|
|
|
if (!Number.isFinite(n)) return DEFAULT_PORT
|
|
|
|
|
|
if (n <= 0 || n > 65535) return DEFAULT_PORT
|
|
|
|
|
|
return n
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-15 05:10:57 +00:00
|
|
|
|
function updateRelayUrl(port) {
|
|
|
|
|
|
const el = document.getElementById('relay-url')
|
|
|
|
|
|
if (!el) return
|
|
|
|
|
|
el.textContent = `http://127.0.0.1:${port}/`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-15 05:15:33 +00:00
|
|
|
|
function setStatus(kind, message) {
|
|
|
|
|
|
const status = document.getElementById('status')
|
|
|
|
|
|
if (!status) return
|
|
|
|
|
|
status.dataset.kind = kind || ''
|
|
|
|
|
|
status.textContent = message || ''
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function checkRelayReachable(port) {
|
|
|
|
|
|
const url = `http://127.0.0.1:${port}/`
|
|
|
|
|
|
const ctrl = new AbortController()
|
|
|
|
|
|
const t = setTimeout(() => ctrl.abort(), 900)
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await fetch(url, { method: 'HEAD', signal: ctrl.signal })
|
|
|
|
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
|
|
|
|
|
setStatus('ok', `Relay reachable at ${url}`)
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
setStatus(
|
|
|
|
|
|
'error',
|
|
|
|
|
|
`Relay not reachable at ${url}. Start Clawdbot’s browser relay on this machine, then click the toolbar button again.`,
|
|
|
|
|
|
)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
clearTimeout(t)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-15 04:50:11 +00:00
|
|
|
|
async function load() {
|
|
|
|
|
|
const stored = await chrome.storage.local.get(['relayPort'])
|
|
|
|
|
|
const port = clampPort(stored.relayPort)
|
|
|
|
|
|
document.getElementById('port').value = String(port)
|
2026-01-15 05:10:57 +00:00
|
|
|
|
updateRelayUrl(port)
|
2026-01-15 05:15:33 +00:00
|
|
|
|
await checkRelayReachable(port)
|
2026-01-15 04:50:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function save() {
|
|
|
|
|
|
const input = document.getElementById('port')
|
|
|
|
|
|
const port = clampPort(input.value)
|
|
|
|
|
|
await chrome.storage.local.set({ relayPort: port })
|
|
|
|
|
|
input.value = String(port)
|
2026-01-15 05:10:57 +00:00
|
|
|
|
updateRelayUrl(port)
|
2026-01-15 05:15:33 +00:00
|
|
|
|
await checkRelayReachable(port)
|
2026-01-15 04:50:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
document.getElementById('save').addEventListener('click', () => void save())
|
|
|
|
|
|
void load()
|