diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index b621ac3c6b7..91df38e5174 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -1,10 +1,12 @@ import { CipherType } from 'jslib/enums'; +import { ConstantsService } from 'jslib/services/constants.service'; import { UtilsService } from 'jslib/services/utils.service'; import { CipherService, PlatformUtilsService, + StorageService, } from 'jslib/abstractions'; import { BrowserApi } from '../browser/browserApi'; @@ -20,7 +22,8 @@ export default class RuntimeBackground { private isSafari: boolean; constructor(private main: MainBackground, private autofillService: AutofillService, - private cipherService: CipherService, private platformUtilsService: PlatformUtilsService) { + private cipherService: CipherService, private platformUtilsService: PlatformUtilsService, + private storageService: StorageService) { this.isSafari = this.platformUtilsService.isSafari(); this.runtime = this.isSafari ? safari.application : chrome.runtime; } @@ -83,6 +86,10 @@ export default class RuntimeBackground { setTimeout(async () => await this.main.refreshBadgeAndMenu(), 2000); } break; + case 'bgGetAutofillOnPageLoadEnabled': + await this.sendStorageValueToTab(ConstantsService.enableAutoFillOnPageLoadKey, sender.tab, + msg.responseCommand); + break; case 'bgOpenNotificationBar': await BrowserApi.tabSendMessageData(sender.tab, 'openNotificationBar', msg.data); break; @@ -256,12 +263,8 @@ export default class RuntimeBackground { } } - private async currenttabSendMessageData(command: string, data: any = null) { - const tab = await BrowserApi.getTabFromCurrentWindow(); - if (tab == null) { - return; - } - - await BrowserApi.tabSendMessageData(tab, command, data); + private async sendStorageValueToTab(storageKey: string, tab: any, responseCommand: string) { + const val = await this.storageService.get(storageKey); + await BrowserApi.tabSendMessageData(tab, responseCommand, val); } } diff --git a/src/content/autofiller.js b/src/content/autofiller.js index 8b7561c90ea..72532493cfb 100644 --- a/src/content/autofiller.js +++ b/src/content/autofiller.js @@ -3,13 +3,18 @@ document.addEventListener('DOMContentLoaded', (event) => { const enabledKey = 'enableAutoFillOnPageLoad'; if ((typeof safari !== 'undefined')) { - const json = safari.extension.settings.getItem(enabledKey); - if (json) { - const obj = JSON.parse(json); - if (obj && obj[enabledKey] === true) { + const responseCommand = 'autofillerAutofillOnPageLoadEnabledResponse'; + safari.self.tab.dispatchMessage('bitwarden', { + command: 'bgGetAutofillOnPageLoadEnabled', + responseCommand: responseCommand + }); + safari.self.addEventListener('message', function (msgEvent) { + var msg = msgEvent.message; + if (msg.command === responseCommand && msg.data === true) { setInterval(doFillIfNeeded, 500); } - } + }, false); + return; } else { chrome.storage.local.get(enabledKey, (obj) => {