convert pages to use browser api message functions

This commit is contained in:
Kyle Spearrin 2018-01-12 11:32:42 -05:00
parent 8f438d818b
commit 2f107ea0c2
5 changed files with 47 additions and 25 deletions

View File

@ -468,7 +468,7 @@ export default class MainBackground {
continue; continue;
} }
BrowserApi.tabSendMessage(tab, 'openNotificationBar', { BrowserApi.tabSendMessageData(tab, 'openNotificationBar', {
type: 'add', type: 'add',
}); });
break; break;

View File

@ -58,13 +58,13 @@ export default class RuntimeBackground {
} }
break; break;
case 'bgOpenNotificationBar': case 'bgOpenNotificationBar':
await BrowserApi.tabSendMessage(sender.tab, 'openNotificationBar', msg.data); await BrowserApi.tabSendMessageData(sender.tab, 'openNotificationBar', msg.data);
break; break;
case 'bgCloseNotificationBar': case 'bgCloseNotificationBar':
await BrowserApi.tabSendMessage(sender.tab, 'closeNotificationBar'); await BrowserApi.tabSendMessageData(sender.tab, 'closeNotificationBar');
break; break;
case 'bgAdjustNotificationBar': case 'bgAdjustNotificationBar':
await BrowserApi.tabSendMessage(sender.tab, 'adjustNotificationBar', msg.data); await BrowserApi.tabSendMessageData(sender.tab, 'adjustNotificationBar', msg.data);
break; break;
case 'bgCollectPageDetails': case 'bgCollectPageDetails':
this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId); this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId);
@ -88,7 +88,7 @@ export default class RuntimeBackground {
switch (msg.sender) { switch (msg.sender) {
case 'notificationBar': case 'notificationBar':
const forms = this.autofillService.getFormsWithPasswordFields(msg.details); const forms = this.autofillService.getFormsWithPasswordFields(msg.details);
await BrowserApi.tabSendMessage(msg.tab, 'notificationBarPageDetails', { await BrowserApi.tabSendMessageData(msg.tab, 'notificationBarPageDetails', {
details: msg.details, details: msg.details,
forms: forms, forms: forms,
}); });
@ -166,7 +166,7 @@ export default class RuntimeBackground {
eventAction: 'Added Login from Notification Bar', eventAction: 'Added Login from Notification Bar',
}); });
BrowserApi.tabSendMessage(tab, 'closeNotificationBar'); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
} }
} }
@ -185,7 +185,7 @@ export default class RuntimeBackground {
this.main.loginsToAdd.splice(i, 1); this.main.loginsToAdd.splice(i, 1);
const hostname = UtilsService.getHostname(tab.url); const hostname = UtilsService.getHostname(tab.url);
await this.cipherService.saveNeverDomain(hostname); await this.cipherService.saveNeverDomain(hostname);
BrowserApi.tabSendMessage(tab, 'closeNotificationBar'); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
} }
} }
@ -231,12 +231,12 @@ export default class RuntimeBackground {
} }
} }
private async currentTabSendMessage(command: string, data: any = null) { private async currenttabSendMessageData(command: string, data: any = null) {
const tab = await BrowserApi.getTabFromCurrentWindow(); const tab = await BrowserApi.getTabFromCurrentWindow();
if (tab == null) { if (tab == null) {
return; return;
} }
await BrowserApi.tabSendMessage(tab, command, data); await BrowserApi.tabSendMessageData(tab, command, data);
} }
} }

View File

@ -33,11 +33,7 @@ class BrowserApi {
return null; return null;
} }
static tabSendMessage(tab: any, command: string, data: any = null): Promise<any[]> { static tabSendMessageData(tab: any, command: string, data: any = null): Promise<any[]> {
if (!tab || !tab.id) {
return;
}
const obj: any = { const obj: any = {
command: command, command: command,
}; };
@ -46,11 +42,23 @@ class BrowserApi {
obj.data = data; obj.data = data;
} }
return BrowserApi.tabSendMessage(tab, obj);
}
static tabSendMessage(tab: any, obj: any): Promise<any[]> {
if (!tab || !tab.id) {
return;
}
if (BrowserApi.isChromeApi) {
return new Promise((resolve) => { return new Promise((resolve) => {
chrome.tabs.sendMessage(tab.id, obj, () => { chrome.tabs.sendMessage(tab.id, obj, () => {
resolve(); resolve();
}); });
}); });
} else if (BrowserApi.isSafariApi) {
// TODO
}
} }
static getBackgroundPage(): any { static getBackgroundPage(): any {
@ -102,6 +110,16 @@ class BrowserApi {
return null; return null;
} }
} }
static messageListener(callback: (message: any, sender: any, response: any) => void) {
if (BrowserApi.isChromeApi) {
chrome.runtime.onMessage.addListener((msg: any, sender: any, response: any) => {
callback(msg, sender, response);
});
} else if (BrowserApi.isSafariApi) {
// TODO
}
}
} }
export { BrowserApi }; export { BrowserApi };

View File

@ -1,5 +1,7 @@
import * as template from './current.component.html'; import * as template from './current.component.html';
import { BrowserApi } from '../../../browser/browserApi';
import { CipherType } from 'jslib/enums/cipherType'; import { CipherType } from 'jslib/enums/cipherType';
import { CipherService } from 'jslib/abstractions/cipher.service'; import { CipherService } from 'jslib/abstractions/cipher.service';
@ -103,9 +105,9 @@ export class CurrentController {
} }
private loadVault() { private loadVault() {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs: any) => { BrowserApi.getTabFromCurrentWindow().then((tab: any) => {
if (tabs.length > 0) { if (tab) {
this.url = tabs[0].url; this.url = tab.url;
} else { } else {
this.$timeout(() => { this.$timeout(() => {
this.loaded = true; this.loaded = true;
@ -115,11 +117,11 @@ export class CurrentController {
this.domain = this.platformUtilsService.getDomain(this.url); this.domain = this.platformUtilsService.getDomain(this.url);
chrome.tabs.sendMessage(tabs[0].id, { BrowserApi.tabSendMessage(tab, {
command: 'collectPageDetails', command: 'collectPageDetails',
tab: tabs[0], tab: tab,
sender: 'currentController', sender: 'currentController',
}, () => { }).then(() => {
this.canAutofill = true; this.canAutofill = true;
}); });

View File

@ -1,3 +1,5 @@
import { BrowserApi } from '../../../browser/browserApi';
import AuthService from '../services/auth.service'; import AuthService from '../services/auth.service';
import { UtilsService } from 'jslib/abstractions/utils.service'; import { UtilsService } from 'jslib/abstractions/utils.service';
@ -23,7 +25,7 @@ export class MainController implements ng.IController {
} }
}); });
chrome.runtime.onMessage.addListener((msg: any, sender: any, sendResponse: any) => { BrowserApi.messageListener((msg: any, sender: any, sendResponse: any) => {
if (msg.command === 'syncCompleted') { if (msg.command === 'syncCompleted') {
$scope.$broadcast('syncCompleted', msg.successfully); $scope.$broadcast('syncCompleted', msg.successfully);
} else if (msg.command === 'syncStarted') { } else if (msg.command === 'syncStarted') {