2026-06-09 16:12:31 +01:00
const stateEl = document . querySelector ( "#state" ) ;
const tabsEl = document . querySelector ( "#tabs" ) ;
const lastSyncEl = document . querySelector ( "#last-sync" ) ;
const endpointEl = document . querySelector ( "#endpoint" ) ;
2026-06-09 17:04:38 +01:00
const debugEl = document . querySelector ( "#debug" ) ;
2026-06-13 19:46:10 +01:00
const pageSnapshotEl = document . querySelector ( "#page-snapshot" ) ;
2026-06-09 16:12:31 +01:00
const errorEl = document . querySelector ( "#error" ) ;
const syncButton = document . querySelector ( "#sync" ) ;
2026-06-13 19:46:10 +01:00
const snapshotActiveTabButton = document . querySelector ( "#snapshot-active-tab" ) ;
2026-06-09 17:04:38 +01:00
const debugSweepButton = document . querySelector ( "#debug-sweep" ) ;
2026-06-09 16:12:31 +01:00
syncButton . addEventListener ( "click" , async ( ) => {
syncButton . disabled = true ;
await chrome . runtime . sendMessage ( { type : "loki.sync-now" } ) ;
await render ( ) ;
syncButton . disabled = false ;
} ) ;
2026-06-09 17:04:38 +01:00
debugSweepButton . addEventListener ( "click" , async ( ) => {
debugSweepButton . disabled = true ;
await chrome . runtime . sendMessage ( { type : "loki.debug-sweep" } ) ;
await render ( ) ;
debugSweepButton . disabled = false ;
} ) ;
2026-06-13 19:46:10 +01:00
snapshotActiveTabButton . addEventListener ( "click" , async ( ) => {
snapshotActiveTabButton . disabled = true ;
await chrome . runtime . sendMessage ( { type : "loki.snapshot-active-tab" } ) ;
await render ( ) ;
snapshotActiveTabButton . disabled = false ;
} ) ;
2026-06-09 16:12:31 +01:00
await render ( ) ;
async function render ( ) {
const result = await chrome . runtime . sendMessage ( { type : "loki.get-status" } ) ;
const status = result . status ;
endpointEl . textContent = result . endpoint ;
if ( ! status ) {
stateEl . textContent = "waiting" ;
stateEl . className = "state" ;
tabsEl . textContent = "-" ;
2026-06-09 17:04:38 +01:00
debugEl . textContent = "-" ;
2026-06-13 19:46:10 +01:00
pageSnapshotEl . textContent = "-" ;
2026-06-09 16:12:31 +01:00
lastSyncEl . textContent = "-" ;
errorEl . hidden = true ;
return ;
}
stateEl . textContent = status . ok ? "connected" : "offline" ;
stateEl . className = ` state ${ status . ok ? "ok" : "bad" } ` ;
tabsEl . textContent = String ( status . tabCount ? ? 0 ) ;
2026-06-09 17:04:38 +01:00
debugEl . textContent = formatDebugProbe ( status . debugProbe ) ;
2026-06-13 19:46:10 +01:00
pageSnapshotEl . textContent = formatPageSnapshot ( status . pageSnapshot ) ;
2026-06-09 16:12:31 +01:00
lastSyncEl . textContent = status . lastSyncAt ? new Date ( status . lastSyncAt ) . toLocaleTimeString ( ) : "-" ;
if ( status . error ) {
errorEl . textContent = status . error ;
errorEl . hidden = false ;
} else {
errorEl . hidden = true ;
}
}
2026-06-09 17:04:38 +01:00
function formatDebugProbe ( debugProbe ) {
if ( ! debugProbe ) return "not run" ;
const summary = debugProbe . summary ? ? { } ;
return ` ${ summary . attachedTabCount ? ? 0 } / ${ summary . attemptedTabCount ? ? 0 } attached ` ;
}
2026-06-13 19:46:10 +01:00
function formatPageSnapshot ( pageSnapshot ) {
if ( ! pageSnapshot ) return "not captured" ;
if ( pageSnapshot . error ) return pageSnapshot . error ;
const textLength = pageSnapshot . content ? . visibleTextLength ? ? 0 ;
const controlCount = pageSnapshot . forms ? . controlCount ? ? 0 ;
return ` ${ textLength } chars, ${ controlCount } controls ` ;
}