Skip to content

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

Refactor birthdate and gender field fill methods (#638)

Leendert de Borstb2093b5892e5
diff --git a/browser-extensions/chrome/src/shared/formDetector/FormFiller.ts b/browser-extensions/chrome/src/shared/formDetector/FormFiller.ts
index 9ab114598..7ac8182dc 100644
--- a/browser-extensions/chrome/src/shared/formDetector/FormFiller.ts
+++ b/browser-extensions/chrome/src/shared/formDetector/FormFiller.ts
@@ -75,101 +75,145 @@ export class FormFiller {
* @param credential The credential to fill the form with.
*/
private fillBirthdateFields(credential: Credential): void {
- // Handle birthdate with input events
+ if (!credential.Alias.BirthDate) {
+ return;
+ }
+
+ const birthDate = new Date(credential.Alias.BirthDate);
+
if (this.form.birthdateField.single) {
- if (credential.Alias.BirthDate) {
- const birthDate = new Date(credential.Alias.BirthDate);
- const day = birthDate.getDate().toString().padStart(2, '0');
- const month = (birthDate.getMonth() + 1).toString().padStart(2, '0');
- const year = birthDate.getFullYear().toString();
-
- let formattedDate = '';
- switch (this.form.birthdateField.format) {
- case 'dd/mm/yyyy':
- formattedDate = `${day}/${month}/${year}`;
- break;
- case 'mm/dd/yyyy':
- formattedDate = `${month}/${day}/${year}`;
- break;
- case 'dd-mm-yyyy':
- formattedDate = `${day}-${month}-${year}`;
- break;
- case 'mm-dd-yyyy':
- formattedDate = `${month}-${day}-${year}`;
- break;
- case 'yyyy-mm-dd':
- default:
- formattedDate = `${year}-${month}-${day}`;
- break;
- }
+ this.fillSingleBirthdateField(birthDate);
+ } else {
+ this.fillSeparateBirthdateFields(birthDate);
+ }
+ }
- this.form.birthdateField.single.value = formattedDate;
- this.triggerInputEvents(this.form.birthdateField.single);
- }
- } else if (credential.Alias.BirthDate) {
- const birthDate = new Date(credential.Alias.BirthDate);
-
- if (this.form.birthdateField.day) {
- const dayElement = this.form.birthdateField.day as HTMLSelectElement | HTMLInputElement;
- if ('options' in dayElement && dayElement.options) {
- const dayValue = birthDate.getDate().toString().padStart(2, '0');
- const dayOption = Array.from(dayElement.options).find(opt =>
- opt.value === dayValue ||
- opt.value === birthDate.getDate().toString() ||
- opt.text === dayValue ||
- opt.text === birthDate.getDate().toString()
- );
- if (dayOption) {
- dayElement.value = dayOption.value;
- }
- } else {
- dayElement.value = birthDate.getDate().toString().padStart(2, '0');
- }
- this.triggerInputEvents(dayElement);
- }
+ /**
+ * Fill the single birthdate field.
+ * @param birthDate The birthdate to fill the form with.
+ */
+ private fillSingleBirthdateField(birthDate: Date): void {
+ const day = birthDate.getDate().toString().padStart(2, '0');
+ const month = (birthDate.getMonth() + 1).toString().padStart(2, '0');
+ const year = birthDate.getFullYear().toString();
- if (this.form.birthdateField.month) {
- const monthElement = this.form.birthdateField.month as HTMLSelectElement | HTMLInputElement;
- if ('options' in monthElement && monthElement.options) {
- // Loop through all supported languages and find a match for the month name.
- CombinedDateOptionPatterns.months.forEach((monthNames, _) => {
- const monthValue = (birthDate.getMonth() + 1).toString().padStart(2, '0');
- const monthOption = Array.from(monthElement.options).find(opt =>
- opt.value === monthValue ||
- opt.value === (birthDate.getMonth() + 1).toString() ||
- opt.text === monthValue ||
- opt.text === (birthDate.getMonth() + 1).toString() ||
- opt.text.toLowerCase() === monthNames[birthDate.getMonth()].toLowerCase() ||
- opt.text.toLowerCase() === monthNames[birthDate.getMonth()].substring(0, 3).toLowerCase()
- );
- if (monthOption) {
- monthElement.value = monthOption.value;
- }
- });
- } else {
- monthElement.value = (birthDate.getMonth() + 1).toString().padStart(2, '0');
- }
- this.triggerInputEvents(monthElement);
+ const formattedDate = this.formatDateString(day, month, year);
+ this.form.birthdateField.single!.value = formattedDate;
+ this.triggerInputEvents(this.form.birthdateField.single!);
+ }
+
+ /**
+ * Format the date string based on the format of the birthdate field.
+ * @param day The day of the birthdate.
+ * @param month The month of the birthdate.
+ * @param year The year of the birthdate.
+ * @returns The formatted date string.
+ */
+ private formatDateString(day: string, month: string, year: string): string {
+ switch (this.form.birthdateField.format) {
+ case 'dd/mm/yyyy': return `${day}/${month}/${year}`;
+ case 'mm/dd/yyyy': return `${month}/${day}/${year}`;
+ case 'dd-mm-yyyy': return `${day}-${month}-${year}`;
+ case 'mm-dd-yyyy': return `${month}-${day}-${year}`;
+ case 'yyyy-mm-dd':
+ default: return `${year}-${month}-${day}`;
+ }
+ }
+
+ /**
+ * Fill the separate birthdate fields.
+ * @param birthDate The birthdate to fill the form with.
+ */
+ private fillSeparateBirthdateFields(birthDate: Date): void {
+ this.fillDayField(birthDate);
+ this.fillMonthField(birthDate);
+ this.fillYearField(birthDate);
+ }
+
+ /**
+ * Fill the day field.
+ * @param birthDate The birthdate to fill the form with.
+ */
+ private fillDayField(birthDate: Date): void {
+ if (!this.form.birthdateField.day) {
+ return;
+ }
+
+ const dayElement = this.form.birthdateField.day as HTMLSelectElement | HTMLInputElement;
+ const dayValue = birthDate.getDate().toString().padStart(2, '0');
+
+ if ('options' in dayElement && dayElement.options) {
+ const dayOption = Array.from(dayElement.options).find(opt =>
+ opt.value === dayValue ||
+ opt.value === birthDate.getDate().toString() ||
+ opt.text === dayValue ||
+ opt.text === birthDate.getDate().toString()
+ );
+ if (dayOption) {
+ dayElement.value = dayOption.value;
}
+ } else {
+ dayElement.value = dayValue;
+ }
+ this.triggerInputEvents(dayElement);
+ }
+
+ /**
+ * Fill the month field.
+ * @param birthDate The birthdate to fill the form with.
+ */
+ private fillMonthField(birthDate: Date): void {
+ if (!this.form.birthdateField.month) {
+ return;
+ }
- if (this.form.birthdateField.year) {
- const yearElement = this.form.birthdateField.year as HTMLSelectElement | HTMLInputElement;
- if ('options' in yearElement && yearElement.options) {
- const yearValue = birthDate.getFullYear().toString();
- const yearOption = Array.from(yearElement.options).find(opt =>
- opt.value === yearValue ||
- opt.text === yearValue
- );
- if (yearOption) {
- yearElement.value = yearOption.value;
- }
- } else {
- yearElement.value = birthDate.getFullYear().toString();
+ const monthElement = this.form.birthdateField.month as HTMLSelectElement | HTMLInputElement;
+ const monthValue = (birthDate.getMonth() + 1).toString().padStart(2, '0');
+
+ if ('options' in monthElement && monthElement.options) {
+ CombinedDateOptionPatterns.months.forEach(monthNames => {
+ const monthOption = Array.from(monthElement.options).find(opt =>
+ opt.value === monthValue ||
+ opt.value === (birthDate.getMonth() + 1).toString() ||
+ opt.text === monthValue ||
+ opt.text === (birthDate.getMonth() + 1).toString() ||
+ opt.text.toLowerCase() === monthNames[birthDate.getMonth()].toLowerCase() ||
+ opt.text.toLowerCase() === monthNames[birthDate.getMonth()].substring(0, 3).toLowerCase()
+ );
+ if (monthOption) {
+ monthElement.value = monthOption.value;
}
+ });
+ } else {
+ monthElement.value = monthValue;
+ }
+ this.triggerInputEvents(monthElement);
+ }
- this.triggerInputEvents(yearElement);
+ /**
+ * Fill the year field.
+ * @param birthDate The birthdate to fill the form with.
+ */
+ private fillYearField(birthDate: Date): void {
+ if (!this.form.birthdateField.year) {
+ return;
+ }
+
+ const yearElement = this.form.birthdateField.year as HTMLSelectElement | HTMLInputElement;
+ const yearValue = birthDate.getFullYear().toString();
+
+ if ('options' in yearElement && yearElement.options) {
+ const yearOption = Array.from(yearElement.options).find(opt =>
+ opt.value === yearValue ||
+ opt.text === yearValue
+ );
+ if (yearOption) {
+ yearElement.value = yearOption.value;
}
+ } else {
+ yearElement.value = yearValue;
}
+ this.triggerInputEvents(yearElement);
}
/**
@@ -177,66 +221,84 @@ export class FormFiller {
* @param credential The credential to fill the form with.
*/
private fillGenderFields(credential: Credential): void {
- // Handle gender with input events
switch (this.form.genderField.type) {
case 'select':
- if (this.form.genderField.field) {
- const maleValues = CombinedGenderOptionPatterns.male;
- const femaleValues = CombinedGenderOptionPatterns.female;
-
- const selectElement = this.form.genderField.field as HTMLSelectElement;
- const options = Array.from(selectElement.options);
-
- if (credential.Alias.Gender === Gender.Male) {
- const maleOption = options.find(opt =>
- maleValues.includes(opt.value.toLowerCase()) ||
- maleValues.includes(opt.text.toLowerCase())
- );
- if (maleOption) {
- selectElement.value = maleOption.value;
- }
- } else if (credential.Alias.Gender === Gender.Female) {
- const femaleOption = options.find(opt =>
- femaleValues.includes(opt.value.toLowerCase()) ||
- femaleValues.includes(opt.text.toLowerCase())
- );
- if (femaleOption) {
- selectElement.value = femaleOption.value;
- }
- }
-
- this.triggerInputEvents(selectElement);
- }
+ this.fillGenderSelect(credential.Alias.Gender);
break;
- case 'radio': {
- const radioButtons = this.form.genderField.radioButtons;
- if (!radioButtons) {
- break;
- }
-
- let selectedRadio: HTMLInputElement | null = null;
- if (credential.Alias.Gender === Gender.Male && radioButtons.male) {
- radioButtons.male.checked = true;
- selectedRadio = radioButtons.male;
- } else if (credential.Alias.Gender === Gender.Female && radioButtons.female) {
- radioButtons.female.checked = true;
- selectedRadio = radioButtons.female;
- } else if (credential.Alias.Gender === Gender.Other && radioButtons.other) {
- radioButtons.other.checked = true;
- selectedRadio = radioButtons.other;
- }
-
- if (selectedRadio) {
- this.triggerInputEvents(selectedRadio);
- }
+ case 'radio':
+ this.fillGenderRadio(credential.Alias.Gender);
break;
- }
case 'text':
- if (this.form.genderField.field && credential.Alias.Gender) {
- (this.form.genderField.field as HTMLInputElement).value = credential.Alias.Gender;
- this.triggerInputEvents(this.form.genderField.field as HTMLInputElement);
- }
+ this.fillGenderText(credential.Alias.Gender);
break;
}
}
+
+ /**
+ * Fill the gender select field.
+ * @param gender The gender to fill the form with.
+ */
+ private fillGenderSelect(gender: Gender | undefined): void {
+ if (!this.form.genderField.field || !gender) {
+ return;
+ }
+
+ const selectElement = this.form.genderField.field as HTMLSelectElement;
+ const options = Array.from(selectElement.options);
+ const genderValues = gender === Gender.Male
+ ? CombinedGenderOptionPatterns.male
+ : CombinedGenderOptionPatterns.female;
+
+ const genderOption = options.find(opt =>
+ genderValues.includes(opt.value.toLowerCase()) ||
+ genderValues.includes(opt.text.toLowerCase())
+ );
+
+ if (genderOption) {
+ selectElement.value = genderOption.value;
+ this.triggerInputEvents(selectElement);
+ }
+ }
+
+ /**
+ * Fill the gender radio fields.
+ * @param gender The gender to fill the form with.
+ */
+ private fillGenderRadio(gender: Gender | undefined): void {
+ const radioButtons = this.form.genderField.radioButtons;
+ if (!radioButtons || !gender) {
+ return;
+ }
+
+ let selectedRadio: HTMLInputElement | null = null;
+
+ if (gender === Gender.Male && radioButtons.male) {
+ radioButtons.male.checked = true;
+ selectedRadio = radioButtons.male;
+ } else if (gender === Gender.Female && radioButtons.female) {
+ radioButtons.female.checked = true;
+ selectedRadio = radioButtons.female;
+ } else if (gender === Gender.Other && radioButtons.other) {
+ radioButtons.other.checked = true;
+ selectedRadio = radioButtons.other;
+ }
+
+ if (selectedRadio) {
+ this.triggerInputEvents(selectedRadio);
+ }
+ }
+
+ /**
+ * Fill the gender text field.
+ * @param gender The gender to fill the form with.
+ */
+ private fillGenderText(gender: Gender | undefined): void {
+ if (!this.form.genderField.field || !gender) {
+ return;
+ }
+
+ const inputElement = this.form.genderField.field as HTMLInputElement;
+ inputElement.value = gender;
+ this.triggerInputEvents(inputElement);
+ }
}
diff --git a/browser-extensions/chrome/src/shared/types/Credential.ts b/browser-extensions/chrome/src/shared/types/Credential.ts
index 03c19536a..c495f8685 100644
--- a/browser-extensions/chrome/src/shared/types/Credential.ts
+++ b/browser-extensions/chrome/src/shared/types/Credential.ts
@@ -1,3 +1,5 @@
+import { Gender } from "../generators/Identity/types/Gender";
+
/**
* Credential SQLite database type.
*/
@@ -15,7 +17,7 @@ export type Credential = {
LastName: string;
NickName?: string;
BirthDate: string;
- Gender?: string;
+ Gender?: Gender;
Email?: string;
};
}
\ No newline at end of file