It already matched every http and https page. Local files are labelled "file". The version bump makes installed copies update. Co-Authored-By: Claude Opus 5.5 <[email protected]>
279 lines
12 KiB
HTML
279 lines
12 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Activity Tracker Setup</title>
|
|
<link rel="stylesheet" href="style.css">
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<nav class="topbar">
|
|
<span class="brand">Activity Tracker</span>
|
|
<div class="tabs">
|
|
<a href="./">Dashboard</a>
|
|
<a href="setup" aria-current="page">Setup</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<h1>Setup</h1>
|
|
<p class="muted">Each collector uploads to <code class="base"></code> with its own API key. Open a collector below and click <b>Generate key</b>; the key is filled into its steps. A key is shown only once.</p>
|
|
|
|
<section class="card">
|
|
<div class="row spread"><h2>Devices</h2></div>
|
|
<div class="tablewrap"><table id="devices"><tr><td class="muted">Loading…</td></tr></table></div>
|
|
<form class="row" id="create" style="margin-top:12px">
|
|
<select id="kind" aria-label="Kind">
|
|
<option value="browser">Browser</option>
|
|
<option value="hyprland">Hyprland</option>
|
|
<option value="health">Apple Health</option>
|
|
<option value="manual">Script / manual</option>
|
|
<option value="presence">Presence</option>
|
|
</select>
|
|
<input id="name" placeholder="Name (optional)" aria-label="Name" style="flex:1; min-width:140px">
|
|
<button class="primary">Generate key</button>
|
|
</form>
|
|
<div class="keyslot"></div>
|
|
<p id="status" class="small-text"></p>
|
|
</section>
|
|
|
|
<details class="collector" id="browser" open>
|
|
<summary><b>Browser</b><span class="muted">Violentmonkey userscript · time per site</span></summary>
|
|
<div class="body">
|
|
<p>Records <code>tab_focus</code> intervals labelled by hostname. A tab counts only while it's the selected tab of the focused window, and stops after 5 minutes without input unless video or audio is playing.</p>
|
|
<button class="primary" data-kind="browser">Generate key for this browser</button>
|
|
<div class="keyslot"></div>
|
|
<ol>
|
|
<li>Install Violentmonkey: <a href="https://addons.mozilla.org/firefox/addon/violentmonkey/" target="_blank" rel="noopener">Firefox</a> · <a href="https://chromewebstore.google.com/detail/violentmonkey/jinjaccalgkegednnccohejagnlnfdag" target="_blank" rel="noopener">Chrome / Chromium</a></li>
|
|
<li>Open <a class="script" href="tracker.user.js"></a> and click <b>Install</b>. Violentmonkey checks this URL for updates.</li>
|
|
<li>Click the Violentmonkey toolbar icon, then <b>Activity Tracker → Set server and API key</b>. Enter the server and key:
|
|
<pre class="base"></pre>
|
|
<pre class="key">trk_… (generate a key above)</pre></li>
|
|
<li>Check it with <b>Status / upload now</b> in the same menu. Uploads happen every minute.</li>
|
|
</ol>
|
|
<p class="muted small-text">The menu also turns full URLs and page titles on or off (both off by default), sets hosts to ignore, and changes the idle timeout. It runs on every web page and on local <code>file://</code> pages (labelled <code>file</code>; in Chrome, turn on <b>Allow access to file URLs</b> in Violentmonkey's extension details). Browsers block all extensions on <code>about:</code> pages, the new tab page, extension stores and the PDF viewer, so those aren't recorded; Hyprland still counts them as browser time.</p>
|
|
</div>
|
|
</details>
|
|
|
|
<details class="collector" id="hyprland">
|
|
<summary><b>Hyprland desktop</b><span class="muted">focused window, idle and locked time</span></summary>
|
|
<div class="body">
|
|
<p>Records <code>app_focus</code> intervals labelled by window class, with the title.</p>
|
|
<button class="primary" data-kind="hyprland">Generate key for this machine</button>
|
|
<div class="keyslot"></div>
|
|
<h3>1. Install the agent</h3>
|
|
<pre>git clone ssh://[email protected]:2222/adamf/ccp.git && cd ccp
|
|
make install-agent # ~/.local/bin/hypr-agent and the systemd user unit</pre>
|
|
<h3>2. Configure and start it</h3>
|
|
<pre>mkdir -p ~/.config/hypr-agent
|
|
cat > ~/.config/hypr-agent/env <<EOF
|
|
TRACKER_URL=<span class="base"></span>
|
|
TRACKER_API_KEY=<span class="key">trk_…</span>
|
|
# HYPR_AGENT_TITLES=false # don't send window titles at all
|
|
# HYPR_AGENT_REDACT=(?i)bank|private # replace matching titles with [redacted]
|
|
EOF
|
|
chmod 600 ~/.config/hypr-agent/env
|
|
systemctl --user enable --now hypr-agent</pre>
|
|
<h3>3. Idle and lock (optional)</h3>
|
|
<p>Add to <code>~/.config/hypr/hypridle.conf</code>:</p>
|
|
<pre>general {
|
|
lock_cmd = hypr-agent lock; pidof hyprlock || hyprlock
|
|
unlock_cmd = hypr-agent unlock
|
|
}
|
|
|
|
listener {
|
|
timeout = 300
|
|
on-timeout = hypr-agent idle
|
|
on-resume = hypr-agent resume
|
|
}</pre>
|
|
<p class="muted small-text">Check it with <code>journalctl --user -u hypr-agent -f</code>. Events upload every minute and wait in <code>~/.local/state/hypr-agent/buffer.jsonl</code> while the server can't be reached.</p>
|
|
</div>
|
|
</details>
|
|
|
|
<details class="collector" id="health">
|
|
<summary><b>Apple Health</b><span class="muted">walks, workouts, steps, sleep</span></summary>
|
|
<div class="body">
|
|
<p>Workouts become activities (<code>walk</code>, <code>run</code>, <code>workout</code>); step counts, sleep and other metrics become samples.</p>
|
|
<button class="primary" data-kind="health">Generate key for Health Auto Export</button>
|
|
<div class="keyslot"></div>
|
|
<p>In the Health Auto Export iOS app, create a <b>REST API</b> automation:</p>
|
|
<ul>
|
|
<li>URL: <code><span class="base"></span>/api/v1/health/hae</code></li>
|
|
<li>Header: <code>Authorization</code> = <code>Bearer <span class="key">trk_…</span></code></li>
|
|
<li>Format: JSON · Sync cadence: hourly</li>
|
|
<li>Data: Workouts, plus metrics such as Step Count and Sleep Analysis</li>
|
|
</ul>
|
|
</div>
|
|
</details>
|
|
|
|
<details class="collector" id="manual">
|
|
<summary><b>Scripts</b><span class="muted">post any interval with curl</span></summary>
|
|
<div class="body">
|
|
<button class="primary" data-kind="manual">Generate key for scripts</button>
|
|
<div class="keyslot"></div>
|
|
<pre>curl -X POST <span class="base"></span>/api/v1/manual/events \
|
|
-H 'Authorization: Bearer <span class="key">trk_…</span>' -H 'Content-Type: application/json' \
|
|
-d '{"events":[{"id":"reading-2026-10-14","kind":"reading","label":"book",
|
|
"started_at":"2026-10-14T20:00:00Z","ended_at":"2026-10-14T20:45:00Z"}]}'</pre>
|
|
<p class="muted small-text">The <code>id</code> makes re-sending safe: a duplicate is ignored. Webcam presence (<code>/api/v1/presence/events</code>) has an endpoint but no agent yet.</p>
|
|
</div>
|
|
</details>
|
|
|
|
<details class="collector" id="goals">
|
|
<summary><b>Goals</b><span class="muted">daily targets on the dashboard</span></summary>
|
|
<div class="body">
|
|
<p>Managed on the Pi. An empty label matches every label of that kind.</p>
|
|
<pre>docker exec activity-tracker tracker goal add --kind tab_focus --label youtube.com --op '<=' --target 1h
|
|
docker exec activity-tracker tracker goal add --kind walk --op '>=' --target 30m
|
|
docker exec activity-tracker tracker goal list
|
|
docker exec activity-tracker tracker goal rm --id 2</pre>
|
|
</div>
|
|
</details>
|
|
</main>
|
|
<script>
|
|
const $ = id => document.getElementById(id);
|
|
const esc = s => String(s ?? "").replace(/[&<>"]/g, c => ({ "&":"&", "<":"<", ">":">", '"':""" }[c]));
|
|
|
|
function ago(t) {
|
|
if (!t) return "never";
|
|
const s = (Date.now() - new Date(t)) / 1000;
|
|
if (s < 90) return "just now";
|
|
if (s < 3600) return Math.round(s / 60) + " min ago";
|
|
if (s < 86400) return Math.round(s / 3600) + " h ago";
|
|
return new Date(t).toLocaleDateString([], { day: "numeric", month: "short", year: "numeric" });
|
|
}
|
|
|
|
// The tracker's public URL, e.g. https://www.adam-french.co.uk/tracker
|
|
const base = new URL(".", location.href).href.replace(/\/$/, "");
|
|
document.querySelectorAll(".base").forEach(el => el.textContent = base);
|
|
document.querySelectorAll("a.script").forEach(el => el.textContent = base + "/tracker.user.js");
|
|
|
|
async function api(method, path, body) {
|
|
const r = await fetch("api/v1/" + path, {
|
|
method, headers: body ? { "Content-Type": "application/json" } : {},
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
const out = await r.json().catch(() => ({}));
|
|
if (r.status === 401) { location.reload(); throw new Error("session expired"); }
|
|
if (!r.ok) throw new Error(out.error || r.statusText);
|
|
return out;
|
|
}
|
|
|
|
function status(msg, cls) {
|
|
$("status").className = "small-text " + (cls || "");
|
|
$("status").textContent = msg;
|
|
}
|
|
|
|
async function copy(text, btn) {
|
|
try { await navigator.clipboard.writeText(text); btn.textContent = "Copied"; }
|
|
catch { btn.textContent = "Copy failed"; }
|
|
setTimeout(() => btn.textContent = "Copy", 1500);
|
|
}
|
|
|
|
// A Copy button on every command block.
|
|
document.querySelectorAll("pre").forEach(pre => {
|
|
const wrap = document.createElement("div");
|
|
wrap.className = "codeblock";
|
|
pre.replaceWith(wrap);
|
|
wrap.append(pre);
|
|
const btn = document.createElement("button");
|
|
btn.className = "small copy"; btn.type = "button"; btn.textContent = "Copy";
|
|
btn.onclick = () => copy(pre.innerText, btn);
|
|
wrap.append(btn);
|
|
});
|
|
|
|
// ---- devices and keys ----
|
|
|
|
let devices = [];
|
|
|
|
async function loadDevices() {
|
|
try {
|
|
({ devices } = await api("GET", "devices"));
|
|
const active = devices.filter(d => !d.revoked_at);
|
|
const revoked = devices.filter(d => d.revoked_at);
|
|
$("devices").innerHTML = `<tr><th>Name</th><th>Kind</th><th>Last upload</th><th></th></tr>` +
|
|
(active.length ? active.map(d => `<tr>
|
|
<td class="label">${esc(d.name)}</td>
|
|
<td><span class="badge">${esc(d.kind)}</span></td>
|
|
<td class="time">${ago(d.last_seen_at)}</td>
|
|
<td class="num"><button class="small" data-revoke="${esc(d.name)}">Revoke</button></td></tr>`).join("")
|
|
: `<tr><td colspan="4" class="muted">No devices yet.</td></tr>`) +
|
|
(revoked.length ? `<tr><td colspan="4" class="muted small-text">${revoked.length} revoked: ${revoked.map(d => esc(d.name)).join(", ")}</td></tr>` : "");
|
|
} catch (e) {
|
|
status(e.message, "err");
|
|
}
|
|
}
|
|
|
|
// A readable name that isn't taken yet (names stay taken after revoking).
|
|
function suggestName(kind) {
|
|
let stem = { health: "iphone-health", manual: "script" }[kind] || kind;
|
|
if (kind === "browser") {
|
|
const ua = navigator.userAgent;
|
|
stem = /Firefox\//.test(ua) ? "firefox" : /Edg\//.test(ua) ? "edge" : /Chrome\//.test(ua) ? "chrome" : "browser";
|
|
}
|
|
const taken = new Set(devices.map(d => d.name));
|
|
let name = stem;
|
|
for (let i = 2; taken.has(name); i++) name = `${stem}-${i}`;
|
|
return name;
|
|
}
|
|
|
|
function showKey(slot, device, key) {
|
|
document.querySelectorAll(".keyslot").forEach(s => s.innerHTML = "");
|
|
slot.innerHTML = `<div class="keybox">
|
|
<div>Key for <b>${esc(device.name)}</b> <span class="badge">${esc(device.kind)}</span>. Copy it now; it won't be shown again.</div>
|
|
<div class="codeblock"><pre>${esc(key)}</pre><button class="small copy" type="button">Copy</button></div>
|
|
</div>`;
|
|
const btn = slot.querySelector("button");
|
|
btn.onclick = () => copy(key, btn);
|
|
copy(key, btn);
|
|
}
|
|
|
|
async function generate(kind, name, slot, button) {
|
|
button.disabled = true;
|
|
try {
|
|
if (!devices.length) await loadDevices();
|
|
const { device, key } = await api("POST", "devices", { kind, name: name || suggestName(kind) });
|
|
showKey(slot, device, key);
|
|
// Fill the key into this collector's steps so they can be copied as-is.
|
|
const section = $(kind);
|
|
if (section) {
|
|
section.open = true;
|
|
section.querySelectorAll(".key").forEach(el => el.textContent = key);
|
|
}
|
|
status("");
|
|
loadDevices();
|
|
return true;
|
|
} catch (e) {
|
|
status(e.message, "err");
|
|
return false;
|
|
} finally {
|
|
button.disabled = false;
|
|
}
|
|
}
|
|
|
|
$("create").onsubmit = e => {
|
|
e.preventDefault();
|
|
const btn = e.submitter || $("create").querySelector("button");
|
|
generate($("kind").value, $("name").value.trim(), $("create").nextElementSibling, btn)
|
|
.then(ok => { if (ok) $("name").value = ""; });
|
|
};
|
|
document.querySelectorAll("button[data-kind]").forEach(btn => {
|
|
btn.onclick = () => generate(btn.dataset.kind, "", btn.nextElementSibling, btn);
|
|
});
|
|
|
|
$("devices").onclick = async e => {
|
|
const name = e.target.dataset?.revoke;
|
|
if (!name || !confirm(`Revoke ${name}? Its collector will stop being able to upload.`)) return;
|
|
try {
|
|
await api("DELETE", "devices/" + encodeURIComponent(name));
|
|
status(`Revoked ${name}.`, "ok");
|
|
loadDevices();
|
|
} catch (err) { status(err.message, "err"); }
|
|
};
|
|
|
|
if (location.hash && $(location.hash.slice(1))?.tagName === "DETAILS") $(location.hash.slice(1)).open = true;
|
|
loadDevices();
|
|
</script>
|
|
</body>
|
|
</html>
|