A Capacitor shell that opens a Sonda server in a WebView. It asks for the address of the server the first time and remembers it, trusts the certificate authorities installed on the phone, and keeps a notification with the controls so that the music goes on with the screen off. Fixes #47
94 lines
3.9 KiB
HTML
94 lines
3.9 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
|
|
<meta name="color-scheme" content="dark" />
|
|
<title>Sonda</title>
|
|
<style>
|
|
:root { --bg: #08161d; --fg: #e8f1f3; --muted: #8fa7ae; --teal: #38e1c6; --line: #1d3640; }
|
|
* { box-sizing: border-box; }
|
|
body { margin: 0; min-height: 100dvh; display: grid; place-items: center; padding: 24px; background: var(--bg); color: var(--fg); font: 16px/1.5 system-ui, sans-serif; }
|
|
main { width: 100%; max-width: 24rem; display: grid; gap: 16px; }
|
|
h1 { margin: 0; font-size: 2rem; letter-spacing: -0.02em; }
|
|
p { margin: 0; color: var(--muted); }
|
|
label { font-weight: 600; }
|
|
input { width: 100%; min-height: 48px; padding: 0 12px; border: 1px solid var(--line); border-radius: 8px; background: transparent; color: var(--fg); font: inherit; }
|
|
input:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }
|
|
button { min-height: 48px; border: 0; border-radius: 6px; background: var(--teal); color: #052b26; font: inherit; font-weight: 700; }
|
|
button:disabled { opacity: 0.6; }
|
|
#message { min-height: 1.5em; color: #ffb4a8; }
|
|
[hidden] { display: none; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<h1>Sonda</h1>
|
|
<p id="lead">Opening your server…</p>
|
|
<form id="form" hidden>
|
|
<p>Where does your Sonda server live?</p>
|
|
<div style="display: grid; gap: 8px; margin-top: 16px">
|
|
<label for="address">Server address</label>
|
|
<input id="address" inputmode="url" autocapitalize="none" autocomplete="url" spellcheck="false" placeholder="music.example.org" />
|
|
<p id="message" role="alert"></p>
|
|
<button id="go" type="submit">Open</button>
|
|
</div>
|
|
</form>
|
|
</main>
|
|
<script type="module">
|
|
import { normalizeAddress } from './server.js'
|
|
|
|
const KEY = 'sonda.server'
|
|
const lead = document.getElementById('lead')
|
|
const form = document.getElementById('form')
|
|
const address = document.getElementById('address')
|
|
const message = document.getElementById('message')
|
|
const go = document.getElementById('go')
|
|
|
|
const read = () => { try { return localStorage.getItem(KEY) } catch { return null } }
|
|
const write = (v) => { try { localStorage.setItem(KEY, v) } catch { /* the address is asked again next time */ } }
|
|
|
|
// Reachable means a Sonda server answers; the answer itself is opaque from here, as the page is on another origin.
|
|
async function reachable(origin) {
|
|
try {
|
|
await fetch(`${origin}/api/v1/health`, { mode: 'no-cors', cache: 'no-store', signal: AbortSignal.timeout(8000) })
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
function ask(prefill, problem) {
|
|
lead.hidden = true
|
|
form.hidden = false
|
|
address.value = prefill ?? ''
|
|
message.textContent = problem ?? ''
|
|
address.focus()
|
|
}
|
|
|
|
form.addEventListener('submit', async (e) => {
|
|
e.preventDefault()
|
|
const origin = normalizeAddress(address.value)
|
|
if (!origin) return void (message.textContent = 'That is not an address. Try music.example.org')
|
|
go.disabled = true
|
|
message.textContent = 'Looking for the server…'
|
|
if (await reachable(origin)) {
|
|
write(origin)
|
|
location.replace(origin)
|
|
return
|
|
}
|
|
go.disabled = false
|
|
message.textContent = 'No answer. Check the address, that the phone is on the right network, and that it trusts the certificate of the server.'
|
|
})
|
|
|
|
const saved = read()
|
|
if (new URLSearchParams(location.search).has('change') || !saved) {
|
|
ask(saved)
|
|
} else if (await reachable(saved)) {
|
|
location.replace(saved)
|
|
} else {
|
|
ask(saved, `No answer from ${saved}. Check the address or the network.`)
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|