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 authored and iccub committed Nov 3, 2023
1 parent 7a6dae7 commit e32f1e4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 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 @@ -3162,11 +3162,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 @@ -301,7 +301,7 @@ class PlaylistWebLoader: UIView {
}, 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 @@ -528,6 +528,18 @@ extension PlaylistWebLoader: 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 @@ -397,7 +397,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 @@ -162,17 +162,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;
}
Empty file.

0 comments on commit e32f1e4

Please sign in to comment.