diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index 50724a3b9c..96c492b0f3 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -42,6 +42,15 @@ export default class RuntimeBackground { } }, true); + this.runtime.addEventListener('message', async (msgEvent: any) => { + await this.processMessage(msgEvent.message, { + tab: { + id: null, // TODO + }, + frameId: null, + }, () => { /* No responses in Safari */ }); + }, false); + return; } @@ -59,84 +68,88 @@ export default class RuntimeBackground { } this.runtime.onMessage.addListener(async (msg: any, sender: any, sendResponse: any) => { - switch (msg.command) { - case 'loggedIn': - case 'unlocked': - case 'locked': - await this.main.setIcon(); - await this.main.refreshBadgeAndMenu(); - break; - case 'logout': - await this.main.logout(msg.expired); - break; - case 'syncCompleted': - if (msg.successfully) { - setTimeout(async () => await this.main.refreshBadgeAndMenu(), 2000); - } - break; - case 'bgOpenNotificationBar': - await BrowserApi.tabSendMessageData(sender.tab, 'openNotificationBar', msg.data); - break; - case 'bgCloseNotificationBar': - await BrowserApi.tabSendMessageData(sender.tab, 'closeNotificationBar'); - break; - case 'bgAdjustNotificationBar': - await BrowserApi.tabSendMessageData(sender.tab, 'adjustNotificationBar', msg.data); - break; - case 'bgCollectPageDetails': - this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId); - break; - case 'bgAddLogin': - await this.addLogin(msg.login, sender.tab); - break; - case 'bgAddClose': - this.removeAddLogin(sender.tab); - break; - case 'bgAddSave': - await this.saveAddLogin(sender.tab); - break; - case 'bgNeverSave': - await this.saveNever(sender.tab); - break; - case 'bgUpdateContextMenu': - await this.main.refreshBadgeAndMenu(); - break; - case 'collectPageDetailsResponse': - switch (msg.sender) { - case 'notificationBar': - const forms = this.autofillService.getFormsWithPasswordFields(msg.details); - await BrowserApi.tabSendMessageData(msg.tab, 'notificationBarPageDetails', { - details: msg.details, - forms: forms, - }); - break; - case 'autofiller': - case 'autofill_cmd': - await this.autofillService.doAutoFillForLastUsedLogin([{ - frameId: sender.frameId, - tab: msg.tab, - details: msg.details, - }], msg.sender === 'autofill_cmd'); - break; - case 'contextMenu': - clearTimeout(this.autofillTimeout); - this.pageDetailsToAutoFill.push({ - frameId: sender.frameId, - tab: msg.tab, - details: msg.details, - }); - this.autofillTimeout = setTimeout(async () => await this.autofillPage(), 300); - break; - default: - break; - } - break; - default: - break; - } + await this.processMessage(msg, sender, sendResponse); }); } + private async processMessage(msg: any, sender: any, sendResponse: any) { + switch (msg.command) { + case 'loggedIn': + case 'unlocked': + case 'locked': + await this.main.setIcon(); + await this.main.refreshBadgeAndMenu(); + break; + case 'logout': + await this.main.logout(msg.expired); + break; + case 'syncCompleted': + if (msg.successfully) { + setTimeout(async () => await this.main.refreshBadgeAndMenu(), 2000); + } + break; + case 'bgOpenNotificationBar': + await BrowserApi.tabSendMessageData(sender.tab, 'openNotificationBar', msg.data); + break; + case 'bgCloseNotificationBar': + await BrowserApi.tabSendMessageData(sender.tab, 'closeNotificationBar'); + break; + case 'bgAdjustNotificationBar': + await BrowserApi.tabSendMessageData(sender.tab, 'adjustNotificationBar', msg.data); + break; + case 'bgCollectPageDetails': + this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId); + break; + case 'bgAddLogin': + await this.addLogin(msg.login, sender.tab); + break; + case 'bgAddClose': + this.removeAddLogin(sender.tab); + break; + case 'bgAddSave': + await this.saveAddLogin(sender.tab); + break; + case 'bgNeverSave': + await this.saveNever(sender.tab); + break; + case 'bgUpdateContextMenu': + await this.main.refreshBadgeAndMenu(); + break; + case 'collectPageDetailsResponse': + switch (msg.sender) { + case 'notificationBar': + const forms = this.autofillService.getFormsWithPasswordFields(msg.details); + await BrowserApi.tabSendMessageData(msg.tab, 'notificationBarPageDetails', { + details: msg.details, + forms: forms, + }); + break; + case 'autofiller': + case 'autofill_cmd': + await this.autofillService.doAutoFillForLastUsedLogin([{ + frameId: sender.frameId, + tab: msg.tab, + details: msg.details, + }], msg.sender === 'autofill_cmd'); + break; + case 'contextMenu': + clearTimeout(this.autofillTimeout); + this.pageDetailsToAutoFill.push({ + frameId: sender.frameId, + tab: msg.tab, + details: msg.details, + }); + this.autofillTimeout = setTimeout(async () => await this.autofillPage(), 300); + break; + default: + break; + } + break; + default: + break; + } + } + private async autofillPage() { await this.autofillService.doAutoFill({ cipher: this.main.loginToAutoFill, diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 742cc7c680..a4a975e4ae 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -101,11 +101,11 @@ class BrowserApi { return Promise.reject('Window not found.'); } - if (safari.application.activeBrowserWindow.tabs.length < tab.index + 1) { + if (win.tabs.length < tab.index + 1) { return Promise.reject('Tab not found.'); } - const t = safari.application.activeBrowserWindow.tabs[tab.index]; + const t = win.tabs[tab.index]; t.page.dispatchMessage('bitwarden', obj); return Promise.resolve(); } diff --git a/src/content/autofill.js b/src/content/autofill.js index e02377012e..af7d2a3a3b 100644 --- a/src/content/autofill.js +++ b/src/content/autofill.js @@ -995,7 +995,7 @@ */ if ((typeof safari !== 'undefined')) { - safari.self.addEventListener('bitwarden', function (msgEvent) { + safari.self.addEventListener('message', function (msgEvent) { var msg = msgEvent.message; if (msg.command === 'collectPageDetails') { var pageDetails = collect(document);