Skip to content

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

Remove autocomplete=off check and attach autofill popup shadowroot to input itself (#756)

Leendert de Borst60ba96cb862b
diff --git a/browser-extension/src/entrypoints/content.ts b/browser-extension/src/entrypoints/content.ts
index 1fbf92f22..4654f02f6 100644
--- a/browser-extension/src/entrypoints/content.ts
+++ b/browser-extension/src/entrypoints/content.ts
@@ -22,88 +22,116 @@ export default defineContentScript({
return;
}
- // Create a shadow root UI for isolation
- const ui = await createShadowRootUi(ctx, {
- name: 'aliasvault-ui',
- position: 'inline',
- anchor: 'body',
- /**
- * Handle mount.
- */
- onMount(container) {
- /**
- * Handle input field focus.
- */
- const handleFocusIn = async (e: FocusEvent) : Promise<void> => {
- if (ctx.isInvalid) {
- return;
+ let ui: Awaited<ReturnType<typeof createShadowRootUi>> | null = null;
+
+ /**
+ * Handle input field focus.
+ */
+ const handleFocusIn = async (e: FocusEvent) : Promise<void> => {
+ if (ctx.isInvalid) {
+ return;
+ }
+
+ // Check if element itself, html or body has av-disable attribute like av-disable="true"
+ const avDisable = (e.target as HTMLElement).getAttribute('av-disable') ?? document.body?.getAttribute('av-disable') ?? document.documentElement.getAttribute('av-disable');
+ if (avDisable === 'true') {
+ return;
+ }
+
+ const target = e.target as HTMLInputElement;
+ const textInputTypes = ['text', 'email', 'tel', 'password', 'search', 'url'];
+
+ if (target.tagName === 'INPUT' && textInputTypes.includes(target.type) && !target.dataset.aliasvaultIgnore) {
+ const formDetector = new FormDetector(document, target);
+ if (!formDetector.containsLoginForm()) {
+ return;
+ }
+
+ // Create shadow root UI only if it doesn't exist
+ if (!ui) {
+ ui = await createShadowRootUi(ctx, {
+ name: 'aliasvault-ui',
+ position: 'inline',
+ anchor: target,
+ /**
+ * Inject the icon and potentially show popup.
+ *
+ * @param container - The container element.
+ */
+ async onMount(container) {
+ injectIcon(target, container);
+
+ // Only show popup if its enabled and debounce time has passed.
+ if (await isAutoShowPopupEnabled() && popupDebounceTimeHasPassed()) {
+ openAutofillPopup(target, container);
+ }
+ },
+ });
+ } else {
+ // If UI exists, just inject the icon and potentially show popup
+ injectIcon(target, ui.uiContainer);
+ if (await isAutoShowPopupEnabled() && popupDebounceTimeHasPassed()) {
+ openAutofillPopup(target, ui.uiContainer);
}
+ }
+ }
+ };
- // Check if element itself, html or body has av-disable attribute like av-disable="true"
- const avDisable = (e.target as HTMLElement).getAttribute('av-disable') ?? document.body?.getAttribute('av-disable') ?? document.documentElement.getAttribute('av-disable');
- if (avDisable === 'true') {
- return;
- }
+ // Listen for input field focus in the main document
+ document.addEventListener('focusin', handleFocusIn);
- const target = e.target as HTMLInputElement;
- const textInputTypes = ['text', 'email', 'tel', 'password', 'search', 'url'];
+ // Listen for popstate events (back/forward navigation)
+ window.addEventListener('popstate', () => {
+ if (ctx.isInvalid || !ui) {
+ return;
+ }
- if (target.tagName === 'INPUT' && textInputTypes.includes(target.type) && !target.dataset.aliasvaultIgnore) {
- const formDetector = new FormDetector(document, target);
- if (!formDetector.containsLoginForm()) {
- return;
- }
+ removeExistingPopup(ui.uiContainer);
+ });
+ // Listen for messages from the background script
+ onMessage('OPEN_AUTOFILL_POPUP', async (message: { data: { elementIdentifier: string } }) : Promise<messageBoolResponse> => {
+ const { data } = message;
+ const { elementIdentifier } = data;
+
+ if (!elementIdentifier) {
+ return { success: false, error: 'No element identifier provided' };
+ }
+
+ const target = document.getElementById(elementIdentifier) ?? document.getElementsByName(elementIdentifier)[0];
+
+ if (!(target instanceof HTMLInputElement)) {
+ return { success: false, error: 'Target element is not an input field' };
+ }
+
+ const formDetector = new FormDetector(document, target);
+
+ if (!formDetector.containsLoginForm()) {
+ return { success: false, error: 'No form found' };
+ }
+
+ // Create shadow root UI if it doesn't exist
+ if (!ui) {
+ ui = await createShadowRootUi(ctx, {
+ name: 'aliasvault-ui',
+ position: 'inline',
+ anchor: target,
+ /**
+ * Inject the icon and show popup.
+ *
+ * @param container - The container element.
+ */
+ async onMount(container) {
injectIcon(target, container);
-
- // Only show popup if its enabled and debounce time has passed.
- if (await isAutoShowPopupEnabled() && popupDebounceTimeHasPassed()) {
- openAutofillPopup(target, container);
- }
- }
- };
-
- // Listen for input field focus in the main document
- document.addEventListener('focusin', handleFocusIn);
-
- // Listen for popstate events (back/forward navigation)
- window.addEventListener('popstate', () => {
- if (ctx.isInvalid) {
- return;
- }
-
- removeExistingPopup(container);
+ openAutofillPopup(target, container);
+ },
});
+ } else {
+ injectIcon(target, ui.uiContainer);
+ openAutofillPopup(target, ui.uiContainer);
+ }
- // Listen for messages from the background script
- onMessage('OPEN_AUTOFILL_POPUP', async (message: { data: { elementIdentifier: string } }) : Promise<messageBoolResponse> => {
- const { data } = message;
- const { elementIdentifier } = data;
-
- if (!elementIdentifier) {
- return { success: false, error: 'No element identifier provided' };
- }
-
- const target = document.getElementById(elementIdentifier) ?? document.getElementsByName(elementIdentifier)[0];
-
- if (!(target instanceof HTMLInputElement)) {
- return { success: false, error: 'Target element is not an input field' };
- }
-
- const formDetector = new FormDetector(document, target);
-
- if (!formDetector.containsLoginForm(true)) {
- return { success: false, error: 'No form found' };
- }
-
- injectIcon(target, container);
- openAutofillPopup(target, container);
- return { success: true };
- });
- },
+ return { success: true };
});
-
- // Mount the UI to create the shadow root
- ui.autoMount();
},
});
\ No newline at end of file
diff --git a/browser-extension/src/utils/formDetector/FormDetector.ts b/browser-extension/src/utils/formDetector/FormDetector.ts
index 1b6513e31..bab14a04b 100644
--- a/browser-extension/src/utils/formDetector/FormDetector.ts
+++ b/browser-extension/src/utils/formDetector/FormDetector.ts
@@ -99,26 +99,22 @@ export class FormDetector {
/**
* Detect login forms on the page based on the clicked element.
- *
- * @param force - Force the detection of forms, skipping checks such as if the element contains autocomplete="off".
*/
- public containsLoginForm(force: boolean = false): boolean {
- if (this.clickedElement) {
- const formWrapper = this.clickedElement.closest('form') ?? this.document.body;
+ public containsLoginForm(): boolean {
+ const formWrapper = this.clickedElement?.closest('form') ?? this.document.body;
- /**
- * Sanity check: if form contains more than 150 inputs, don't process as this is likely not a login form.
- * This is a simple way to prevent processing large forms that are not login forms and making the browser page unresponsive.
- */
- const inputCount = formWrapper.querySelectorAll('input').length;
- if (inputCount > 200) {
- return false;
- }
+ /**
+ * Sanity check: if form contains more than 150 inputs, don't process as this is likely not a login form.
+ * This is a simple way to prevent processing large forms that are not login forms and making the browser page unresponsive.
+ */
+ const inputCount = formWrapper.querySelectorAll('input').length;
+ if (inputCount > 200) {
+ return false;
+ }
- // Check if the wrapper contains a password or likely username field before processing.
- if (this.containsPasswordField(formWrapper) || this.containsLikelyUsernameOrEmailField(formWrapper, force)) {
- return true;
- }
+ // Check if the wrapper contains a password or likely username field before processing.
+ if (this.containsPasswordField(formWrapper) || this.containsLikelyUsernameOrEmailField(formWrapper)) {
+ return true;
}
return false;
@@ -126,8 +122,6 @@ export class FormDetector {
/**
* Detect login forms on the page based on the clicked element.
- *
- * @param force - Force the detection of forms, skipping checks such as if the element contains autocomplete="off".
*/
public getForm(): FormFields | null {
if (!this.clickedElement) {
@@ -454,41 +448,29 @@ export class FormDetector {
/**
* Check if a form contains a likely username or email field.
*/
- private containsLikelyUsernameOrEmailField(wrapper: HTMLElement, force: boolean = false): boolean {
+ private containsLikelyUsernameOrEmailField(wrapper: HTMLElement): boolean {
// Check if the form contains an email field.
const emailFields = this.findEmailField(wrapper as HTMLFormElement | null);
if (emailFields.primary && this.isElementVisible(emailFields.primary)) {
- const isValid = force || emailFields.primary.getAttribute('autocomplete') !== 'off';
- if (isValid) {
- return true;
- }
+ return true;
}
// Check if the form contains a username field.
const usernameField = this.findInputField(wrapper as HTMLFormElement | null, CombinedFieldPatterns.username, ['text'], []);
if (usernameField && this.isElementVisible(usernameField)) {
- const isValid = force || usernameField.getAttribute('autocomplete') !== 'off';
- if (isValid) {
- return true;
- }
+ return true;
}
// Check if the form contains a first name field.
const firstNameField = this.findInputField(wrapper as HTMLFormElement | null, CombinedFieldPatterns.firstName, ['text'], []);
if (firstNameField && this.isElementVisible(firstNameField)) {
- const isValid = force || firstNameField.getAttribute('autocomplete') !== 'off';
- if (isValid) {
- return true;
- }
+ return true;
}
// Check if the form contains a last name field.
const lastNameField = this.findInputField(wrapper as HTMLFormElement | null, CombinedFieldPatterns.lastName, ['text'], []);
if (lastNameField && this.isElementVisible(lastNameField)) {
- const isValid = force || lastNameField.getAttribute('autocomplete') !== 'off';
- if (isValid) {
- return true;
- }
+ return true;
}
return false;
diff --git a/browser-extension/src/utils/formDetector/__tests__/FormDetector.en.test.ts b/browser-extension/src/utils/formDetector/__tests__/FormDetector.en.test.ts
index 4ceb98364..b707cfc0b 100644
--- a/browser-extension/src/utils/formDetector/__tests__/FormDetector.en.test.ts
+++ b/browser-extension/src/utils/formDetector/__tests__/FormDetector.en.test.ts
@@ -68,7 +68,6 @@ describe('FormDetector English tests', () => {
describe('English email form 1 detection', () => {
const htmlFile = 'en-email-form1.html';
- // Assert that this test fails, because the autocomplete=off for the specified element.
testField(FormField.Email, 'P0-0', htmlFile);
});
diff --git a/browser-extension/src/utils/formDetector/__tests__/FormDetector.generic.test.ts b/browser-extension/src/utils/formDetector/__tests__/FormDetector.generic.test.ts
index d3137b776..c38b4496e 100644
--- a/browser-extension/src/utils/formDetector/__tests__/FormDetector.generic.test.ts
+++ b/browser-extension/src/utils/formDetector/__tests__/FormDetector.generic.test.ts
@@ -30,15 +30,15 @@ describe('FormDetector generic tests', () => {
});
});
- describe('Form with autocomplete="off" not detected', () => {
+ describe('Form with autocomplete="off" still detected', () => {
const htmlFile = 'autocomplete-off.html';
- it('should not detect form with autocomplete="off" on email field', () => {
+ it('should still detect form with autocomplete="off" on email field', () => {
const dom = createTestDom(htmlFile);
const document = dom.window.document;
const formDetector = new FormDetector(document);
const form = formDetector.containsLoginForm();
- expect(form).toBe(false);
+ expect(form).toBe(true);
});
});