From 0a056a3941e86b4505d084b9f68762c54041a1ab Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 12 Jan 2018 14:44:44 -0500 Subject: [PATCH] send message to safari tab for autofill --- src/background/contextMenus.background.ts | 2 +- src/background/main.background.ts | 8 ++------ src/browser/browserApi.ts | 20 +++++++++++++++++--- src/content/autofill.js | 22 +++++++++++++++++++++- src/services/autofill.service.ts | 22 ++++++++++------------ 5 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index b0a37ee9aa..93ffbe6a20 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -91,7 +91,7 @@ export default class ContextMenusBackground { return; } - chrome.tabs.sendMessage(tab.id, { + BrowserApi.tabSendMessage(tab, { command: 'collectPageDetails', tab: tab, sender: 'contextMenu', diff --git a/src/background/main.background.ts b/src/background/main.background.ts index a8a25afd4e..301430648d 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -248,15 +248,11 @@ export default class MainBackground { options.frameId = frameId; } - chrome.tabs.sendMessage(tab.id, { + BrowserApi.tabSendMessage(tab, { command: 'collectPageDetails', tab: tab, sender: sender, - }, options, () => { - if (chrome.runtime.lastError) { - return; - } - }); + }, options); } async checkLoginsToAdd(tab: any = null): Promise { diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 7a9ad88a47..742cc7c680 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -81,19 +81,33 @@ class BrowserApi { return BrowserApi.tabSendMessage(tab, obj); } - static tabSendMessage(tab: any, obj: any): Promise { + static tabSendMessage(tab: any, obj: any, options: any = null): Promise { if (!tab || !tab.id) { return; } if (BrowserApi.isChromeApi) { return new Promise((resolve) => { - chrome.tabs.sendMessage(tab.id, obj, () => { + chrome.tabs.sendMessage(tab.id, obj, options, () => { + if (chrome.runtime.lastError) { + // Some error happened + } resolve(); }); }); } else if (BrowserApi.isSafariApi) { - return Promise.resolve(); // TODO + const win = safari.application.activeBrowserWindow; + if (safari.application.browserWindows.indexOf(win) !== tab.windowId) { + return Promise.reject('Window not found.'); + } + + if (safari.application.activeBrowserWindow.tabs.length < tab.index + 1) { + return Promise.reject('Tab not found.'); + } + + const t = safari.application.activeBrowserWindow.tabs[tab.index]; + t.page.dispatchMessage('bitwarden', obj); + return Promise.resolve(); } } diff --git a/src/content/autofill.js b/src/content/autofill.js index 321ec34a64..e02377012e 100644 --- a/src/content/autofill.js +++ b/src/content/autofill.js @@ -1,4 +1,4 @@ -!(function () { +!(function () { /* 1Password Extension @@ -994,6 +994,26 @@ End 1Password Extension */ + if ((typeof safari !== 'undefined')) { + safari.self.addEventListener('bitwarden', function (msgEvent) { + var msg = msgEvent.message; + if (msg.command === 'collectPageDetails') { + var pageDetails = collect(document); + var pageDetailsObj = JSON.parse(pageDetails); + safari.self.tab.dispatchMessage('bitwarden', { + command: 'collectPageDetailsResponse', + tab: msg.tab, + details: pageDetailsObj, + sender: msg.sender + }); + } + else if (msg.command === 'fillForm') { + fill(document, msg.fillScript); + } + }, false); + return; + } + chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { if (msg.command === 'collectPageDetails') { var pageDetails = collect(document); diff --git a/src/services/autofill.service.ts b/src/services/autofill.service.ts index d2518de2f9..a1a52341af 100644 --- a/src/services/autofill.service.ts +++ b/src/services/autofill.service.ts @@ -7,6 +7,8 @@ import AutofillField from '../models/autofillField'; import AutofillPageDetails from '../models/autofillPageDetails'; import AutofillScript from '../models/autofillScript'; +import { BrowserApi } from '../browser/browserApi'; + import { UtilsService } from 'jslib/services'; import { AutofillService as AutofillServiceInterface } from './abstractions/autofill.service'; @@ -170,9 +172,8 @@ export default class AutofillService implements AutofillServiceInterface { this.cipherService.updateLastUsedDate(options.cipher.id); } - chrome.tabs.sendMessage(tab.id, { + BrowserApi.tabSendMessage(tab, { command: 'fillForm', - // tslint:disable-next-line fillScript: fillScript, }, { frameId: pd.frameId }); @@ -238,16 +239,13 @@ export default class AutofillService implements AutofillServiceInterface { // Helpers - private getActiveTab(): Promise { - return new Promise((resolve, reject) => { - chrome.tabs.query({ active: true, currentWindow: true }, (tabs: any[]) => { - if (tabs.length === 0) { - reject('No tab found.'); - } else { - resolve(tabs[0]); - } - }); - }); + private async getActiveTab(): Promise { + const tab = await BrowserApi.getTabFromCurrentWindow(); + if (!tab) { + throw new Error('No tab found.'); + } + + return tab; } private generateFillScript(pageDetails: AutofillPageDetails, options: any): AutofillScript {