diff --git a/DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift b/DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift index 65eb64e98a..2da642e537 100644 --- a/DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift +++ b/DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift @@ -213,16 +213,23 @@ extension DownloadsTabExtension: NavigationResponder { func enqueueDownload(_ download: WebKitDownload, withNavigationAction navigationAction: NavigationAction?) { let task = downloadManager.add(download, fromBurnerWindow: self.isBurner, delegate: self, destination: .auto) + var isMainFrameNavigationActionWithNoHistory: Bool { + guard let navigationAction, + navigationAction.isForMainFrame, + navigationAction.isTargetingNewWindow, + // webView has no navigation history (downloaded navigationAction has started from an empty state) + (navigationAction.redirectHistory?.first ?? navigationAction).fromHistoryItemIdentity == nil + else { return false } + return true + } + // If the download has started from a popup Tab - close it after starting the download // e.g. download button on this page: // https://en.wikipedia.org/wiki/Guitar#/media/File:GuitareClassique5.png - guard let navigationAction, - navigationAction.isForMainFrame, - navigationAction.isTargetingNewWindow, - let webView = download.webView, - // webView has no navigation history (downloaded navigationAction has started from an empty state) - (navigationAction.redirectHistory?.first ?? navigationAction).fromHistoryItemIdentity == nil - else { return } + guard let webView = download.webView, + isMainFrameNavigationActionWithNoHistory + // if converted from navigation response but no page was loaded + || navigationAction == nil && webView.backForwardList.currentItem == nil else { return } self.closeWebView(webView, afterDownloadTaskHasStarted: task) }