wire up internal messaging for safari app

This commit is contained in:
Kyle Spearrin 2019-08-15 16:36:49 -04:00
parent 3e27b3634e
commit 06234f6b23
6 changed files with 16 additions and 5 deletions

View File

@ -23,7 +23,7 @@ export default class CommandsBackground {
async init() {
if (this.isSafari || this.isEdge || this.isVivaldi) {
BrowserApi.messageListener(async (msg: any, sender: any, sendResponse: any) => {
BrowserApi.messageListener('commands.background', async (msg: any, sender: any, sendResponse: any) => {
if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) {
await this.processCommand(msg.shortcut, sender);
}

View File

@ -76,7 +76,7 @@ export default class RuntimeBackground {
await this.checkOnInstalled();
BrowserApi.messageListener(async (msg: any, sender: any, sendResponse: any) => {
BrowserApi.messageListener('runtime.background', async (msg: any, sender: any, sendResponse: any) => {
await this.processMessage(msg, sender, sendResponse);
});
}

View File

@ -196,12 +196,13 @@ export class BrowserApi {
}
}
static messageListener(callback: (message: any, sender: any, response: any) => void) {
static messageListener(name: string, 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) {
SafariApp.addMessageListener(name, callback);
// TODO
/*
safari.application.addEventListener('message', async (msgEvent: any) => {

View File

@ -26,7 +26,16 @@ export class SafariApp {
});
}
static addMessageListener(name: string, callback: (message: any, sender: any, response: any) => void) {
SafariApp.messageListeners.set(name, callback);
}
static sendMessageToListeners(message: any, sender: any, response: any) {
SafariApp.messageListeners.forEach((f) => f(message, sender, response));
}
private static requests = new Map<string, { resolve: (value?: unknown) => void, date: Date }>();
private static messageListeners = new Map<string, (message: any, sender: any, response: any) => void>();
private static receiveMessageFromApp(message: any) {
if (message == null || message.id == null || !SafariApp.requests.has(message.id)) {

View File

@ -133,7 +133,7 @@ export class AppComponent implements OnInit {
}
};
BrowserApi.messageListener((window as any).bitwardenPopupMainMessageListener);
BrowserApi.messageListener('app.component', (window as any).bitwardenPopupMainMessageListener);
this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {

View File

@ -5,10 +5,11 @@ import { MessagingService } from 'jslib/abstractions/messaging.service';
export default class BrowserMessagingService implements MessagingService {
send(subscriber: string, arg: any = {}) {
const message = Object.assign({}, { command: subscriber }, arg);
if (BrowserApi.isSafariApi) {
SafariApp.sendMessageToApp(subscriber, arg);
SafariApp.sendMessageToListeners(message, 'BrowserMessagingService', null);
} else {
const message = Object.assign({}, { command: subscriber }, arg);
chrome.runtime.sendMessage(message);
}
}