Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update formdetector to detect more variations (#541)
Leendert de Borst738f93b88247
diff --git a/browser-extensions/chrome/src/contentScript.ts b/browser-extensions/chrome/src/contentScript.tsindex eb7e78dc5..ef0219169 100644--- a/browser-extensions/chrome/src/contentScript.ts+++ b/browser-extensions/chrome/src/contentScript.ts@@ -180,7 +180,7 @@ function createPopup(input: HTMLInputElement, credentials: Credential[]) : voidconst inputWidth = input.offsetWidth;// Set popup width to match input width, with min/max constraints- const popupWidth = Math.max(250, Math.min(960, inputWidth));+ const popupWidth = Math.max(250, Math.min(640, inputWidth));popup.style.cssText = `position: absolute;@@ -192,6 +192,7 @@ function createPopup(input: HTMLInputElement, credentials: Credential[]) : voidpadding: 8px 0;width: ${popupWidth}px;color: ${isDarkMode() ? '#f8f9fa' : '#000000'};+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;`;// Filter credentials based on current page context@@ -211,6 +212,7 @@ function createPopup(input: HTMLInputElement, credentials: Credential[]) : voiddisplay: flex;align-items: center;gap: 8px;+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;`;const imgElement = document.createElement('img');@@ -255,6 +257,7 @@ function createPopup(input: HTMLInputElement, credentials: Credential[]) : voidpadding: 8px 16px;color: ${isDarkMode() ? '#9ca3af' : '#6b7280'};font-style: italic;+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;`;noMatches.textContent = 'No matches found';popup.appendChild(noMatches);@@ -474,7 +477,7 @@ function createStatusPopup(input: HTMLInputElement, message: string): void {const inputWidth = input.offsetWidth;// Set popup width to match input width, with min/max constraints- const popupWidth = Math.max(250, Math.min(960, inputWidth));+ const popupWidth = Math.max(250, Math.min(640, inputWidth));popup.style.cssText = `position: absolute;@@ -488,6 +491,7 @@ function createStatusPopup(input: HTMLInputElement, message: string): void {color: ${isDarkMode() ? '#f8f9fa' : '#000000'};cursor: pointer;transition: background-color 0.2s;+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;`;// Add hover effect to the entire popup@@ -513,6 +517,7 @@ function createStatusPopup(input: HTMLInputElement, message: string): void {color: ${isDarkMode() ? '#d1d5db' : '#666'};font-size: 14px;padding-right: 32px;+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;`;messageElement.textContent = message;container.appendChild(messageElement);@@ -836,6 +841,7 @@ const createEditNamePopup = (defaultName: string): Promise<string | null> => {transform: scale(0.95);opacity: 0;transition: transform 0.2s ease, opacity 0.2s ease;+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important;`;popup.innerHTML = `diff --git a/browser-extensions/chrome/src/utils/form-detector/FormDetector.ts b/browser-extensions/chrome/src/utils/form-detector/FormDetector.tsindex 23b9aa364..10fa68f45 100644--- a/browser-extensions/chrome/src/utils/form-detector/FormDetector.ts+++ b/browser-extensions/chrome/src/utils/form-detector/FormDetector.ts@@ -41,17 +41,28 @@ export class FormDetector {*/public detectForms(): LoginForm[] {const forms: LoginForm[] = [];- const passwordFields = this.document.querySelectorAll<HTMLInputElement>('input[type="password"]');-- passwordFields.forEach(passwordField => {- const form = passwordField.closest('form');- // Find all the required fields- const emailField = this.findEmailField(passwordField);- const usernameField = this.findUsernameField(passwordField);- const passwordConfirmField = this.findPasswordConfirmField(passwordField);-- // Find identity fields+ // Find all input fields that could be part of a login form+ const passwordFields = this.document.querySelectorAll<HTMLInputElement>('input[type="password"]');+ const emailFields = this.document.querySelectorAll<HTMLInputElement>('input[type="email"], input[type="text"]');+ const textFields = this.document.querySelectorAll<HTMLInputElement>('input[type="text"]');++ // Create a Set to track processed forms to avoid duplicates+ const processedForms = new Set<HTMLFormElement | null>();++ // Helper to create a form entry+ const createFormEntry = (+ form: HTMLFormElement | null,+ emailField: HTMLInputElement | null,+ usernameField: HTMLInputElement | null,+ passwordField: HTMLInputElement | null+ ) => {+ // Skip if we've already processed this form+ if (form && processedForms.has(form)) return;+ processedForms.add(form);++ // Find additional fields+ const passwordConfirmField = passwordField ? this.findPasswordConfirmField(passwordField) : null;const firstNameField = this.findInputField(form, ['firstname', 'first-name', 'fname', 'voornaam'], ['text']);const lastNameField = this.findInputField(form, ['lastname', 'last-name', 'lname', 'achternaam'], ['text']);const birthdateField = this.findBirthdateFields(form);@@ -68,6 +79,40 @@ export class FormDetector {birthdateField,genderField});+ };++ // Process password fields first+ passwordFields.forEach(passwordField => {+ const form = passwordField.closest('form');+ const emailField = this.findEmailField(passwordField);+ const usernameField = this.findUsernameField(passwordField);+ createFormEntry(form, emailField, usernameField, passwordField);+ });++ // Process email fields that aren't already part of a processed form+ emailFields.forEach(field => {+ const form = field.closest('form');+ if (form && processedForms.has(form)) return;++ if (this.isLikelyEmailField(field)) {+ const passwordField = form ?+ form.querySelector<HTMLInputElement>('input[type="password"]') : null;+ const usernameField = this.findUsernameField(field);+ createFormEntry(form, field, usernameField, passwordField);+ }+ });++ // Process potential username fields that aren't already part of a processed form+ textFields.forEach(field => {+ const form = field.closest('form');+ if (form && processedForms.has(form)) return;++ if (this.isLikelyUsernameField(field)) {+ const passwordField = form ?+ form.querySelector<HTMLInputElement>('input[type="password"]') : null;+ const emailField = this.findEmailField(field);+ createFormEntry(form, emailField, field, passwordField);+ }});return forms;@@ -296,4 +341,36 @@ export class FormDetector {field: textField};}++ /**+ * Check if a field is likely an email field based on its attributes+ */+ private isLikelyEmailField(input: HTMLInputElement): boolean {+ const attributes = [+ input.type,+ input.id,+ input.name,+ input.className,+ input.placeholder+ ].map(attr => attr?.toLowerCase() || '');++ const patterns = ['email', 'e-mail', 'mail', 'address', '@'];+ return patterns.some(pattern => attributes.some(attr => attr.includes(pattern)));+ }++ /**+ * Check if a field is likely a username field based on its attributes+ */+ private isLikelyUsernameField(input: HTMLInputElement): boolean {+ const attributes = [+ input.type,+ input.id,+ input.name,+ input.className,+ input.placeholder+ ].map(attr => attr?.toLowerCase() || '');++ const patterns = ['user', 'username', 'login', 'identifier'];+ return patterns.some(pattern => attributes.some(attr => attr.includes(pattern)));+ }}\ No newline at end of filediff --git a/browser-extensions/chrome/src/utils/form-detector/__tests__/FormDetector.test.ts b/browser-extensions/chrome/src/utils/form-detector/__tests__/FormDetector.test.tsindex 6dc813cd6..d89fa393f 100644--- a/browser-extensions/chrome/src/utils/form-detector/__tests__/FormDetector.test.ts+++ b/browser-extensions/chrome/src/utils/form-detector/__tests__/FormDetector.test.ts@@ -111,4 +111,10 @@ describe('FormDetector', () => {testField(FormField.Email, 'login', htmlFile);testField(FormField.Password, 'password', htmlFile);});++ describe('English email form 1 detection', () => {+ const htmlFile = 'en-email-form1.html';++ testField(FormField.Email, 'P0-0', htmlFile);+ });});\ No newline at end of filediff --git a/browser-extensions/chrome/src/utils/form-detector/__tests__/test-forms/en-email-form1.html b/browser-extensions/chrome/src/utils/form-detector/__tests__/test-forms/en-email-form1.htmlnew file mode 100644index 000000000..877b4399e--- /dev/null+++ b/browser-extensions/chrome/src/utils/form-detector/__tests__/test-forms/en-email-form1.html@@ -0,0 +1,15 @@+<main class="css-qg5f0b"><header><h1 class="css-1tfp4pj">Create your account</h1></header><section class="css-t9ru4u"><form method="post" action="/register?componentEventParams=componentType%3Didentityauthentication%26componentId%3Dguardian_signin_header&returnUrl" data-testid="main-form" class="css-1pw6os9"><div id="recaptcha" class="g-recaptcha css-wyp7vc"><div class="grecaptcha-badge" data-style="bottomright" style="width: 256px; height: 60px; display: block; transition: right 0.3s; position: fixed; bottom: 14px; right: -186px; box-shadow: gray 0px 0px 5px; border-radius: 2px; overflow: hidden;"><div class="grecaptcha-logo"><iframe title="reCAPTCHA" width="256" height="60" role="presentation" name="a-8x9lsyn7q548" frameborder="0" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox allow-storage-access-by-user-activation" src="https://www.google.com/recaptcha/api2/anchor?ar=1&k=6LdWnfoUAAAAAKAE3PTWGLw8m_lV4PFqm2pft3DQ&co=aHR0cHM6Ly9wcm9maWxlLnRoZWd1YXJkaWFuLmNvbTo0NDM.&hl=en&v=I0bG74fWAenNf3Z5ncHSz-bd&size=invisible&cb=8qr7nqn2v8zy"></iframe></div><div class="grecaptcha-error"></div><textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"></textarea></div><iframe style="display: none;"></iframe></div><input type="hidden" name="_csrf" value="JQefOI4bd9Rd91s8TaOBEacBoqnZ56ht_sgm6T6wOMSdu0hx0eluksj9fviOzBzmQUcBotU4NKcfGxuQ2cVIfw"><input type="hidden" name="_csrfPageUrl"><input type="hidden" name="ref" value="https://profile.theguardian.com/register/email"><input type="hidden" name="refViewId" value="m6qgqwadrjxge8ow8pnc"><div><label for="P0-0" class="css-0"><div class="css-1daa38r">Email </div></label><div style="position: relative; display: inline-block; width: 100%;"><input type="email" id="P0-0" aria-required="true" aria-invalid="false" aria-describedby="" required="" name="email" autocomplete="off" class="css-l9vrwo" style="width: 428px; display: inline-block;"><div class="aliasvault-input-icon" style="+ display: flex;+ align-items: center;+ justify-content: center;+ position: absolute;+ right: 8px;+ top: 8px;+ margin: auto;+ cursor: pointer;+ width: 20px;+ height: 20px;+ z-index: 999999;+">+ <img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCA1MDAgNTAwIiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9IjAgMCA1MDAgNTAwIiB4bWw6c3BhY2U9InByZXNlcnZlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8c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LjU5MS03LjE2NTEgMjkuMDY0LTIxLjY2NSAyOC4xNjEtOC41MjU0LTAuNTMwODgtMTcuMjAyLTYuNTA5NC0xOS4xOTgtMTUuODMxeiIgZmlsbD0iI0VFQzE3MCIvPgo8cGF0aCBkPSJtMzM2LjkxIDMzMy40MWMtOS4wMTc1LTQuMjQ5MS0xNS4zMzctMTQuMzQ5LTEzLjgyOS0yMS42ODIgMy4wODI1LTE0Ljk4OSAxMy4zNDEtMjAuMzA0IDIzLjAxOC0xOS41ODUgMTAuNjUzIDAuNzkxNDEgMTcuOTMgNy40MDcgMTkuNzY1IDE3LjU0NyAxLjk1ODggMTAuODI0LTQuMTE3MSAxOS45MzktMTMuNDk0IDIzLjcwMy01LjI3MiAyLjExNjItMTAuMDkxIDEuNTA4Ni0xNS40NiAwLjAxNzg4M3oiIGZpbGw9IiNFRUMxNzAiLz4KPC9zdmc+" style="width: 100%; height: 100%;">+</div></div></div><div class="css-sh1xpo"><label id="6031_description" class="css-141740"><div class="css-wqo2kt"><span class="css-1yorkrx">Saturday Edition newsletter</span><span class="css-o3hxza">An exclusive email highlighting the week’s best Guardian journalism from the editor-in-chief, Katharine Viner.</span></div><input name="6031" type="checkbox" role="switch" checked="" aria-labelledby="6031_description" class="css-1ksnltd"><span aria-hidden="true" aria-labelledby="6031_description" class="css-14xm7ey"></span></label><label id="similar_guardian_products_description" class="css-141740"><div class="css-wqo2kt"><span class="css-o3hxza">Receive information on our products and ways to support and enjoy our journalism. Toggle to opt out.</span></div><input name="similar_guardian_products" type="checkbox" role="switch" checked="" aria-labelledby="similar_guardian_products_description" class="css-1ksnltd"><span aria-hidden="true" aria-labelledby="similar_guardian_products_description" class="css-14xm7ey"></span></label></div><div class="css-gsjv6q"><div class="css-h996v6">Newsletters may contain information about Guardian products, services and chosen charities or online advertisements.</div><div class="css-h996v6">This service is protected by reCAPTCHA and the Google <a href="https://policies.google.com/privacy" rel="noopener noreferrer" class="css-1ss633q">privacy policy</a> and <a href="https://policies.google.com/terms" rel="noopener noreferrer" class="css-1ss633q">terms of service</a> apply.</div></div><button type="submit" aria-live="polite" data-cy="main-form-submit-button" aria-disabled="false" class="css-1aiqjx3">Next</button></form><hr class="css-5bcdcv"><p class="css-jckhu">Already have an account? <a href="/signin?componentEventParams=componentType%3Didentityauthentication%26componentId%3Dguardian_signin_header&returnUrl=https%3A%2F%2Fwww.theguardian.com%2Feurope" class="css-1ss633q">Sign in</a></p></section></main>\ No newline at end of file