send message to safari tab for autofill

This commit is contained in:
Kyle Spearrin 2018-01-12 14:44:44 -05:00
parent ca5026429a
commit 0a056a3941
5 changed files with 51 additions and 23 deletions

View File

@ -91,7 +91,7 @@ export default class ContextMenusBackground {
return;
}
chrome.tabs.sendMessage(tab.id, {
BrowserApi.tabSendMessage(tab, {
command: 'collectPageDetails',
tab: tab,
sender: 'contextMenu',

View File

@ -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<any> {

View File

@ -81,19 +81,33 @@ class BrowserApi {
return BrowserApi.tabSendMessage(tab, obj);
}
static tabSendMessage(tab: any, obj: any): Promise<any> {
static tabSendMessage(tab: any, obj: any, options: any = null): Promise<any> {
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();
}
}

View File

@ -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);

View File

@ -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<any> {
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<any> {
const tab = await BrowserApi.getTabFromCurrentWindow();
if (!tab) {
throw new Error('No tab found.');
}
});
});
return tab;
}
private generateFillScript(pageDetails: AutofillPageDetails, options: any): AutofillScript {