88 lines
3.3 KiB
JavaScript
88 lines
3.3 KiB
JavaScript
import { buildFingerprint } from "./lesspass.js";
|
|
import { getDomain } from "./domain.js";
|
|
import { ICONS } from "./icons.js";
|
|
export { getDomain } from "./domain.js";
|
|
|
|
export const DEFAULTS = { lowercase: true, uppercase: true, digits: true, symbols: true, length: 16, counter: 1 };
|
|
export const $ = (s) => document.querySelector(s);
|
|
|
|
// Everything secret goes through background.js; pages only see passwords via reveal.
|
|
export const bg = (type, data) => browser.runtime.sendMessage({ type, ...data });
|
|
|
|
// Show `form` (with input[name=master] + .fp), resolve once background is unlocked.
|
|
export function askMaster(form, show = () => { form.hidden = false; }, hide = () => { form.hidden = true; }) {
|
|
show();
|
|
const input = form.elements.master, out = form.querySelector("output");
|
|
input.focus();
|
|
return new Promise((resolve) => {
|
|
form.onsubmit = async (e) => {
|
|
e.preventDefault();
|
|
const master = input.value;
|
|
input.value = "";
|
|
input.dispatchEvent(new Event("input"));
|
|
if (!master) return input.focus();
|
|
try { await bg("unlock", { master }); } catch (err) { if (out) out.textContent = err.message; return; }
|
|
hide();
|
|
resolve();
|
|
};
|
|
});
|
|
}
|
|
|
|
// Run a background action; if it reports locked, prompt via `ask()` and retry once.
|
|
export async function withUnlock(type, data, ask) {
|
|
let r = await bg(type, data);
|
|
if (r?.locked) { await ask(); r = await bg(type, data); }
|
|
return r;
|
|
}
|
|
|
|
export const domainOfTab = (tab) => {
|
|
try { const u = new URL(tab.url); if (u.protocol.startsWith("http")) return getDomain(u.hostname); } catch {}
|
|
return "";
|
|
};
|
|
|
|
// Live fingerprint (3 colored glyphs) next to a master password input.
|
|
export function fingerprint(input, el) {
|
|
let timer, seq = 0;
|
|
input.addEventListener("input", () => {
|
|
clearTimeout(timer);
|
|
const n = ++seq;
|
|
if (!input.value) return el.replaceChildren();
|
|
timer = setTimeout(async () => {
|
|
const fp = await buildFingerprint(input.value);
|
|
if (n !== seq) return;
|
|
el.replaceChildren(...fp.map(({ icon, color }) => {
|
|
const s = document.createElement("span");
|
|
s.textContent = ICONS[icon] ?? "?";
|
|
s.title = icon.slice(3);
|
|
s.style.background = color;
|
|
return s;
|
|
}));
|
|
}, 300);
|
|
});
|
|
}
|
|
|
|
// Pull the active browser theme's colors into our CSS vars.
|
|
export async function applyTheme() {
|
|
const c = (await browser.theme.getCurrent())?.colors ?? {};
|
|
const vars = {
|
|
bg: c.popup, fg: c.popup_text, border: c.popup_border, hl: c.popup_highlight, "hl-fg": c.popup_highlight_text,
|
|
field: c.toolbar_field, "field-fg": c.toolbar_field_text, "btn-active": c.button_background_active,
|
|
accent: c.accentcolor ?? c.frame,
|
|
};
|
|
for (const [k, v] of Object.entries(vars))
|
|
if (v) document.documentElement.style.setProperty(`--${k}`, Array.isArray(v) ? `rgb(${v})` : v);
|
|
}
|
|
applyTheme().catch(() => {});
|
|
|
|
// Options form (a-z, A-Z, 0-9, %!@, length, counter) <-> profile object.
|
|
export const OPTS = ["lowercase", "uppercase", "digits", "symbols"];
|
|
export const readOpts = (form) => ({
|
|
...Object.fromEntries(OPTS.map((k) => [k, form.elements[k].checked])),
|
|
length: +form.elements.len.value, counter: +form.elements.counter.value,
|
|
});
|
|
export const writeOpts = (form, p) => {
|
|
for (const k of OPTS) form.elements[k].checked = p[k];
|
|
form.elements.len.value = p.length;
|
|
form.elements.counter.value = p.counter;
|
|
};
|