diff --git a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift index 05160f8381..13afd90eb0 100644 --- a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift +++ b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift @@ -936,6 +936,16 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling { return false } + // Allow Youtube's internal navigation when DuckPlayer is enabled and user is watching on Youtube + // This is to prevent DuckPlayer from interfering with Youtube's internal navigation for search and settings + // https://app.asana.com/0/1204099484721401/1208930843675395/f + if let (destinationVideoID, _) = url.youtubeVideoParams, + let (originVideoID, _) = webView.url?.youtubeVideoParams, + destinationVideoID == originVideoID, + duckPlayerMode == .enabled { + return false + } + // Redirect to Duck Player if enabled if url.isYoutubeWatch && duckPlayerMode == .enabled && !isDuckPlayerRedirect(url: url) { redirectToDuckPlayerVideo(url: url, webView: webView) diff --git a/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift b/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift index 4b968fcd68..3ae7bc73ea 100644 --- a/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift +++ b/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift @@ -582,4 +582,24 @@ class DuckPlayerNavigationHandlerTests: XCTestCase { XCTAssertNil(tabNavigator.openedURL, "No new tabs should open") } + @MainActor + func testHandleDelegateNavigation_YoutubeWatchURLWithDuckPlayerEnabledAndSameVideoNavigation_ReturnsFalse() async { + // Arrange + let youtubeURL = URL(string: "https://www.youtube.com/watch?v=abc123")! + let youtubeInternalURL = URL(string: "https://www.youtube.com/watch?v=abc123&settings")! + let request = URLRequest(url: youtubeURL) + let mockFrameInfo = MockFrameInfo(isMainFrame: true) + let navigationAction = MockNavigationAction(request: request, targetFrame: mockFrameInfo) + playerSettings.mode = .enabled + featureFlagger.enabledFeatures = [.duckPlayer, .duckPlayerOpenInNewTab] + + mockWebView.setCurrentURL(youtubeInternalURL) + + // Act + let shouldCancel = handler.handleDelegateNavigation(navigationAction: navigationAction, webView: mockWebView) + + // Assert + XCTAssertFalse(shouldCancel, "Expected navigation NOT to be cancelled as it's Youtube Internal navigation") + } + }