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

Commit

Permalink
Update to wait until player has resolutions available
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin committed Apr 11, 2019
1 parent c17021c commit dd920b4
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions app/src/main/assets/web_extensions/youtube_webcompat/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
hd: false,
quality: 1440,
log: qs.get('mozDebug') ? getTruthyQS('mozDebug') : true,
retryAttempts: parseInt(qs.get('retryAttempts') || qs.get('retryattempts') || '5', 10),
retryAttempts: parseInt(qs.get('retryAttempts') || qs.get('retryattempts') || '10', 10),
retryTimeout: parseInt(qs.get('retryTimeout') || qs.get('retrytimeout') || '500', 10)
};

Expand All @@ -45,21 +45,42 @@
attempts = 1;
}
if (attempts >= prefs.retryAttempts) {
logError(`Giving up trying to increase resolution after ${prefs.retryAttempts} attempts.`);
return;
}

const player = document.getElementById('movie_player');
if (state !== 1 || !player) {
let player = document.getElementById('movie_player');
let reason = 'unknown';
if (state !== 1) {
reason = 'invalid state';
} else if (!player) {
reason = 'player not found';
} else if (!player.wrappedJSObject) {
reason = 'player.wrappedJSObject not found';
player = null;
} else if (!player.wrappedJSObject.getAvailableQualityLevels) {
reason = 'player.wrappedJSObject.getAvailableQualityLevels not found';
player = null;
}

if (!player) {
logWarn(`Cannot find player because ${reason}. attempts: ${attempts}`);
attempts++;
retryTimeout = setTimeout(() => {
ytImprover(state, attempts);
}, prefs.retryInterval);
}, prefs.retryTimeout);
return;
}

player = player.wrappedJSObject;

const levels = player.getAvailableQualityLevels();
if (!levels || !levels.length) {
logWarn('Cannot read `player.getAvailableQualityLevels()`');
logWarn(`Cannot read 'player.getAvailableQualityLevels()' attempts: ${attempts}`);
attempts++;
retryTimeout = setTimeout(() => {
ytImprover(state, attempts);
}, prefs.retryTimeout);
return;
}

Expand Down Expand Up @@ -149,6 +170,8 @@
try {
player.setPlaybackQualityRange(newBestQuality, newBestQuality);
} catch (e) {
logError(`Failed to call 'player.setPlaybackQualityRange(${newBestQuality}, ${newBestQuality})' with exception: `, e);
return;
}

log(`Changed quality from "${currentQuality}" to "${newBestQuality}"`);
Expand Down

0 comments on commit dd920b4

Please sign in to comment.