[extension] Fields: read autocomplete from the attribute (Gecko gives '' for one-time-code), a numeric 'code' field is a 2FA field; 0.22.1

This commit is contained in:
2026-12-03 01:56:49 +01:00
parent 077112492a
commit 4ca7df7043
3 changed files with 25 additions and 20 deletions
+11 -6
View File
@@ -31,11 +31,16 @@
const s = getComputedStyle(el);
return r.width > 0 && r.height > 0 && s.visibility !== 'hidden' && s.display !== 'none' && !el.disabled && !el.readOnly;
};
const isText = (el) => el instanceof HTMLInputElement && ['text', 'email', 'tel'].includes(el.type);
const isText = (el) => el instanceof HTMLInputElement && ['text', 'email', 'tel', 'number'].includes(el.type);
// The page's own autocomplete words: the attribute, not the property (Gecko gives '' for a word it does not know,
// such as one-time-code).
const auto = (el) => (el.getAttribute('autocomplete') ?? '').toLowerCase();
const isOtp = (el) =>
el instanceof HTMLInputElement &&
(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}`);
(auto(el).includes('one-time-code') ||
/(^|[^a-z])(otp|totp|2fa|mfa|one.?time|verification.?code)/i.test(`${el.name} ${el.id}`) ||
(el.inputMode === 'numeric' && /(^|[^a-z])code([^a-z]|$)/i.test(`${el.name} ${el.id}`)));
const isUserName = (el) => isText(el) && /username|email/i.test(`${auto(el)} ${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
@@ -46,10 +51,10 @@
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}`),
(el) => auto(el).includes('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 === 2 && pws.every((el) => !auto(el).includes('current-password'))) return pws;
if (pws.length === 3) return pws.slice(1);
return [];
}
@@ -66,7 +71,7 @@
/** 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);
const marked = inputs.find((el) => isText(el) && /username|email/i.test(el.autocomplete));
const marked = inputs.find((el) => isText(el) && /username|email/i.test(auto(el)));
if (marked) return marked;
for (let i = inputs.indexOf(pw) - 1; i >= 0; i--) if (isText(inputs[i]) && !isOtp(inputs[i])) return inputs[i];
return null;