Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add common translations for username, password and email fields to browser extension formDetector logic (#1788)

Leendert de Borste6fe4bc86862
diff --git a/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts b/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts
index f1efad5cb..22b0cab48 100644
--- a/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts
+++ b/apps/browser-extension/src/utils/formDetector/FieldPatterns.ts
@@ -234,11 +234,20 @@ export const DutchStopWords = new Set([
'je', 'in', 'op', 'de', 'van', 'ons', 'allemaal'
]);
+/*
+ * Import translation-based patterns dynamically
+ * These patterns are extracted from all i18n translation files
+ */
+import { TranslationEmailPatterns, TranslationUsernamePatterns, TranslationPasswordPatterns } from './TranslationPatterns';
+
/**
* Combined field patterns which includes all supported languages.
+ * This includes:
+ * - Hardcoded English and Dutch patterns
+ * - Translation-based patterns from all supported languages
*/
export const CombinedFieldPatterns: FieldPatterns = {
- username: [...new Set([...EnglishFieldPatterns.username, ...DutchFieldPatterns.username])],
+ username: [...new Set([...EnglishFieldPatterns.username, ...DutchFieldPatterns.username, ...TranslationUsernamePatterns])],
fullName: [...new Set([...EnglishFieldPatterns.fullName, ...DutchFieldPatterns.fullName])],
firstName: [...new Set([...EnglishFieldPatterns.firstName, ...DutchFieldPatterns.firstName])],
lastName: [...new Set([...EnglishFieldPatterns.lastName, ...DutchFieldPatterns.lastName])],
@@ -247,10 +256,12 @@ export const CombinedFieldPatterns: FieldPatterns = {
* the nl-registration-form5.html honeypot field is named. The order of the patterns
* determine which field is detected. If a pattern entry with higher index is detected, that
* field will be selected instead of the lower index one.
+ *
+ * Translation patterns are added last to catch all language variations (e.g., "E-post" in Swedish)
*/
- email: [...new Set([...DutchFieldPatterns.email, ...EnglishFieldPatterns.email])],
+ email: [...new Set([...DutchFieldPatterns.email, ...EnglishFieldPatterns.email, ...TranslationEmailPatterns])],
emailConfirm: [...new Set([...EnglishFieldPatterns.emailConfirm, ...DutchFieldPatterns.emailConfirm])],
- password: [...new Set([...EnglishFieldPatterns.password, ...DutchFieldPatterns.password])],
+ password: [...new Set([...EnglishFieldPatterns.password, ...DutchFieldPatterns.password, ...TranslationPasswordPatterns])],
birthdate: [...new Set([...EnglishFieldPatterns.birthdate, ...DutchFieldPatterns.birthdate])],
gender: [...new Set([...EnglishFieldPatterns.gender, ...DutchFieldPatterns.gender])],
birthDateDay: [...new Set([...EnglishFieldPatterns.birthDateDay, ...DutchFieldPatterns.birthDateDay])],
diff --git a/apps/browser-extension/src/utils/formDetector/TranslationPatterns.ts b/apps/browser-extension/src/utils/formDetector/TranslationPatterns.ts
new file mode 100644
index 000000000..d92f9b93b
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/TranslationPatterns.ts
@@ -0,0 +1,87 @@
+/**
+ * This module extracts field detection patterns from all available translation files.
+ * It looks at common.email, common.username, and common.password translations
+ * across all supported languages to help detect form fields in any language.
+ *
+ * This module dynamically imports all translation files, so adding a new language
+ * automatically extends form detection support without any code changes.
+ */
+
+/**
+ * Dynamically import all translation JSON files from the locales directory
+ */
+const translationModules = import.meta.glob('../../i18n/locales/*.json', { eager: true });
+
+/**
+ * Extract all translation objects from the imported modules
+ */
+const allTranslations = Object.values(translationModules).map((module: unknown) => {
+ return (module as { default: unknown }).default;
+});
+
+/**
+ * Extract unique, lowercase field patterns from all translations
+ * for a given key path (e.g., 'common.email')
+ */
+function extractPatternsFromTranslations(keyPath: string): string[] {
+ const patterns = new Set<string>();
+
+ for (const translation of allTranslations) {
+ const value = getNestedValue(translation, keyPath);
+ if (value && typeof value === 'string') {
+ // Normalize and split multi-word translations
+ const normalized = value.toLowerCase().trim();
+
+ // Add the full phrase
+ patterns.add(normalized);
+ }
+ }
+
+ return Array.from(patterns);
+}
+
+/**
+ * Get a nested value from an object using dot notation
+ */
+function getNestedValue(obj: unknown, path: string): unknown {
+ const keys = path.split('.');
+ let current: unknown = obj;
+
+ for (const key of keys) {
+ if (current && typeof current === 'object' && key in current) {
+ current = (current as Record<string, unknown>)[key];
+ } else {
+ return undefined;
+ }
+ }
+
+ return current;
+}
+
+/**
+ * Email patterns extracted from all translation files
+ * These are combined with the existing email patterns for better detection
+ */
+export const TranslationEmailPatterns: string[] = extractPatternsFromTranslations('common.email');
+
+/**
+ * Username patterns extracted from all translation files
+ * These are combined with the existing username patterns for better detection
+ */
+export const TranslationUsernamePatterns: string[] = extractPatternsFromTranslations('common.username');
+
+/**
+ * Password patterns extracted from all translation files
+ * These are combined with the existing password patterns for better detection
+ */
+export const TranslationPasswordPatterns: string[] = extractPatternsFromTranslations('common.password');
+
+/**
+ * Combined patterns that include both translation-based and hardcoded patterns
+ * This ensures we catch fields in all supported languages
+ */
+export const AllLanguagePatterns = {
+ email: TranslationEmailPatterns,
+ username: TranslationUsernamePatterns,
+ password: TranslationPasswordPatterns
+};
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.emby-connect-sv.test.ts b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.emby-connect-sv.test.ts
new file mode 100644
index 000000000..cd4d6e701
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/FormDetector.emby-connect-sv.test.ts
@@ -0,0 +1,95 @@
+import { describe, it, expect } from 'vitest';
+
+import { FormDetector } from '../FormDetector';
+
+import { createTestDom } from './TestUtils';
+
+describe('FormDetector - Emby Connect login form (Swedish)', () => {
+ const htmlFile = 'emby-connect-login-sv.html';
+
+ it('should detect login form with Swedish labels', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+ const formDetector = new FormDetector(document);
+ expect(formDetector.containsLoginForm()).toBe(true);
+ });
+
+ it('should detect Swedish "E-post" label as email field', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ // Click on the email field with Swedish label
+ const emailInput = document.getElementById('embyinput0');
+ const formDetector = new FormDetector(document, emailInput as HTMLElement);
+ const form = formDetector.getForm();
+
+ expect(form).toBeTruthy();
+
+ // Should detect the email field based on Swedish "E-post" label
+ expect(form?.emailField).toBeTruthy();
+ expect(form?.emailField?.id).toBe('embyinput0');
+ expect(form?.emailField?.className).toContain('txtUser');
+ });
+
+ it('should detect Swedish "Lösenord" label as password field', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ // Click on the password field with Swedish label
+ const passwordInput = document.getElementById('embyinput1');
+ const formDetector = new FormDetector(document, passwordInput as HTMLElement);
+ const form = formDetector.getForm();
+
+ expect(form).toBeTruthy();
+
+ // Should detect the password field based on Swedish "Lösenord" label
+ expect(form?.passwordField).toBeTruthy();
+ expect(form?.passwordField?.id).toBe('embyinput1');
+ expect(form?.passwordField?.className).toContain('txtPassword');
+ });
+
+ it('should ignore hidden fake fields in Swedish form', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ const emailInput = document.getElementById('embyinput0');
+ const formDetector = new FormDetector(document, emailInput as HTMLElement);
+ const form = formDetector.getForm();
+
+ expect(form).toBeTruthy();
+
+ // Should not detect fake fields
+ expect(form?.emailField?.name).not.toBe('fakeusernameremembered');
+ expect(form?.passwordField?.name).not.toBe('fakepasswordremembered');
+ });
+
+ it('should not trigger autofill on fake Swedish form fields', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ const fakeField = document.querySelector('input[name="fakeusernameremembered"]');
+ const formDetector = new FormDetector(document, fakeField as HTMLElement);
+
+ expect(formDetector.isAutofillTriggerableField()).toBe(false);
+ });
+
+ it('should trigger autofill on real Swedish email field', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ const emailInput = document.getElementById('embyinput0');
+ const formDetector = new FormDetector(document, emailInput as HTMLElement);
+
+ expect(formDetector.isAutofillTriggerableField()).toBe(true);
+ });
+
+ it('should trigger autofill on real Swedish password field', () => {
+ const dom = createTestDom(htmlFile);
+ const document = dom.window.document;
+
+ const passwordInput = document.getElementById('embyinput1');
+ const formDetector = new FormDetector(document, passwordInput as HTMLElement);
+
+ expect(formDetector.isAutofillTriggerableField()).toBe(true);
+ });
+});
diff --git a/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/emby-connect-login-sv.html b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/emby-connect-login-sv.html
new file mode 100644
index 000000000..8899322db
--- /dev/null
+++ b/apps/browser-extension/src/utils/formDetector/__tests__/test-forms/emby-connect-login-sv.html
@@ -0,0 +1,21 @@
+<form class="manualLoginForm">
+
+ <div style="height:0; overflow: hidden;"><input type="text" name="fakeusernameremembered" tabindex="-1"><input type="password" name="fakepasswordremembered" tabindex="-1"></div>
+
+ <div class="inputContainer">
+ <label class="inputLabel" for="embyinput0">E-post</label><input is="emby-input" type="text" class="txtUser emby-input" required="required" label="E-post" id="embyinput0" autocomplete="off" data-av-field-type="username">
+ <div class="fieldDescription">Det här är e-postadressen som du använde för att konfigurera ditt Emby Connect-konto.</div>
+ </div>
+ <div class="inputContainer">
+ <label class="inputLabel" for="embyinput1">Lösenord</label><input is="emby-input" type="password" class="txtPassword emby-input" required="required" label="Lösenord" id="embyinput1">
+ </div>
+ <button is="emby-button" type="submit" class="raised accent block emby-button emby-button-focusscale">
+ <span>Logga in</span>
+ </button>
+
+ <a is="emby-linkbutton" class="raised cancel block btnForgotPassword emby-button emby-button-focusscale" target="_blank" href="https://emby.media/community/index.php?/lostpassword" data-autohide="true">
+ <span>Glömt lösenord?</span>
+ </a>
+
+ <div class="terms hide fieldDescription" style="margin-top: 2em; color: #ccc;">Emby är utformat för att hjälpa dig att hantera ditt personliga mediebibliotek, till exempel hemvideor och foton. Se våra <a is="emby-linkbutton" class="lnkTerms button-link emby-button emby-button-focusscale" href="https://emby.media/terms" target="_blank">användarvillkor</a>. Användning av Emby-programvara utgör godkännande av dessa villkor.</div>
+</form>