diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift index fdd5c182ea6..72d4f444943 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift @@ -1016,6 +1016,16 @@ extension BrowserViewController: WKUIDelegate { newTab.url = URL(string: "about:blank") toolbarVisibilityViewModel.toolbarState = .expanded + + // Wait until WebKit starts the request before selecting the new tab, otherwise the tab manager may + // restore it as if it was a dead tab. + var observation: NSKeyValueObservation? + observation = newTab.webView?.observe(\.url, changeHandler: { [weak self] webView, _ in + _ = observation // Silence write but not read warning + observation = nil + guard let self = self, let tab = self.tabManager[webView] else { return } + self.tabManager.selectTab(tab) + }) return newTab.webView } diff --git a/Sources/Brave/Frontend/Browser/TabManager.swift b/Sources/Brave/Frontend/Browser/TabManager.swift index cc5d92a00b6..df2cd8c781c 100644 --- a/Sources/Brave/Frontend/Browser/TabManager.swift +++ b/Sources/Brave/Frontend/Browser/TabManager.swift @@ -411,14 +411,6 @@ class TabManager: NSObject { @MainActor func addPopupForParentTab(_ parentTab: Tab, configuration: WKWebViewConfiguration) -> Tab { let popup = Tab(configuration: configuration, id: UUID(), type: parentTab.type, tabGeneratorAPI: tabGeneratorAPI) configureTab(popup, request: nil, afterTab: parentTab, flushToDisk: true, zombie: false, isPopup: true) - - // Wait momentarily before selecting the new tab, otherwise the parent tab - // may be unable to set `window.location` on the popup immediately after - // calling `window.open("")`. - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { - self.selectTab(popup) - } - return popup }