97 lines
4.1 KiB
JavaScript
97 lines
4.1 KiB
JavaScript
import { $, bg, askMaster, withUnlock, fingerprint, readOpts, writeOpts, DEFAULTS, getDomain } from "./common.js";
|
|
|
|
const dlg = $("#dlg"), prompt = $("#prompt"), editor = $("#editor"), out = $("#out");
|
|
fingerprint(prompt.elements.master, prompt.querySelector(".fp"));
|
|
$("#dlgCancel").onclick = () => dlg.close();
|
|
const ask = () => askMaster(prompt, () => dlg.showModal(), () => dlg.close()).then(state);
|
|
const say = (t, err) => { out.textContent = t; out.className = err ? "err" : ""; };
|
|
const hidePw = () => { $("#pw").textContent = "••••••••"; $("#reveal").title = "Reveal"; };
|
|
let profiles = [], cur = null;
|
|
|
|
async function state() {
|
|
const { unlocked } = await bg("status");
|
|
$("#state").textContent = unlocked ? "🔓 Unlocked" : "🔒 Locked";
|
|
$("#lock").textContent = unlocked ? "Lock" : "Unlock";
|
|
$("#lock").onclick = unlocked ? () => { hidePw(); bg("lock").then(state); } : ask;
|
|
}
|
|
|
|
function renderList() {
|
|
const q = $("#search").value.toLowerCase();
|
|
$("#list").replaceChildren(...profiles
|
|
.filter((p) => `${p.site} ${p.login}`.toLowerCase().includes(q))
|
|
.sort((a, b) => a.site.localeCompare(b.site) || a.login.localeCompare(b.login))
|
|
.map((p) => {
|
|
const li = document.createElement("li");
|
|
li.tabIndex = 0;
|
|
li.classList.toggle("sel", p === cur);
|
|
li.append(Object.assign(document.createElement("b"), { textContent: p.site }),
|
|
Object.assign(document.createElement("span"), { className: "dim", textContent: p.login || "(no login)" }));
|
|
li.onclick = li.onkeydown = (e) => (e.type === "click" || e.key === "Enter") && view(p);
|
|
return li;
|
|
}));
|
|
}
|
|
|
|
async function refresh(keep) {
|
|
try { profiles = (await bg("list")).profiles; } catch (e) { $("#list").textContent = `Native host: ${e.message}`; return; }
|
|
cur = keep && profiles.find((p) => p.site === keep.site && p.login === keep.login) || null;
|
|
renderList();
|
|
cur ? view(cur) : ($("#view").hidden = true);
|
|
}
|
|
|
|
function view(p) {
|
|
cur = p;
|
|
hidePw();
|
|
say("");
|
|
$("#title").textContent = p.site;
|
|
$("#site").textContent = p.site;
|
|
$("#site").href = `https://${p.site}`;
|
|
$("#login").textContent = p.login || "(no login)";
|
|
editor.hidden = true;
|
|
$("#view").hidden = false;
|
|
renderList();
|
|
}
|
|
|
|
function edit(p) {
|
|
editor.elements.site.value = p?.site ?? "";
|
|
editor.elements.login.value = p?.login ?? "";
|
|
writeOpts(editor, { ...DEFAULTS, ...p });
|
|
$("#etitle").textContent = p ? `Edit ${p.site}` : "New account";
|
|
editor.querySelector("output").textContent = "";
|
|
$("#view").hidden = true;
|
|
editor.hidden = false;
|
|
editor.elements[p ? "login" : "site"].focus();
|
|
}
|
|
|
|
const act = (fn) => async () => { try { await fn(); } catch (e) { say(e.message, true); } };
|
|
$("#search").oninput = renderList;
|
|
$("#new").onclick = () => { cur = null; renderList(); edit(null); };
|
|
$("#edit").onclick = () => edit(cur);
|
|
$("#cancel").onclick = () => (cur ? view(cur) : (editor.hidden = true));
|
|
$("#copyLogin").onclick = act(async () => { await navigator.clipboard.writeText(cur.login); say("Username copied"); });
|
|
$("#copyPw").onclick = act(async () => { await withUnlock("copy", { profile: cur }, ask); say("Password copied — clears in 30s"); });
|
|
$("#reveal").onclick = act(async () => {
|
|
if ($("#reveal").title === "Hide") return hidePw();
|
|
$("#pw").textContent = (await withUnlock("reveal", { profile: cur }, ask)).password;
|
|
$("#reveal").title = "Hide";
|
|
});
|
|
$("#remove").onclick = act(async () => {
|
|
if (!confirm(`Remove ${cur.login || "(no login)"} on ${cur.site}?`)) return;
|
|
await bg("delete", { site: cur.site, login: cur.login });
|
|
refresh();
|
|
});
|
|
editor.onsubmit = async (e) => {
|
|
e.preventDefault();
|
|
const raw = editor.elements.site.value.trim();
|
|
let host = raw;
|
|
try { host = new URL(raw.includes("://") ? raw : `https://${raw}`).hostname; } catch {}
|
|
const p = { site: getDomain(host), login: editor.elements.login.value.trim(), ...readOpts(editor) };
|
|
try {
|
|
await bg("save", { profile: p });
|
|
if (cur && (cur.site !== p.site || cur.login !== p.login)) await bg("delete", { site: cur.site, login: cur.login });
|
|
refresh(p);
|
|
} catch (err) { editor.querySelector("output").textContent = err.message; }
|
|
};
|
|
addEventListener("focus", state);
|
|
state();
|
|
refresh();
|