Initial import of BugPass
This commit is contained in:
commit
df3554542c
20 files changed
+1588
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import { $, bg, askMaster, fingerprint, readOpts, writeOpts, DEFAULTS, getDomain } from "./common.js";
|
||||
|
||||
const q = new URLSearchParams(location.search), id = q.get("id"), site = getDomain(q.get("host"));
|
||||
const prompt = $("#prompt"), create = $("#create");
|
||||
// Extension frames inside web pages get no `tabs` API in Firefox; background uses sender.tab instead.
|
||||
const relay = (msg) => bg("relay", { msg: { ...msg, id } });
|
||||
const close = (refocus) => relay({ type: "close", refocus });
|
||||
let rows = [], sel = -1, focusEl = null;
|
||||
|
||||
for (const el of document.querySelectorAll(".dom")) el.textContent = site;
|
||||
for (const f of [prompt, create]) fingerprint(f.elements.master, f.querySelector(".fp"));
|
||||
new ResizeObserver(() => relay({ type: "resize", h: Math.ceil(document.documentElement.getBoundingClientRect().height) }))
|
||||
.observe(document.body);
|
||||
|
||||
// switch from the list to a form: pull keyboard focus into the iframe
|
||||
function show(form, el) {
|
||||
$("#accounts").hidden = true;
|
||||
form.hidden = false;
|
||||
focusEl = el;
|
||||
relay({ type: "focus" });
|
||||
el.focus();
|
||||
}
|
||||
addEventListener("focus", () => focusEl?.focus());
|
||||
|
||||
async function fill(profile, toast) {
|
||||
try {
|
||||
const r = await bg("fill", { profile, toast });
|
||||
if (r?.locked) {
|
||||
$("#who").textContent = profile.login || site;
|
||||
show(prompt, prompt.elements.master);
|
||||
await askMaster(prompt, () => {}, () => {});
|
||||
await bg("fill", { profile, toast });
|
||||
}
|
||||
} catch (e) { (prompt.hidden ? create : prompt).querySelector("output").textContent = e.message; }
|
||||
}
|
||||
|
||||
async function newAccount() {
|
||||
writeOpts(create, DEFAULTS);
|
||||
create.elements.login.value = q.get("login") ?? "";
|
||||
$("#cmaster").hidden = (await bg("status")).unlocked;
|
||||
show(create, create.elements.login.value && !$("#cmaster").hidden ? create.elements.master : create.elements.login);
|
||||
}
|
||||
create.onsubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
const out = create.querySelector("output"), m = create.elements.master;
|
||||
try {
|
||||
if (!$("#cmaster").hidden) {
|
||||
if (!m.value) return m.focus();
|
||||
const master = m.value;
|
||||
m.value = "";
|
||||
m.dispatchEvent(new Event("input"));
|
||||
await bg("unlock", { master });
|
||||
}
|
||||
const profile = { site, login: create.elements.login.value.trim(), ...readOpts(create) };
|
||||
await bg("save", { profile });
|
||||
await fill(profile, true);
|
||||
} catch (err) { out.textContent = err.message; }
|
||||
};
|
||||
|
||||
const select = (i) => {
|
||||
sel = (i + rows.length) % rows.length;
|
||||
rows.forEach((li, j) => li.classList.toggle("sel", j === sel));
|
||||
rows[sel].scrollIntoView({ block: "nearest" });
|
||||
};
|
||||
const li = (cls, act, ...kids) => {
|
||||
const el = document.createElement("li");
|
||||
el.className = cls;
|
||||
el.append(...kids);
|
||||
el.onclick = act;
|
||||
el.act = act;
|
||||
return el;
|
||||
};
|
||||
const span = (cls, text) => Object.assign(document.createElement(cls === "b" ? "b" : "span"), { className: cls, textContent: text });
|
||||
|
||||
browser.runtime.onMessage.addListener((m, s) => {
|
||||
if (m.type !== "key" || m.id !== id || s.id !== browser.runtime.id || $("#accounts").hidden || !rows.length) return;
|
||||
if (m.key === "ArrowDown") select(sel + 1);
|
||||
else if (m.key === "ArrowUp") select(sel < 0 ? -1 : sel - 1);
|
||||
else if (m.key === "Enter" && sel >= 0) rows[sel].act();
|
||||
});
|
||||
addEventListener("keydown", (e) => {
|
||||
if (e.key === "Escape") close(true);
|
||||
else if (!$("#accounts").hidden && rows.length && ["ArrowDown", "ArrowUp", "Enter"].includes(e.key)) {
|
||||
e.preventDefault();
|
||||
if (e.key === "Enter") sel >= 0 && rows[sel].act();
|
||||
else select(sel + (e.key === "ArrowDown" ? 1 : -1));
|
||||
}
|
||||
});
|
||||
addEventListener("blur", () => close());
|
||||
|
||||
let profiles = [], err = "";
|
||||
try { profiles = (await bg("list")).profiles.filter((p) => p.site === site); } catch (e) { err = `Native host: ${e.message}`; }
|
||||
const sep = document.createElement("li");
|
||||
sep.className = "sep";
|
||||
rows = [
|
||||
...profiles.map((p) => li("acct", () => fill(p), span("b", p.login || "(no login)"), span("dim", p.site))),
|
||||
...(err ? [li("dim", null, err)] : []),
|
||||
];
|
||||
$("#rows").replaceChildren(...rows, ...(rows.length ? [sep] : []),
|
||||
li("act", newAccount, `Create new account on ${site}…`),
|
||||
li("act", () => { bg("openManager"); close(); }, "Manage accounts"));
|
||||
rows = [...$("#rows").children].filter((el) => el.act);
|
||||
Reference in new issue
Block a user