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

Commit

Permalink
Fix #8350: Fix Playlist not working on Youtube at all (#8352)
Browse files Browse the repository at this point in the history
Fix playlist media compatibility on Youtube
Allow WebKit to reload the page automatically.
  • Loading branch information
Brandon-T committed Nov 3, 2023
1 parent 8143bb1 commit de19f72
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
8 changes: 3 additions & 5 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/Brave/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion Sources/Playlist/PlaylistPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension Preferences {
/// The Option to download video yes / no / only wi-fi
public static let autoDownloadVideo = Option<String>(key: "playlist.autoDownload", default: PlayListDownloadType.on.rawValue)
/// The Option to disable playlist MediaSource web-compatibility
public static let webMediaSourceCompatibility = Option<Bool>(key: "playlist.webMediaSourceCompatibility", default: UIDevice.isIpad)
public static let webMediaSourceCompatibility = Option<Bool>(key: "playlist.webMediaSourceCompatibility", default: false)
/// The option to start the playback where user left-off
public static let playbackLeftOff = Option<Bool>(key: "playlist.playbackLeftOff", default: true)
/// The option to disable long-press-to-add-to-playlist gesture.
Expand Down

0 comments on commit de19f72

Please sign in to comment.