Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Add OpenInYouTube Nav #3317

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@
// Subscription

case subscriptionEnvironment = "subscription.environment"

Check failure on line 219 in DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
// Duck Player

Check failure on line 221 in DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
case lastRenderedVideoID = "duckplayer.lastRenderedVideoID"
}

enum RemovedKeys: String, CaseIterable {
Expand Down
45 changes: 44 additions & 1 deletion DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
private let onboardingDecider: DuckPlayerOnboardingDecider
private var shouldSelectNextNewTab: Bool?
private let experimentManager: OnboardingExperimentManager

Check failure on line 59 in DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
private struct Constants {
static let watchInYoutubeVideoParameter = "v"
static let watchInYoutubePath = "openInYoutube"
static let duckPlayerScheme = URL.NavigationalScheme.duck.rawValue
}

init(duckPlayer: DuckPlayer,
isBurner: Bool,
Expand Down Expand Up @@ -85,6 +91,26 @@
}
}.store(in: &cancellables)
}

Check failure on line 94 in DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
private func isWatchInYouTubeURL(url: URL) -> Bool {
guard url.scheme == Constants.duckPlayerScheme,
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false),
urlComponents.path == "/\(Constants.watchInYoutubePath)" else {
return false
}
return true
}

Check failure on line 103 in DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
private func getYoutubeURLFromOpenInYoutubeLink(url: URL) -> URL? {
guard isWatchInYouTubeURL(url: url),
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false),
let videoParameterItem = urlComponents.queryItems?.first(where: { $0.name == Constants.watchInYoutubeVideoParameter }),
let id = videoParameterItem.value,
let newURL = URL.youtube(id, timestamp: nil).addingWatchInYoutubeQueryParameter() else {
return nil
}
return newURL
}

@MainActor
private func setUpYoutubeScriptsIfNeeded(for url: URL?) {
Expand Down Expand Up @@ -184,13 +210,29 @@
extension DuckPlayerTabExtension: NavigationResponder {

@MainActor
func decidePolicy(for navigationAction: NavigationAction, preferences: inout NavigationPreferences) async -> NavigationActionPolicy? {

Check failure on line 213 in DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Function should have complexity 10 or less; currently complexity is 11 (cyclomatic_complexity)
// only proceed when Private Player is enabled

guard duckPlayer.isAvailable, duckPlayer.mode != .disabled else {
return decidePolicyWithDisabledDuckPlayer(for: navigationAction)
}

// Handle Open in Youtube Links
// duck://player/openInYoutube?v=12345
if let newURL = getYoutubeURLFromOpenInYoutubeLink(url: navigationAction.url) {

// Attempt to open a clean YouTubeURL load in webView
guard let mainFrame = navigationAction.mainFrameTarget,
let (videoID, timestamp) = newURL.youtubeVideoParams else {
return .cancel
}

return .redirect(mainFrame) { navigator in
navigator.load(URLRequest(url: newURL.appendingPathComponent("lamadrequetepario")))
}

}

// session restoration will try to load real www.youtube-nocookie.com url
// we need to redirect it to custom duck:// scheme handler which will load
// www.youtube-nocookie.com as a simulated request
Expand Down Expand Up @@ -249,6 +291,7 @@

@MainActor
private func handleYoutubeNavigation(for navigationAction: NavigationAction) -> NavigationActionPolicy? {

Check failure on line 294 in DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
guard navigationAction.url.isYoutubeVideo,
let (videoID, timestamp) = navigationAction.url.youtubeVideoParams else {
return .next
Expand Down Expand Up @@ -285,7 +328,7 @@
let mainFrame = navigationAction.mainFrameTarget else {
return .cancel
}

Check failure on line 331 in DuckDuckGo/Tab/TabExtensions/DuckPlayerTabExtension.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
return .redirect(mainFrame) { navigator in
navigator.load(URLRequest(url: .youtube(videoID, timestamp: timestamp)))
}
Expand Down
Loading