1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-06-20 09:35:22 +02:00
bitwarden-browser/apps/browser/src/autofill/content/autofiller.ts
Cesar Gonzalez bf60711efe
[PM-934] Autofill not working until page has been refreshed (#6826)
* [PM-934] Autofill not working until page has been refreshed

* [PM-934] Adjusting cleanup of the messages_handler script

* [PM-934] Fixing small issue found within collection of page details

* [PM-934] Addressing concenrs brought up during code review

* [PM-934] Addressing concenrs brought up during code review

* [PM-934] Addressing concenrs brought up during code review

* [PM-934] Addressing concenrs brought up during code review

* [PM-934] Applying re-set changes to the autofill overlay implementation on reset of the extension

* [PM-934] Applying jest tests to added logic within AutofillOverlayContent service

* [PM-934] Fixing typo present in tabs background listener

* [PM-934] Finishing up jest tests for updated implementation

* [PM-934] Incorporating methodology for ensuring the autofill overlay updates to reflect user settings within existing tabs

* [PM-934] Refining implementation to ensure we do not unnecessarily re-inject content scripts when the autofill overlay settings change

* [PM-934] Working through jest tests for added implementation details

* [PM-934] Working through jest tests for added implementation details

* [PM-934] Finalizing jest tests for implemented logic

* [PM-5035] Refactoring method structure
2023-12-13 16:25:16 +00:00

79 lines
2.2 KiB
TypeScript

import { getFromLocalStorage, setupExtensionDisconnectAction } from "../utils";
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", loadAutofiller);
} else {
loadAutofiller();
}
function loadAutofiller() {
let pageHref: string = null;
let filledThisHref = false;
let delayFillTimeout: number;
let doFillInterval: NodeJS.Timeout;
const handleExtensionDisconnect = () => {
clearDoFillInterval();
clearDelayFillTimeout();
};
const handleExtensionMessage = (message: any) => {
if (message.command === "fillForm" && pageHref === message.url) {
filledThisHref = true;
}
};
setupExtensionEventListeners();
triggerUserFillOnLoad();
async function triggerUserFillOnLoad() {
const activeUserIdKey = "activeUserId";
const userKeyStorage = await getFromLocalStorage(activeUserIdKey);
const activeUserId = userKeyStorage[activeUserIdKey];
const activeUserStorage = await getFromLocalStorage(activeUserId);
if (activeUserStorage?.[activeUserId]?.settings?.enableAutoFillOnPageLoad === true) {
clearDoFillInterval();
doFillInterval = setInterval(() => doFillIfNeeded(), 500);
}
}
function doFillIfNeeded(force = false) {
if (force || pageHref !== window.location.href) {
if (!force) {
// Some websites are slow and rendering all page content. Try to fill again later
// if we haven't already.
filledThisHref = false;
clearDelayFillTimeout();
delayFillTimeout = window.setTimeout(() => {
if (!filledThisHref) {
doFillIfNeeded(true);
}
}, 1500);
}
pageHref = window.location.href;
const msg: any = {
command: "bgCollectPageDetails",
sender: "autofiller",
};
chrome.runtime.sendMessage(msg);
}
}
function clearDoFillInterval() {
if (doFillInterval) {
window.clearInterval(doFillInterval);
}
}
function clearDelayFillTimeout() {
if (delayFillTimeout) {
window.clearTimeout(delayFillTimeout);
}
}
function setupExtensionEventListeners() {
setupExtensionDisconnectAction(handleExtensionDisconnect);
chrome.runtime.onMessage.addListener(handleExtensionMessage);
}
}