diff --git a/apps/browser/src/autofill/background/abstractions/overlay.background.ts b/apps/browser/src/autofill/background/abstractions/overlay.background.ts index ae233de750..b0bd5f2246 100644 --- a/apps/browser/src/autofill/background/abstractions/overlay.background.ts +++ b/apps/browser/src/autofill/background/abstractions/overlay.background.ts @@ -106,10 +106,10 @@ type OverlayBackgroundExtensionMessageHandlers = { unlockCompleted: ({ message }: BackgroundMessageParam) => void; addEditCipherSubmitted: () => void; deletedCipher: () => void; - checkIsFieldCurrentlyFocused: () => boolean; - checkIsFieldCurrentlyFilling: () => boolean; updateIsFieldCurrentlyFocused: ({ message }: BackgroundMessageParam) => void; + checkIsFieldCurrentlyFocused: () => boolean; updateIsFieldCurrentlyFilling: ({ message }: BackgroundMessageParam) => void; + checkIsFieldCurrentlyFilling: () => boolean; checkIsInlineMenuButtonVisible: ({ sender }: BackgroundSenderParam) => void; checkIsInlineMenuListVisible: ({ sender }: BackgroundSenderParam) => void; checkIsInlineMenuCiphersPopulated: ({ sender }: BackgroundSenderParam) => void; diff --git a/apps/browser/src/autofill/background/overlay.background.ts b/apps/browser/src/autofill/background/overlay.background.ts index bba50cce27..e96a5106ab 100644 --- a/apps/browser/src/autofill/background/overlay.background.ts +++ b/apps/browser/src/autofill/background/overlay.background.ts @@ -77,12 +77,10 @@ class OverlayBackground implements OverlayBackgroundInterface { unlockCompleted: ({ message }) => this.unlockCompleted(message), addEditCipherSubmitted: () => this.updateOverlayCiphers(), deletedCipher: () => this.updateOverlayCiphers(), - checkIsFieldCurrentlyFocused: () => this.isFieldCurrentlyFocused, - checkIsFieldCurrentlyFilling: () => this.isCurrentlyFilling, - updateIsFieldCurrentlyFocused: ({ message }) => - (this.isFieldCurrentlyFocused = message.isFieldCurrentlyFocused), - updateIsFieldCurrentlyFilling: ({ message }) => - (this.isCurrentlyFilling = message.isFieldCurrentlyFilling), + updateIsFieldCurrentlyFocused: ({ message }) => this.updateIsFieldCurrentlyFocused(message), + checkIsFieldCurrentlyFocused: () => this.checkIsFieldCurrentlyFocused(), + updateIsFieldCurrentlyFilling: ({ message }) => this.updateIsFieldCurrentlyFilling(message), + checkIsFieldCurrentlyFilling: () => this.checkIsFieldCurrentlyFilling(), checkIsInlineMenuButtonVisible: ({ sender }) => this.checkIsInlineMenuButtonVisible(sender), checkIsInlineMenuListVisible: ({ sender }) => this.checkIsInlineMenuListVisible(sender), checkIsInlineMenuCiphersPopulated: ({ sender }) => @@ -123,33 +121,6 @@ class OverlayBackground implements OverlayBackgroundInterface { private themeStateService: ThemeStateService, ) {} - private async checkIsInlineMenuButtonVisible(sender: chrome.runtime.MessageSender) { - return await BrowserApi.tabSendMessage( - sender.tab, - { command: "checkIsInlineMenuButtonVisible" }, - { frameId: 0 }, - ); - } - - private async checkIsInlineMenuListVisible(sender: chrome.runtime.MessageSender) { - return await BrowserApi.tabSendMessage( - sender.tab, - { command: "checkIsInlineMenuListVisible" }, - { frameId: 0 }, - ); - } - - private checkIsInlineMenuCiphersPopulated(sender: chrome.runtime.MessageSender) { - return sender.tab.id === this.focusedFieldData.tabId && this.overlayLoginCiphers.size > 0; - } - - updateSubFrameData(message: any, sender: chrome.runtime.MessageSender) { - const subFrameOffsetsForTab = this.subFrameOffsetsForTab[sender.tab.id]; - if (subFrameOffsetsForTab) { - subFrameOffsetsForTab.set(message.subFrameData.frameId, message.subFrameData); - } - } - /** * Removes cached page details for a tab * based on the passed tabId. @@ -271,36 +242,11 @@ class OverlayBackground implements OverlayBackgroundInterface { pageDetailsMap.set(sender.frameId, pageDetails); } - private async rebuildSubFrameOffsets(sender: chrome.runtime.MessageSender) { - if (sender.frameId === this.focusedFieldData?.frameId) { - return; - } - + private updateSubFrameData(message: any, sender: chrome.runtime.MessageSender) { const subFrameOffsetsForTab = this.subFrameOffsetsForTab[sender.tab.id]; - if (!subFrameOffsetsForTab) { - return; + if (subFrameOffsetsForTab) { + subFrameOffsetsForTab.set(message.subFrameData.frameId, message.subFrameData); } - - if (this.rebuildSubFrameOffsetsTimeout) { - clearTimeout(this.rebuildSubFrameOffsetsTimeout as number); - } - - const frameTabs = Array.from(subFrameOffsetsForTab.keys()); - for (const frameId of frameTabs) { - if (frameId === sender.frameId) { - continue; - } - - subFrameOffsetsForTab.delete(frameId); - await this.buildSubFrameOffsets(sender.tab, frameId, sender.url); - } - - this.rebuildSubFrameOffsetsTimeout = setTimeout(() => { - if (this.isFieldCurrentlyFocused) { - void this.updateOverlayPosition({ overlayElement: AutofillOverlayElement.List }, sender); - void this.updateOverlayPosition({ overlayElement: AutofillOverlayElement.Button }, sender); - } - }, 650); } private async buildSubFrameOffsets(tab: chrome.tabs.Tab, frameId: number, url: string) { @@ -351,6 +297,38 @@ class OverlayBackground implements OverlayBackgroundInterface { subFrameOffsetsForTab.set(frameId, subFrameData); } + private async rebuildSubFrameOffsets(sender: chrome.runtime.MessageSender) { + if (sender.frameId === this.focusedFieldData?.frameId) { + return; + } + + const subFrameOffsetsForTab = this.subFrameOffsetsForTab[sender.tab.id]; + if (!subFrameOffsetsForTab) { + return; + } + + if (this.rebuildSubFrameOffsetsTimeout) { + clearTimeout(this.rebuildSubFrameOffsetsTimeout as number); + } + + const frameTabs = Array.from(subFrameOffsetsForTab.keys()); + for (const frameId of frameTabs) { + if (frameId === sender.frameId) { + continue; + } + + subFrameOffsetsForTab.delete(frameId); + await this.buildSubFrameOffsets(sender.tab, frameId, sender.url); + } + + this.rebuildSubFrameOffsetsTimeout = setTimeout(() => { + if (this.isFieldCurrentlyFocused) { + void this.updateOverlayPosition({ overlayElement: AutofillOverlayElement.List }, sender); + void this.updateOverlayPosition({ overlayElement: AutofillOverlayElement.Button }, sender); + } + }, 650); + } + /** * Triggers autofill for the selected cipher in the overlay list. Also places * the selected cipher at the top of the list of ciphers. @@ -854,6 +832,42 @@ class OverlayBackground implements OverlayBackgroundInterface { await BrowserApi.sendMessage("inlineAutofillMenuRefreshAddEditCipher"); } + private updateIsFieldCurrentlyFocused({ message }: OverlayBackgroundExtensionMessage) { + this.isFieldCurrentlyFocused = message.isFieldCurrentlyFocused; + } + + private checkIsFieldCurrentlyFocused() { + return this.isFieldCurrentlyFocused; + } + + private updateIsFieldCurrentlyFilling({ message }: OverlayBackgroundExtensionMessage) { + this.isCurrentlyFilling = message.isFieldCurrentlyFilling; + } + + private checkIsFieldCurrentlyFilling() { + return this.isCurrentlyFilling; + } + + private async checkIsInlineMenuButtonVisible(sender: chrome.runtime.MessageSender) { + return await BrowserApi.tabSendMessage( + sender.tab, + { command: "checkIsInlineMenuButtonVisible" }, + { frameId: 0 }, + ); + } + + private async checkIsInlineMenuListVisible(sender: chrome.runtime.MessageSender) { + return await BrowserApi.tabSendMessage( + sender.tab, + { command: "checkIsInlineMenuListVisible" }, + { frameId: 0 }, + ); + } + + private checkIsInlineMenuCiphersPopulated(sender: chrome.runtime.MessageSender) { + return sender.tab.id === this.focusedFieldData.tabId && this.overlayLoginCiphers.size > 0; + } + /** * Sets up the extension message listeners for the overlay. */