Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #8272: Add setting to ensure YouTube links always load in Brave
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed Oct 18, 2023
1 parent 2cc24b1 commit 9df36c4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions App/iOS/Delegates/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// To avoid unexpected problems we clear all vpn keychain items.
// New set of keychain items will be created on purchase or iap restoration.
BraveVPN.clearCredentials()

// Always load YouTube in Brave for new users
Preferences.General.keepYouTubeInBrave.value = true
}

if UserReferralProgram.shared != nil {
Expand Down
14 changes: 11 additions & 3 deletions Sources/Brave/Frontend/Browser/TabManagerNavDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,17 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {
}
}

private func defaultAllowPolicy() -> WKNavigationActionPolicy {
private func defaultAllowPolicy(for navigationAction: WKNavigationAction) -> WKNavigationActionPolicy {
let isPrivateBrowsing = tabManager?.privateBrowsingManager.isPrivateBrowsing == true
if isPrivateBrowsing || !Preferences.General.followUniversalLinks.value {
func isYouTubeLoad() -> Bool {
guard let domain = navigationAction.request.mainDocumentURL?.baseDomain else {
return false
}
let domainsWithUniversalLinks: Set<String> = ["youtube.com", "youtu.be"]
return domainsWithUniversalLinks.contains(domain)
}
if isPrivateBrowsing || !Preferences.General.followUniversalLinks.value ||
(Preferences.General.keepYouTubeInBrave.value && isYouTubeLoad()) {
// Stop Brave from opening universal links by using the private enum value
// `_WKNavigationActionPolicyAllowWithoutTryingAppLink` which is defined here:
// https://github.com/WebKit/WebKit/blob/main/Source/WebKit/UIProcess/API/Cocoa/WKNavigationDelegatePrivate.h#L62
Expand All @@ -81,7 +89,7 @@ class TabManagerNavDelegate: NSObject, WKNavigationDelegate {

@MainActor
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences) async -> (WKNavigationActionPolicy, WKWebpagePreferences) {
var res = defaultAllowPolicy()
var res = defaultAllowPolicy(for: navigationAction)
var pref = preferences

for delegate in delegates {
Expand Down
3 changes: 3 additions & 0 deletions Sources/Brave/Frontend/ClientPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ extension Preferences {

/// Whether or not the app (in regular browsing mode) will follow universal links
static let followUniversalLinks = Option<Bool>(key: "general.follow-universal-links", default: true)

/// Whether or not the app will always load YouTube in Brave
public static let keepYouTubeInBrave = Option<Bool>(key: "general.follow-universal-links.youtube", default: false)

/// Whether or not the pull-to-refresh control is added to web views
static let enablePullToRefresh = Option<Bool>(key: "general.enable-pull-to-refresh", default: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import BraveUI

struct MediaSettingsView: View {
@ObservedObject var enableBackgroundAudio = Preferences.General.mediaAutoBackgrounding
@ObservedObject var keepYouTubeInBrave = Preferences.General.keepYouTubeInBrave

var body: some View {
Form {
Expand All @@ -22,6 +23,10 @@ struct MediaSettingsView: View {
.listRowBackground(Color(.secondaryBraveGroupedBackground))

Section(header: Text(Strings.Settings.youtube)) {
Toggle(isOn: $keepYouTubeInBrave.value) {
Text(Strings.Settings.openYouTubeInBrave)
}
.toggleStyle(SwitchToggleStyle(tint: .accentColor))
NavigationLink(destination: QualitySettingsView()) {
VStack(alignment: .leading) {
Text(Strings.Settings.highestQualityPlayback)
Expand Down
9 changes: 9 additions & 0 deletions Sources/BraveStrings/BraveStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,15 @@ extension Strings {
comment: "Header for the Youtube settings section"
)

public static let openYouTubeInBrave =
NSLocalizedString(
"settings.openYouTubeInBrave",
tableName: "BraveShared",
bundle: .module,
value: "Open YouTube links in Brave",
comment: "A toggle label which lets the user always open YouTube urls in Brave"
)

public static let highestQualityPlayback =
NSLocalizedString(
"settings.highestQualityPlayback",
Expand Down

0 comments on commit 9df36c4

Please sign in to comment.