diff --git a/apps/browser/src/platform/browser/browser-api.ts b/apps/browser/src/platform/browser/browser-api.ts index 33f18ce572..072ef74004 100644 --- a/apps/browser/src/platform/browser/browser-api.ts +++ b/apps/browser/src/platform/browser/browser-api.ts @@ -58,11 +58,33 @@ export class BrowserApi { } static async createWindow(options: chrome.windows.CreateData): Promise { - return new Promise((resolve) => - chrome.windows.create(options, (window) => { - resolve(window); - }), - ); + return new Promise((resolve) => { + chrome.windows.create(options, async (newWindow) => { + if (!BrowserApi.isSafariApi) { + return resolve(newWindow); + } + // Safari doesn't close the default extension popup when a new window is created so we need to + // manually trigger the close by focusing the main window after the new window is created + const allWindows = await new Promise((resolve) => { + chrome.windows.getAll({ windowTypes: ["normal"] }, (windows) => resolve(windows)); + }); + + const mainWindow = allWindows.find((window) => window.id !== newWindow.id); + + // No main window found, resolve the new window + if (mainWindow == null || !mainWindow.id) { + return resolve(newWindow); + } + + // Focus the main window to close the extension popup + chrome.windows.update(mainWindow.id, { focused: true }, () => { + // Refocus the newly created window + chrome.windows.update(newWindow.id, { focused: true }, () => { + resolve(newWindow); + }); + }); + }); + }); } /**