From 70e73df81b8be5593b47f690b356c24f783cc6da Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 2 Nov 2023 11:40:21 -0400 Subject: [PATCH] Fix playlist media compatibility on Youtube Allow WebKit to reload the page automatically. --- .../Browser/BrowserViewController.swift | 8 +++----- .../PlaylistCacheLoader.swift | 14 ++++++++++++- Sources/Brave/Frontend/Browser/Tab.swift | 3 ++- .../PlaylistSettingsViewController.swift | 20 +++++++++---------- .../Scripts/Paged/PlaylistSwizzlerScript.js | 18 +++++++++++------ Sources/Playlist/PlaylistPreferences.swift | 2 +- 6 files changed, 40 insertions(+), 25 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController.swift b/Sources/Brave/Frontend/Browser/BrowserViewController.swift index 5164079761d..5d2e92653b0 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController.swift @@ -3150,11 +3150,9 @@ extension BrowserViewController: PreferencesObserver { Preferences.Rewards.rewardsToggledOnce.key: updateRewardsButtonState() case Preferences.Playlist.webMediaSourceCompatibility.key: - if UIDevice.isIpad { - tabManager.allTabs.forEach { - $0.setScript(script: .playlistMediaSource, enabled: Preferences.Playlist.webMediaSourceCompatibility.value) - $0.webView?.reload() - } + tabManager.allTabs.forEach { + $0.setScript(script: .playlistMediaSource, enabled: Preferences.Playlist.webMediaSourceCompatibility.value) + $0.webView?.reload() } case Preferences.General.mediaAutoBackgrounding.key: tabManager.allTabs.forEach { diff --git a/Sources/Brave/Frontend/Browser/Playlist/Managers & Cache/PlaylistCacheLoader.swift b/Sources/Brave/Frontend/Browser/Playlist/Managers & Cache/PlaylistCacheLoader.swift index 556078eb5e0..97cb1b376f4 100644 --- a/Sources/Brave/Frontend/Browser/Playlist/Managers & Cache/PlaylistCacheLoader.swift +++ b/Sources/Brave/Frontend/Browser/Playlist/Managers & Cache/PlaylistCacheLoader.swift @@ -36,7 +36,7 @@ class LivePlaylistWebLoader: UIView, PlaylistWebLoader { }, type: .private ).then { $0.createWebview() - $0.setScript(script: .playlistMediaSource, enabled: Preferences.Playlist.webMediaSourceCompatibility.value) + $0.setScript(script: .playlistMediaSource, enabled: true) $0.webView?.scrollView.layer.masksToBounds = true } @@ -263,6 +263,18 @@ extension LivePlaylistWebLoader: WKNavigationDelegate { } func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { + // There is a bug on some sites or something where the page may load TWICE OR there is a bug in WebKit where the page fails to load + // Either way, WebKit returns _WKRecoveryAttempterErrorKey with a WKReloadFrameErrorRecoveryAttempter + // Then it automatically reloads the page. In this case, we don't want to error and cancel loading and show the user an alert + // We want to continue waiting for the page to load and a proper response to come to us. + // If there is a real error, then we handle it and display an alert to the user. + if let error = error as? NSError { + if error.userInfo["_WKRecoveryAttempterErrorKey"] == nil { + self.handler?(nil) + } + return + } + self.handler?(nil) } diff --git a/Sources/Brave/Frontend/Browser/Tab.swift b/Sources/Brave/Frontend/Browser/Tab.swift index 5e63a6d3b5d..f5225749763 100644 --- a/Sources/Brave/Frontend/Browser/Tab.swift +++ b/Sources/Brave/Frontend/Browser/Tab.swift @@ -398,7 +398,8 @@ class Tab: NSObject { .cookieBlocking: Preferences.Privacy.blockAllCookies.value, .mediaBackgroundPlay: Preferences.General.mediaAutoBackgrounding.value, .nightMode: Preferences.General.nightModeEnabled.value, - .deAmp: Preferences.Shields.autoRedirectAMPPages.value + .deAmp: Preferences.Shields.autoRedirectAMPPages.value, + .playlistMediaSource: Preferences.Playlist.webMediaSourceCompatibility.value, ] userScripts = Set(scriptPreferences.filter({ $0.value }).map({ $0.key })) diff --git a/Sources/Brave/Frontend/Settings/Features/PlaylistSettingsViewController.swift b/Sources/Brave/Frontend/Settings/Features/PlaylistSettingsViewController.swift index d30440c5f65..ae313e946b1 100644 --- a/Sources/Brave/Frontend/Settings/Features/PlaylistSettingsViewController.swift +++ b/Sources/Brave/Frontend/Settings/Features/PlaylistSettingsViewController.swift @@ -156,17 +156,15 @@ class PlaylistSettingsViewController: TableViewController { dataSource.sections.append(sideSelectionSection) } - if UIDevice.isIpad { - dataSource.sections.append( - Section( - rows: [ - .boolRow( - title: Strings.PlayList.playlistWebCompatibilityTitle, - detailText: Strings.PlayList.playlistWebCompatibilityDescription, - option: Preferences.Playlist.webMediaSourceCompatibility) - ]) - ) - } + dataSource.sections.append( + Section( + rows: [ + .boolRow( + title: Strings.PlayList.playlistWebCompatibilityTitle, + detailText: Strings.PlayList.playlistWebCompatibilityDescription, + option: Preferences.Playlist.webMediaSourceCompatibility) + ]) + ) dataSource.sections.append( Section( diff --git a/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/PlaylistSwizzlerScript.js b/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/PlaylistSwizzlerScript.js index a27afd761f7..9593607afc4 100644 --- a/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/PlaylistSwizzlerScript.js +++ b/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Paged/PlaylistSwizzlerScript.js @@ -4,12 +4,18 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. // Stub out the MediaSource API so video players do not attempt to use `blob` for streaming -if (window.MediaSource || window.WebKitMediaSource || window.HTMLMediaElement && HTMLMediaElement.prototype.webkitSourceAddId) { - window.MediaSource = null; - window.WebKitMediaSource = null; - //HTMLMediaElement.prototype.webkitSourceAddId = null; - //window.SourceBuffer = null; - + +if (window.MediaSource || window.WebKitMediaSource || window.ManagedMediaSource || (window.HTMLMediaElement && HTMLMediaElement.prototype.webkitSourceAddId)) { delete window.MediaSource; delete window.WebKitMediaSource; + + // This API is only availale in iOS 17.1+ and only available in WebKit atm. The proposal to get it in all browsers is currently still open. + delete window.ManagedMediaSource; + +// window.MediaSource = undefined; +// window.WebKitMediaSource = undefined; +// window.ManagedMediaSource = undefined; +// +// HTMLMediaElement.prototype.webkitSourceAddId = undefined; +// window.SourceBuffer = undefined; } diff --git a/Sources/Playlist/PlaylistPreferences.swift b/Sources/Playlist/PlaylistPreferences.swift index 271edf04f37..feac41d3dfe 100644 --- a/Sources/Playlist/PlaylistPreferences.swift +++ b/Sources/Playlist/PlaylistPreferences.swift @@ -38,7 +38,7 @@ extension Preferences { /// The Option to download video yes / no / only wi-fi public static let autoDownloadVideo = Option(key: "playlist.autoDownload", default: PlayListDownloadType.on.rawValue) /// The Option to disable playlist MediaSource web-compatibility - public static let webMediaSourceCompatibility = Option(key: "playlist.webMediaSourceCompatibility", default: UIDevice.isIpad) + public static let webMediaSourceCompatibility = Option(key: "playlist.webMediaSourceCompatibility", default: false) /// The option to start the playback where user left-off public static let playbackLeftOff = Option(key: "playlist.playbackLeftOff", default: true) /// The option to disable long-press-to-add-to-playlist gesture.