mirror of
https://github.com/bitwarden/browser
synced 2025-12-12 06:13:38 +00:00
setup safari app messaging
This commit is contained in:
2
jslib
2
jslib
Submodule jslib updated: 393f6c9c20...4f876fc222
@@ -57,6 +57,7 @@ import { Analytics } from 'jslib/misc';
|
|||||||
import { Utils } from 'jslib/misc/utils';
|
import { Utils } from 'jslib/misc/utils';
|
||||||
|
|
||||||
import { BrowserApi } from '../browser/browserApi';
|
import { BrowserApi } from '../browser/browserApi';
|
||||||
|
import { SafariApp } from '../browser/safariApp';
|
||||||
|
|
||||||
import CommandsBackground from './commands.background';
|
import CommandsBackground from './commands.background';
|
||||||
import ContextMenusBackground from './contextMenus.background';
|
import ContextMenusBackground from './contextMenus.background';
|
||||||
@@ -216,6 +217,7 @@ export default class MainBackground {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async bootstrap() {
|
async bootstrap() {
|
||||||
|
SafariApp.init();
|
||||||
this.analytics.ga('send', 'pageview', '/background.html');
|
this.analytics.ga('send', 'pageview', '/background.html');
|
||||||
this.containerService.attachToWindow(window);
|
this.containerService.attachToWindow(window);
|
||||||
|
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ export class BrowserApi {
|
|||||||
return chrome.runtime.getManifest().version;
|
return chrome.runtime.getManifest().version;
|
||||||
} else if (BrowserApi.isSafariApi) {
|
} else if (BrowserApi.isSafariApi) {
|
||||||
// TODO
|
// TODO
|
||||||
return null;
|
return 'TODO';
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -291,10 +291,4 @@ export class BrowserApi {
|
|||||||
return chrome.runtime.reload();
|
return chrome.runtime.reload();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static sendSafariMessageToApp(message: any, response: (data: any) => {} = null) {
|
|
||||||
if (this.isSafariApi) {
|
|
||||||
(window as any).webkit.messageHandlers.bitwardenApp.postMessage(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
35
src/browser/safariApp.ts
Normal file
35
src/browser/safariApp.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import { BrowserApi } from './browserApi';
|
||||||
|
|
||||||
|
export class SafariApp {
|
||||||
|
static init() {
|
||||||
|
if (BrowserApi.isSafariApi) {
|
||||||
|
(window as any).bitwardenSafariAppMessageReceiver = (message: any) =>
|
||||||
|
SafariApp.receiveMessageFromApp(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static sendMessageToApp(command: string, data: any = null): Promise<any> {
|
||||||
|
if (!BrowserApi.isSafariApi) {
|
||||||
|
return Promise.resolve(null);
|
||||||
|
}
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const messageId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
||||||
|
(window as any).webkit.messageHandlers.bitwardenApp.postMessage({
|
||||||
|
id: messageId,
|
||||||
|
command: command,
|
||||||
|
data: data,
|
||||||
|
});
|
||||||
|
SafariApp.requests.set(messageId, { resolve: resolve, date: new Date() });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static requests = new Map<number, { resolve: (value?: unknown) => void, date: Date }>();
|
||||||
|
|
||||||
|
private static receiveMessageFromApp(message: any) {
|
||||||
|
if (message == null || message.id == null || !SafariApp.requests.has(message.id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const p = SafariApp.requests.get(message.id);
|
||||||
|
p.resolve(message.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user