diff --git a/app/static/css/app.css b/app/static/css/app.css index b935eac..770ded8 100644 --- a/app/static/css/app.css +++ b/app/static/css/app.css @@ -37,6 +37,7 @@ .form-grid .wide { grid-column: 1 / -1; } .cmd { display: flex; gap: var(--space-2); align-items: stretch; } .cmd pre { flex: 1; min-width: 0; margin: 0; padding: var(--space-2) var(--space-3); border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--code-bg); font: var(--text-sm)/1.5 var(--font-mono); white-space: pre-wrap; word-break: break-all; } +.select-all { user-select: all; -webkit-user-select: all; cursor: text; } .more { display: flex; justify-content: center; padding: var(--space-3); } .toolbar .input { width: 220px; height: var(--control-h); padding: 0 10px; } .table a { color: inherit; text-decoration: none; } diff --git a/app/static/js/ui.js b/app/static/js/ui.js index b70b5af..cf5128a 100644 --- a/app/static/js/ui.js +++ b/app/static/js/ui.js @@ -23,12 +23,15 @@ export function el(spec, props = {}, ...children) { return node; } -/** A short confirmation in the corner: toast('Copied'), toast('Upload failed: too large', 'error'). */ +/** A short confirmation in the corner: toast('Copied'), toast('Upload failed: too large', 'error'). + * While a modal dialog is open the toast goes inside it: everything outside a modal sits under + * its backdrop, which would dim the toast. */ export function toast(message, kind = 'info') { - let box = document.getElementById('toasts'); + const host = document.querySelector('dialog[open]') || document.body; + let box = [...host.children].find((c) => c.classList.contains('toasts')); if (!box) { - box = el('div.toasts', { id: 'toasts', 'aria-live': 'polite' }); - document.body.append(box); + box = el('div.toasts', { 'aria-live': 'polite' }); + host.append(box); } const t = el(`div.toast.${kind}`, { role: kind === 'error' ? 'alert' : 'status' }, message); box.append(t); @@ -36,17 +39,29 @@ export function toast(message, kind = 'info') { setTimeout(() => t.remove(), kind === 'error' ? 5400 : 2600); } +/** Copies text and toasts `what`; resolves false (and says so) when the browser refuses. + * navigator.clipboard only exists in secure contexts (https, localhost). On plain http, e.g. + * http://192.168.1.10 or http://host.local, it falls back to a hidden textarea + execCommand. */ export async function copyText(text, what = 'Copied') { + let ok = false; try { await navigator.clipboard.writeText(text); + ok = true; } catch { - const ta = el('textarea', { value: text, style: 'position:fixed;opacity:0' }); - document.body.append(ta); + // Inside an open modal, anything outside the dialog is inert and can't be selected. + const host = document.querySelector('dialog[open]') || document.body; + const before = document.activeElement; + const ta = el('textarea', { readonly: true, 'aria-hidden': 'true', style: 'position:fixed;top:0;left:0;width:1px;height:1px;opacity:0' }); + ta.value = text; // a textarea's value attribute is ignored: set the property + host.append(ta); + ta.focus(); ta.select(); - document.execCommand('copy'); + try { ok = document.execCommand('copy'); } catch { ok = false; } ta.remove(); + before?.focus?.(); } - toast(what); + toast(ok ? what : 'Couldn’t copy here: select the text and press Ctrl+C (⌘C on a Mac)', ok ? 'info' : 'error'); + return ok; } /** Copies HTML as rich text (with a plain-text alternative) for pasting into docs and chat apps. */ diff --git a/app/static/js/views/probes.js b/app/static/js/views/probes.js index f8c09da..8b64c5c 100644 --- a/app/static/js/views/probes.js +++ b/app/static/js/views/probes.js @@ -19,10 +19,12 @@ function installCommand(info, token) { return `curl -fsSL ${url} | HUB_URL=${hub} PROBE_TOKEN=${token} bash`; } +const README = 'https://git.neilbantoc.dev/services/network-monitor#2-probes'; + function commandBlock(cmd) { const loopback = /^(localhost|127\.|\[::1\])/.test(location.hostname) && cmd.includes(location.origin); return el('div', { style: 'display:grid;gap:6px' }, - el('div.cmd', {}, el('pre', {}, cmd), el('button.btn', { type: 'button', onclick: () => copyText(cmd, 'Install command copied') }, 'Copy')), + el('div.cmd', {}, el('pre.select-all', {}, cmd), el('button.btn', { type: 'button', onclick: () => copyText(cmd, 'Install command copied') }, 'Copy')), loopback ? el('div.callout.error', {}, `You opened this page at ${location.host}, which other machines can't reach. Replace HUB_URL with the hub's LAN address, or set HUB_PUBLIC_URL in the hub's .env.`) : null); } @@ -90,9 +92,10 @@ function list(root, ctx) { function showInstall(p) { const cmd = installCommand(info, p.token); const dlg = modal(`Install “${p.name}”`, el('div', { style: 'display:grid;gap:12px' }, - el('p', { style: 'margin:0' }, 'Run this on the machine that will be the probe. It starts on boot and keeps running. On a Proxmox node it creates a small LXC container for the probe. On macOS and inside LXC containers it installs as a system service. On other Linux hosts it uses Docker. On Linux it needs root: if you aren’t root, put sudo env before HUB_URL. If the git repo is private, the installer asks for a read-only token.'), + el('p', { style: 'margin:0' }, 'Run this on the machine that will be the probe. It installs a service that starts on boot.'), commandBlock(cmd), - el('p.small.muted', { style: 'margin:0' }, `The probe reports to ${info.hub_url || location.origin}. If that address isn't reachable from the probe, change HUB_URL in the command. The command is also on the probe's page.`), + el('p.small.muted', { style: 'margin:0' }, `Reports to ${(cmd.match(/HUB_URL=(\S+)/) || [])[1]}. The command is also on the probe’s page. `, + el('a', { href: README, target: '_blank', rel: 'noopener' }, 'Install notes')), ), { wide: true, footer: [el('a.btn', { href: `/probes/${p.id}`, onclick: () => dlg.close() }, 'Open probe'), el('button.btn.primary', { type: 'button', onclick: () => dlg.close() }, 'Done')] }); }