[extension] Redesign: the popup in layout C with the custos room, Custos's theme, a new password on sign-up and change-password forms, Save or Update
This commit is contained in:
+153
-35
@@ -19,6 +19,11 @@
|
||||
gone: 'That offer has expired.',
|
||||
};
|
||||
const say = (code) => SENTENCES[code] ?? 'Custos could not do that.';
|
||||
const SAVED = { new: 'Saved to Custos.', updated: 'Updated in Custos.', unchanged: 'Custos already has this login.' };
|
||||
const offerText = (o) =>
|
||||
o.update
|
||||
? `Update the password of ${o.username || 'your login'} on ${o.host} in Custos?`
|
||||
: `Save the login${o.username ? ` of ${o.username}` : ''} for ${o.host} to Custos?`;
|
||||
const send = (msg) => Promise.resolve(api.runtime.sendMessage(msg)).catch(() => ({ ok: false, error: 'not_running' }));
|
||||
|
||||
const visible = (el) => {
|
||||
@@ -32,6 +37,32 @@
|
||||
(el.autocomplete === 'one-time-code' || /(^|[^a-z])(otp|totp|2fa|mfa|one.?time|verification.?code)/i.test(`${el.name} ${el.id}`));
|
||||
const isUserName = (el) => isText(el) && /username|email/i.test(`${el.autocomplete} ${el.name} ${el.id} ${el.type}`);
|
||||
|
||||
/**
|
||||
* The fields of a sign-up or change-password form that take a new password (and its repeat), around `near` (a
|
||||
* field, or the whole page): the ones the page marks `new-password` or names new/confirm/repeat; else, in a form
|
||||
* with two password fields, both (a password and its repeat), and with three, the last two (the current one first).
|
||||
*/
|
||||
function newPasswordFields(near) {
|
||||
const scope = near?.form ?? near?.closest?.('form') ?? document;
|
||||
const pws = [...scope.querySelectorAll('input[type=password]')].filter(visible);
|
||||
const marked = pws.filter(
|
||||
(el) => el.autocomplete === 'new-password' || /(^|[^a-z])(new|confirm|repeat|again|retype)/i.test(`${el.name} ${el.id}`),
|
||||
);
|
||||
if (marked.length) return marked;
|
||||
if (pws.length === 2 && pws.every((el) => el.autocomplete !== 'current-password')) return pws;
|
||||
if (pws.length === 3) return pws.slice(1);
|
||||
return [];
|
||||
}
|
||||
|
||||
/** The new-password fields of the first form on the page that has any. */
|
||||
function firstNewFields() {
|
||||
for (const f of [...document.forms, null]) {
|
||||
const found = newPasswordFields(f ? (f.querySelector('input[type=password]') ?? f) : null);
|
||||
if (found.length) return found;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/** The username field of a password field: the one the page marks, else the last text field before it. */
|
||||
function usernameFor(pw) {
|
||||
const inputs = [...(pw.form ?? document).querySelectorAll('input')].filter(visible);
|
||||
@@ -51,7 +82,8 @@
|
||||
return { user, pass, otp };
|
||||
}
|
||||
|
||||
// Fields Custos filled and the person has not typed in since: a form sent with them is not offered for saving.
|
||||
// Fields Custos filled from a saved login and the person has not typed in since: a form sent with them is not
|
||||
// offered for saving. A new password Custos made is not among them: sending that form offers to save it.
|
||||
const filledByCustos = new WeakSet();
|
||||
document.addEventListener('input', (e) => e.isTrusted && filledByCustos.delete(e.target), true);
|
||||
|
||||
@@ -65,6 +97,24 @@
|
||||
filledByCustos.add(el);
|
||||
}
|
||||
|
||||
/** A new password into every field that takes it (the password and its repeat). */
|
||||
function putNew(fields, password) {
|
||||
for (const el of fields) {
|
||||
put(el, password);
|
||||
filledByCustos.delete(el);
|
||||
}
|
||||
}
|
||||
|
||||
/** Asks Custos for a new password and puts it into the form's new-password fields around `near`. */
|
||||
async function fillNew(near) {
|
||||
const fields = newPasswordFields(near);
|
||||
if (!fields.length) return 'This form has no field for a new password.';
|
||||
const r = await send({ kind: 'generate' });
|
||||
if (!r?.ok || typeof r.password !== 'string') return say(r?.error);
|
||||
putNew(fields, r.password);
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Fills login `id` around `near`. Only what the page has fields for is asked for: a 2FA page gets the code only. */
|
||||
async function fill(id, near) {
|
||||
const f = fieldsAround(near);
|
||||
@@ -88,34 +138,60 @@
|
||||
const root = host.attachShadow({ mode: 'closed' });
|
||||
root.innerHTML = `
|
||||
<style>
|
||||
/* @domi/ui's custos room: dark, and parchment in the light theme. Custos's theme decides; until it answers,
|
||||
the system's. */
|
||||
:host { all: initial; }
|
||||
.chip, .menu, .offer { position: fixed; z-index: 2147483647; font: 13px/1.4 "Segoe UI", system-ui, sans-serif; }
|
||||
.chip { width: 24px; height: 24px; padding: 0; border: 1px solid #5d6573; border-radius: 6px; background: #15181d;
|
||||
cursor: pointer; display: grid; place-items: center; box-shadow: 0 2px 6px rgba(0,0,0,.35); }
|
||||
.chip:hover, .chip:focus-visible { border-color: #c9d1dc; outline: none; }
|
||||
.menu, .offer { min-width: 240px; max-width: 360px; padding: 6px; border: 1px solid #3a404b; border-radius: 10px;
|
||||
background: #15181d; color: #e8ebf0; box-shadow: 0 14px 32px rgba(0,0,0,.45); }
|
||||
.offer { top: 12px; right: 12px; padding: 10px 12px; }
|
||||
.head { padding: 4px 8px 6px; color: #9aa3b1; font-size: 11px; letter-spacing: .14em; text-transform: uppercase; }
|
||||
button.login { display: grid; width: 100%; padding: 7px 8px; border: 0; border-radius: 7px; background: none;
|
||||
.ui { --bg: #121519; --hover: #1a1e24; --line: #242930; --fg: #eef1f4; --fg2: #c4cbd3; --muted: #959fab;
|
||||
--accent: #d3dce5; --leaf: linear-gradient(#ffffff, #dfe5eb 55%, #b7c1cc); --leaf-fg: #0a0c0f; --rim: #aab5c1;
|
||||
--shadow: 0 14px 32px rgba(0, 0, 0, .45); }
|
||||
@media (prefers-color-scheme: light) {
|
||||
.ui:not([data-domi-theme="dark"]) { --bg: #fbf8f1; --hover: #ece5d5; --line: #d8cdb6; --fg: #1e1b16;
|
||||
--fg2: #3e382e; --muted: #5e5647; --accent: #666e76; --leaf: linear-gradient(#6e777f, #666e76 55%, #525a62);
|
||||
--leaf-fg: #ffffff; --rim: #666e76; --shadow: 0 14px 32px rgba(60, 48, 24, .22); }
|
||||
}
|
||||
.ui[data-domi-theme="light"] { --bg: #fbf8f1; --hover: #ece5d5; --line: #d8cdb6; --fg: #1e1b16; --fg2: #3e382e;
|
||||
--muted: #5e5647; --accent: #666e76; --leaf: linear-gradient(#6e777f, #666e76 55%, #525a62); --leaf-fg: #ffffff;
|
||||
--rim: #666e76; --shadow: 0 14px 32px rgba(60, 48, 24, .22); }
|
||||
.chip, .menu, .offer { position: fixed; z-index: 2147483647;
|
||||
font: 13px/1.4 "Manrope Variable", Manrope, "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif; }
|
||||
.chip { width: 24px; height: 24px; padding: 0; border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
|
||||
color: var(--accent); cursor: pointer; display: grid; place-items: center; box-shadow: 0 2px 6px rgba(0, 0, 0, .25); }
|
||||
.chip:hover, .chip:focus-visible { border-color: var(--rim); outline: none; }
|
||||
.menu, .offer { min-width: 250px; max-width: 360px; padding: 6px; border: 1px solid var(--line); border-radius: 14px;
|
||||
background: var(--bg); color: var(--fg); box-shadow: var(--shadow); }
|
||||
.offer { top: 12px; right: 12px; padding: 12px; }
|
||||
.head { padding: 4px 8px 6px; color: var(--muted); font-size: 11px; letter-spacing: .3em; }
|
||||
button.login { display: grid; width: 100%; padding: 8px 10px; border: 0; border-radius: 10px; background: none;
|
||||
color: inherit; text-align: left; cursor: pointer; font: inherit; }
|
||||
button.login:hover, button.login:focus-visible { background: #262b33; outline: none; }
|
||||
button.login:hover, button.login:focus-visible { background: var(--hover); outline: none; }
|
||||
button.login.new { margin-bottom: 4px; border: 1px dashed var(--line); }
|
||||
.title { font-weight: 600; }
|
||||
.user { color: #9aa3b1; font-size: 12px; }
|
||||
.note { padding: 6px 8px; color: #c9d1dc; }
|
||||
.row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
|
||||
.row button { padding: 5px 12px; border: 1px solid #3a404b; border-radius: 7px; background: #1d2128; color: inherit;
|
||||
font: inherit; cursor: pointer; }
|
||||
.row button.primary { border-color: #c9a96e; }
|
||||
.user { color: var(--muted); font-size: 12px; }
|
||||
.note { padding: 6px 8px; color: var(--fg2); }
|
||||
.row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }
|
||||
.row button { height: 30px; padding: 0 14px; border: 1px solid var(--line); border-radius: 10px; background: none;
|
||||
color: inherit; font: inherit; font-weight: 600; cursor: pointer; }
|
||||
.row button:hover, .row button:focus-visible { background: var(--hover); outline: none; }
|
||||
.row button.primary { border-color: transparent; background: var(--leaf); color: var(--leaf-fg); }
|
||||
[hidden] { display: none !important; }
|
||||
</style>
|
||||
<button class="chip" type="button" title="Fill from Custos" aria-label="Fill from Custos" hidden>
|
||||
<svg width="14" height="14" viewBox="0 0 16 16" aria-hidden="true"><path d="M8 1.5l5 2v4c0 3.2-2.1 5.8-5 7-2.9-1.2-5-3.8-5-7v-4z"
|
||||
fill="none" stroke="#c9d1dc" stroke-width="1.5" stroke-linejoin="round"/><path d="M5.8 8l1.6 1.6L10.4 6.4" fill="none"
|
||||
stroke="#c9d1dc" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
|
||||
fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/><path d="M5.8 8l1.6 1.6L10.4 6.4" fill="none"
|
||||
stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
|
||||
</button>
|
||||
<div class="menu" role="menu" aria-label="Custos logins" hidden></div>
|
||||
<div class="offer" role="dialog" aria-label="Save in Custos" hidden></div>`;
|
||||
<div class="offer" role="dialog" aria-label="Save to Custos" hidden></div>`;
|
||||
// Every part sits in one box that carries the theme.
|
||||
const ui = document.createElement('div');
|
||||
ui.className = 'ui';
|
||||
ui.append(...[...root.children].filter((c) => c.tagName !== 'STYLE'));
|
||||
root.append(ui);
|
||||
/** Custos's theme, from any answer that carries it: `light`, `dark`, or `auto` (the system's). */
|
||||
function theme(r) {
|
||||
if (r?.theme === 'light' || r?.theme === 'dark') ui.dataset.domiTheme = r.theme;
|
||||
else if (r?.theme === 'auto') delete ui.dataset.domiTheme;
|
||||
}
|
||||
const chip = root.querySelector('.chip');
|
||||
const menu = root.querySelector('.menu');
|
||||
const offerBox = root.querySelector('.offer');
|
||||
@@ -149,9 +225,13 @@
|
||||
note('Asking Custos…');
|
||||
const r = await send({ kind: 'match' });
|
||||
if (field !== target) return;
|
||||
if (!r?.ok) return note(say(r?.error));
|
||||
if (!r.logins.length) return note('Custos has no login for this site.');
|
||||
const items = r.logins.map((l) => {
|
||||
theme(r);
|
||||
// A sign-up or change-password field: a new password from Custos comes first.
|
||||
const fresh = newPasswordFields(target).includes(target) ? [newItem(target)] : [];
|
||||
if (!r?.ok && !fresh.length) return note(say(r?.error));
|
||||
const logins = r?.ok ? r.logins : [];
|
||||
if (!logins.length && !fresh.length) return note('Custos has no login for this site.');
|
||||
const items = logins.map((l) => {
|
||||
const b = el('button', { className: 'login', type: 'button' });
|
||||
b.setAttribute('role', 'menuitem');
|
||||
b.append(
|
||||
@@ -166,8 +246,25 @@
|
||||
});
|
||||
return b;
|
||||
});
|
||||
menu.replaceChildren(el('div', { className: 'head', textContent: 'Custos' }), ...items);
|
||||
items[0]?.focus();
|
||||
menu.replaceChildren(el('div', { className: 'head', textContent: 'CUSTOS' }), ...fresh, ...items);
|
||||
(fresh[0] ?? items[0])?.focus();
|
||||
}
|
||||
|
||||
/** "Use a new password": Custos makes one, and it goes into this form's new-password fields only. */
|
||||
function newItem(target) {
|
||||
const b = el('button', { className: 'login new', type: 'button' });
|
||||
b.setAttribute('role', 'menuitem');
|
||||
b.append(
|
||||
el('span', { className: 'title', textContent: 'Use a new password' }),
|
||||
el('span', { className: 'user', textContent: 'Made by Custos. Sending the form offers to save it.' }),
|
||||
);
|
||||
b.addEventListener('click', async (e) => {
|
||||
if (!e.isTrusted) return;
|
||||
const why = await fillNew(target);
|
||||
if (why) note(why);
|
||||
else hide();
|
||||
});
|
||||
return b;
|
||||
}
|
||||
|
||||
chip.addEventListener('click', (e) => e.isTrusted && (menu.hidden ? void open() : (menu.hidden = true)));
|
||||
@@ -198,13 +295,14 @@
|
||||
function showOffer(offer) {
|
||||
if (!offer || window.top !== window) return;
|
||||
attach();
|
||||
const yes = el('button', { type: 'button', className: 'primary', textContent: 'Save in Custos' });
|
||||
void send({ kind: 'status' }).then(theme);
|
||||
const yes = el('button', { type: 'button', className: 'primary', textContent: offer.update ? 'Update' : 'Save to Custos' });
|
||||
const no = el('button', { type: 'button', textContent: 'Not now' });
|
||||
yes.addEventListener('click', async (e) => {
|
||||
if (!e.isTrusted) return;
|
||||
offerBox.replaceChildren(el('div', { className: 'note', textContent: 'Answer in Custos…' }));
|
||||
const r = await send({ kind: 'save' });
|
||||
const done = r?.ok ? (r.saved === 'unchanged' ? 'Custos already has this login.' : 'Saved in Custos.') : say(r?.error);
|
||||
const done = r?.ok ? (SAVED[r.saved] ?? SAVED.new) : say(r?.error);
|
||||
offerBox.replaceChildren(el('div', { className: 'note', textContent: done }));
|
||||
setTimeout(() => (offerBox.hidden = true), 2500);
|
||||
});
|
||||
@@ -214,18 +312,23 @@
|
||||
offerBox.hidden = true;
|
||||
});
|
||||
offerBox.replaceChildren(
|
||||
el('div', { className: 'head', textContent: 'Custos' }),
|
||||
el('div', { className: 'note', textContent: `Save the login${offer.username ? ` of ${offer.username}` : ''} for ${offer.host}?` }),
|
||||
el('div', { className: 'head', textContent: 'CUSTOS' }),
|
||||
el('div', { className: 'note', textContent: offerText(offer) }),
|
||||
Object.assign(el('div', { className: 'row' }), {}),
|
||||
);
|
||||
offerBox.lastChild.append(no, yes);
|
||||
offerBox.hidden = false;
|
||||
}
|
||||
|
||||
/** A form is being sent: if it has a password the person typed (not one Custos filled), offer to save it. */
|
||||
/**
|
||||
* A form is being sent: if it has a password the person typed or Custos just made (not a saved one Custos filled),
|
||||
* offer to save it. On a sign-up or change-password form that is the new password, not the current one.
|
||||
*/
|
||||
function sent(form) {
|
||||
const scope = form ? [...form.querySelectorAll('input')] : [...document.querySelectorAll('input')];
|
||||
const pass = scope.find((i) => i.type === 'password' && visible(i) && i.value);
|
||||
const first = scope.find((i) => i.type === 'password' && visible(i)) ?? null;
|
||||
const fresh = first ? newPasswordFields(first).find((i) => i.value) : undefined;
|
||||
const pass = fresh ?? scope.find((i) => i.type === 'password' && visible(i) && i.value);
|
||||
if (!pass || filledByCustos.has(pass)) return;
|
||||
const user = usernameFor(pass);
|
||||
// Read once, handed to the background in memory, and not kept here.
|
||||
@@ -253,10 +356,25 @@
|
||||
// After the sign-in page went away: the next page of the same site shows the offer.
|
||||
if (window.top === window) void send({ kind: 'pending-save' }).then((r) => r?.ok && showOffer(r.offer));
|
||||
|
||||
// ---- the popup's choice, in the top frame: fill the page's login form, or only its 2FA field ----
|
||||
// ---- the popup's choices, in the top frame ----
|
||||
// `fill-now`: the page's login form, or only its 2FA field. `page-forms`: does the page take a new password?
|
||||
// `fill-new`: the new password the popup shows, into those fields, only if this is still the page it was made for.
|
||||
api.runtime.onMessage.addListener((msg, sender, reply) => {
|
||||
if (sender.id !== api.runtime.id || msg?.kind !== 'fill-now' || typeof msg.id !== 'string') return false;
|
||||
fill(msg.id, null).then((why) => reply({ ok: !why, error: why }));
|
||||
return true;
|
||||
if (sender.id !== api.runtime.id || sender.tab || window.top !== window) return false;
|
||||
if (msg?.kind === 'fill-now' && typeof msg.id === 'string') {
|
||||
fill(msg.id, null).then((why) => reply({ ok: !why, error: why }));
|
||||
return true;
|
||||
}
|
||||
if (msg?.kind === 'page-forms') {
|
||||
reply({ ok: true, newPassword: firstNewFields().length > 0 });
|
||||
return false;
|
||||
}
|
||||
if (msg?.kind === 'fill-new' && typeof msg.password === 'string' && msg.password && msg.origin === location.origin) {
|
||||
const fields = firstNewFields();
|
||||
if (fields.length) putNew(fields, msg.password);
|
||||
reply(fields.length ? { ok: true } : { ok: false, error: 'This page has no field for a new password.' });
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user