diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 31dab16fdd..5678300774 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -114,7 +114,7 @@ export class BrowserApi { tab: tab, obj: JSON.stringify(obj), options: options, - })); + }), true); /* let t = tab.safariTab; if (!t || !t.page) { diff --git a/src/browser/safariApp.ts b/src/browser/safariApp.ts index 4582ed63cc..dc76828274 100644 --- a/src/browser/safariApp.ts +++ b/src/browser/safariApp.ts @@ -1,19 +1,26 @@ import { BrowserApi } from './browserApi'; export class SafariApp { + static inited = false; + static init() { + if (SafariApp.inited) { + return; + } + SafariApp.inited = true; if (BrowserApi.isSafariApi) { (window as any).bitwardenSafariAppRequests = - new Map void, date: Date }>(); + new Map void, timeoutDate: Date }>(); (window as any).bitwardenSafariAppMessageListeners = - new Map void, date: Date }>(); + new Map void>(); (window as any).bitwardenSafariAppMessageReceiver = (message: any) => { SafariApp.receiveMessageFromApp(message); }; + setInterval(() => SafariApp.cleanupOldRequests(), 5 * 60000); } } - static sendMessageToApp(command: string, data: any = null): Promise { + static sendMessageToApp(command: string, data: any = null, resolveNow = false): Promise { if (!BrowserApi.isSafariApi) { return Promise.resolve(null); } @@ -26,7 +33,14 @@ export class SafariApp { data: data, responseData: null, })); - (window as any).bitwardenSafariAppRequests.set(messageId, { resolve: resolve, date: now }); + if (resolveNow) { + resolve(); + } else { + (window as any).bitwardenSafariAppRequests.set(messageId, { + resolve: resolve, + timeoutDate: new Date(now.getTime() + 5 * 60000), + }); + } }); } @@ -50,6 +64,21 @@ export class SafariApp { } else if (message.id != null && (window as any).bitwardenSafariAppRequests.has(message.id)) { const p = (window as any).bitwardenSafariAppRequests.get(message.id); p.resolve(message.responseData); + (window as any).bitwardenSafariAppRequests.delete(message.id); } } + + private static cleanupOldRequests() { + const remoteIds: string[] = []; + ((window as any).bitwardenSafariAppRequests as + Map void, timeoutDate: Date }>) + .forEach((v, key) => { + if (v.timeoutDate < new Date()) { + remoteIds.push(key); + } + }); + remoteIds.forEach((id) => { + (window as any).bitwardenSafariAppRequests.delete(id); + }); + } }