Files
aliasvault/browser-extensions/chrome/contentScript.ts
2025-02-20 11:04:28 +01:00

57 lines
1.8 KiB
TypeScript

import { FormDetector } from './src/shared/formDetector/FormDetector';
import { isAutoShowPopupDisabled, openAutofillPopup, removeExistingPopup } from './src/contentScript/Popup';
import { canShowPopup, injectIcon } from './src/contentScript/Form';
/**
* Listen for input field focus
*/
document.addEventListener('focusin', async (e) => {
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);
const forms = formDetector.detectForms();
if (!forms.length) return;
injectIcon(target);
const isDisabled = await isAutoShowPopupDisabled();
const canShow = canShowPopup();
// Only show popup if it's not disabled and the popup can be shown (not blocked by debounce)
if (!isDisabled && canShow) {
openAutofillPopup(target);
}
}
});
/**
* Listen for popstate events (back/forward navigation)
*/
window.addEventListener('popstate', () => {
removeExistingPopup();
});
/**
* Listen for messages from the background script context menu
* to open the AliasVault popup on a specific element.
*/
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'OPEN_ALIASVAULT_POPUP') {
const elementIdentifier = message.elementIdentifier;
if (elementIdentifier) {
const target = document.getElementById(elementIdentifier) ||
document.getElementsByName(elementIdentifier)[0];
if (target instanceof HTMLInputElement) {
openAutofillPopup(target, true); // Pass true to force open
}
}
}
// Must return true if response is sent asynchronously
return true;
});