diff --git a/packages/extension/src/libs/window-promise/promise.ts b/packages/extension/src/libs/window-promise/promise.ts index 78db6010a..f8cd156f3 100644 --- a/packages/extension/src/libs/window-promise/promise.ts +++ b/packages/extension/src/libs/window-promise/promise.ts @@ -15,24 +15,28 @@ class WindowPromise { private async getRawResponse( url: string, msg: string, - tabId: number + tabInfo: { tabId: number } ): Promise { return new Promise((resolve) => { Browser.tabs.onUpdated.addListener(function listener(_tabId, info, tab) { - if (info.status === "complete" && _tabId === tabId && tab.url === url) { + if ( + info.status === "complete" && + _tabId === tabInfo.tabId && + tab.url === url + ) { resolve( sendToNewWindowFromBackground( { provider: ProviderName.enkrypt, message: msg, }, - tabId + tabInfo.tabId ) ); Browser.tabs.onUpdated.removeListener(listener); } }); - Browser.tabs.update(tabId, { url }); + Browser.tabs.update(tabInfo.tabId, { url }); }); } @@ -47,36 +51,49 @@ class WindowPromise { msg: string, unlockKeyring = false ): Promise { + const loadingURL = "/index.html#/enkrypt/loading"; + const tabInfo = { + tabId: -1, + }; const windowInfo = await Browser.windows.create({ - url: "/index.html#/enkrypt/loading", + url: loadingURL, type: "popup", focused: true, height: 600, width: 460, }); - const tabId: number | undefined = windowInfo.tabs?.length - ? windowInfo.tabs[0].id - : 0; - if (typeof tabId === "undefined") { + const validTabs = await Browser.tabs.query({ + windowId: windowInfo.id, + }); + tabInfo.tabId = validTabs.length && validTabs[0].id ? validTabs[0].id : -1; + if (typeof tabInfo.tabId === "undefined" || tabInfo.tabId === -1) { return Promise.resolve({ error: getCustomError("unknown error, no tabId"), }); } const waitForWindow = async (): Promise => { // eslint-disable-next-line no-empty - while ((await Browser.tabs.get(tabId)).status !== "complete") {} + while ((await Browser.tabs.get(tabInfo.tabId)).status !== "complete") {} }; await waitForWindow(); const monitorTabs = (): Promise => { return new Promise((resolve) => { - Browser.tabs.onRemoved.addListener(function tabListener(_tabId) { - if (_tabId === tabId) { + const tabReplacedListener = (addedId: number, replacedId: number) => { + if (replacedId === tabInfo.tabId) { + tabInfo.tabId = addedId; + } + }; + const tabListener = (_tabId: number) => { + if (_tabId === tabInfo.tabId) { Browser.tabs.onRemoved.removeListener(tabListener); + Browser.tabs.onReplaced.removeListener(tabReplacedListener); resolve({ error: getError(ErrorCodes.userRejected), }); } - }); + }; + Browser.tabs.onRemoved.addListener(tabListener); + Browser.tabs.onReplaced.addListener(tabReplacedListener); }); }; const executePromise = async (): Promise => { @@ -91,18 +108,18 @@ class WindowPromise { const unlockKeyring = await this.getRawResponse( Browser.runtime.getURL(UNLOCK_PATH), msg, - tabId + tabInfo ); if (unlockKeyring.error) { - this.removeTab(tabId); + this.removeTab(tabInfo.tabId); return unlockKeyring; } else { return await this.getRawResponse( Browser.runtime.getURL(url), msg, - tabId + tabInfo ).then((res) => { - this.removeTab(tabId); + this.removeTab(tabInfo.tabId); return res; }); } @@ -110,9 +127,9 @@ class WindowPromise { return await this.getRawResponse( Browser.runtime.getURL(url), msg, - tabId + tabInfo ).then((res) => { - this.removeTab(tabId); + this.removeTab(tabInfo.tabId); return res; }); };