Skip to content

Commit

Permalink
using mutation observer
Browse files Browse the repository at this point in the history
  • Loading branch information
tapanmodh committed Aug 9, 2024
1 parent 28e5128 commit ccf5518
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
2 changes: 1 addition & 1 deletion dist/resources.json

Large diffs are not rendered by default.

54 changes: 14 additions & 40 deletions resources/brave-youtube-quality.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,29 @@
(function() {
const IS_MOBILE_YOUTUBE = window.location.hostname == 'm.youtube.com';
const IS_ANDROID = window.navigator.userAgent.indexOf('Android') > -1;

let intervalId = 0;
if (IS_ANDROID && IS_MOBILE_YOUTUBE && typeof isYtHdQualityPlaybackEnabled !== 'undefined' && isYtHdQualityPlaybackEnabled) {

if (IS_ANDROID && IS_MOBILE_YOUTUBE && typeof isYoutubeHdQualityPlaybackEnabled !== 'undefined' && isYoutubeHdQualityPlaybackEnabled) {
navigation.addEventListener('navigate', (event) => {
const url = new URL(event.destination.url);
if (url.pathname && url.pathname === '/watch' && url.search && url.searchParams.get('v')) {
handlePlaybackVideoQuality();
const mutationObserver = new MutationObserver(() => {
let player = document.getElementById('movie_player');
if (player && typeof player.getAvailableQualityLevels !== 'undefined') {
setPlaybackVideoQuality(player, 'hd720');
mutationObserver.disconnect();
}
});
mutationObserver.observe(document.body, { childList: true, subtree: true });
}
});

function setPlaybackVideoQuality(player, quality) {
if (!player || typeof player.getAvailableQualityLevels === 'undefined') {
return false;
}

let availableQualities = player.getAvailableQualityLevels();
if (availableQualities && availableQualities.length > 0) {
if (player.setPlaybackQualityRange) {
player.setPlaybackQualityRange(quality);
return true;
}

if (player.setPlaybackQuality) {
player.setPlaybackQuality(quality);
return true;
}
if (player.setPlaybackQualityRange) {
player.setPlaybackQualityRange(quality);
} else if (player.setPlaybackQuality) {
player.setPlaybackQuality(quality);
}

return false;
}

function handlePlaybackVideoQuality() {
let attemptCount = 0;
const maxAttempts = 5;
clearInterval(intervalId);
intervalId = setInterval(function() {
try {
let player = document.getElementById('movie_player');
if (attemptCount++ > maxAttempts) {
clearInterval(intervalId);
return;
}
if (setPlaybackVideoQuality(player, 'hd720')) {
clearInterval(intervalId);
}
} catch (error) {
clearInterval(intervalId);
}
}, 500)
}
}
})()

0 comments on commit ccf5518

Please sign in to comment.