90 lines
4.5 KiB
JavaScript
90 lines
4.5 KiB
JavaScript
// Anchors the extension-owned autofill.html dropdown under login fields and fills on {fill} from background.
|
|
// Never sees the master password; only receives a generated password for this page's own site.
|
|
if (/^(https?|file):$/.test(location.protocol)) {
|
|
const isPw = (el) => el.type === "password";
|
|
const isUser = (el) => ["text", "email", ""].includes(el.getAttribute("type") ?? "") || el.type === "email";
|
|
const pwIn = (root) => [...(root ?? document).querySelectorAll("input[type=password]")];
|
|
const wanted = (el) => el instanceof HTMLInputElement && !el.readOnly && !el.disabled && (isPw(el) ||
|
|
(isUser(el) && (/username|email/.test(el.autocomplete) || el.type === "email" || (el.form && pwIn(el.form).length))));
|
|
// username field: the anchor itself, else the last text/email input before the first password field
|
|
const userFor = (root, pws, a) => (a && !isPw(a) ? a : pws[0]
|
|
? [...root.querySelectorAll("input")].filter((el) => isUser(el) && el.compareDocumentPosition(pws[0]) & Node.DOCUMENT_POSITION_FOLLOWING).pop()
|
|
: document.querySelector("input[autocomplete~=username], input[autocomplete~=email], input[type=email]"));
|
|
|
|
let frame, anchor, id = 0, nav = false, skip = null;
|
|
const send = (m) => browser.runtime.sendMessage({ ...m, id: String(id) }).catch(() => {}); // reaches the iframe directly
|
|
const place = () => {
|
|
const r = anchor.getBoundingClientRect();
|
|
Object.assign(frame.style, { top: `${r.bottom + 2}px`, left: `${r.left}px`, width: `${Math.max(r.width, 280)}px` });
|
|
};
|
|
const close = (refocus) => {
|
|
const a = anchor;
|
|
frame?.remove();
|
|
frame = anchor = null;
|
|
removeEventListener("scroll", place, true);
|
|
removeEventListener("resize", place);
|
|
if (refocus && a) { skip = a; a.focus(); }
|
|
};
|
|
const open = (el) => {
|
|
close();
|
|
anchor = el;
|
|
nav = false;
|
|
const root = el.form ?? document, login = userFor(root, pwIn(root), el)?.value ?? "";
|
|
frame = document.createElement("iframe");
|
|
frame.src = browser.runtime.getURL(`autofill.html?id=${++id}&host=${encodeURIComponent(location.hostname)}&login=${encodeURIComponent(login)}`);
|
|
frame.style.cssText = "position:fixed;height:0;border:0;border-radius:8px;z-index:2147483647;" +
|
|
"box-shadow:0 4px 16px rgba(0,0,0,.3);color-scheme:normal;background:transparent";
|
|
document.documentElement.append(frame);
|
|
place();
|
|
addEventListener("scroll", place, true);
|
|
addEventListener("resize", place);
|
|
};
|
|
|
|
document.addEventListener("focusin", (e) => {
|
|
if (!wanted(e.target) || e.target === anchor) return;
|
|
if (e.target === skip) { skip = null; return; }
|
|
open(e.target);
|
|
});
|
|
document.addEventListener("click", (e) => !frame && wanted(e.target) && open(e.target));
|
|
document.addEventListener("focusout", (e) => e.target === anchor &&
|
|
setTimeout(() => document.activeElement !== frame && document.activeElement !== anchor && close(), 150));
|
|
document.addEventListener("keydown", (e) => {
|
|
if (!frame || e.target !== anchor) return;
|
|
if (e.key === "Escape") return close();
|
|
if (e.key === "ArrowDown" || e.key === "ArrowUp") nav = true;
|
|
else if (!(e.key === "Enter" && nav)) return;
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
send({ type: "key", key: e.key });
|
|
}, true);
|
|
|
|
const set = (el, v) => {
|
|
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set.call(el, v); // bypass React's tracker
|
|
for (const t of ["input", "change"]) el.dispatchEvent(new Event(t, { bubbles: true }));
|
|
};
|
|
const toast = (text) => {
|
|
const d = document.createElement("div");
|
|
d.textContent = text;
|
|
d.style.cssText = "position:fixed;right:16px;bottom:16px;z-index:2147483647;padding:8px 14px;border-radius:6px;" +
|
|
"background:#2b2a33;color:#fbfbfe;font:13px system-ui,sans-serif;box-shadow:0 4px 16px rgba(0,0,0,.3)";
|
|
document.documentElement.append(d);
|
|
setTimeout(() => d.remove(), 3000);
|
|
};
|
|
|
|
browser.runtime.onMessage.addListener((m, sender) => {
|
|
if (sender.id !== browser.runtime.id) return;
|
|
if (m.type === "fill") {
|
|
const root = anchor?.form ?? pwIn()[0]?.form ?? document, pws = pwIn(root), user = userFor(root, pws, anchor);
|
|
if (user && m.username) set(user, m.username);
|
|
for (const el of pws) set(el, m.password); // incl. "confirm password"
|
|
close();
|
|
if (m.toast) toast("Saved to BugPass");
|
|
return;
|
|
}
|
|
if (!frame || m.id !== String(id)) return;
|
|
if (m.type === "close") close(m.refocus);
|
|
else if (m.type === "resize") frame.style.height = `${Math.min(m.h, 480)}px`;
|
|
else if (m.type === "focus") frame.focus();
|
|
});
|
|
}
|