Skip to content

Commit

Permalink
Fixed a bug occurring when a channel has no videos (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Mar 30, 2023
1 parent 5a146f6 commit 5bedf5f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 31 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Changelog

## v1.4.2
## v1.4.3

<!--Releasenotes start-->
- Fixed a bug occurring when a channel has no videos.
<!--Releasenotes end-->

## v1.4.2

- The shuffle button will now show the current progress when more than one request to the YouTube API needs to be made.
- The extension popup now shows the number of videos you have shuffled so far.
- Fixed a bug where the surprise wouldn't work for people with certain settings.
<!--Releasenotes end-->

## v1.4.1

Expand Down
9 changes: 2 additions & 7 deletions extension/js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async function channelDetectedAction(pageType, channelId, channelName) {
}

// When navigating from e.g. the homepage after an invalidated extension context, sometimes the config is not loaded correctly
if(!configSync) {
if (!configSync) {
window.location.reload();
}

Expand Down Expand Up @@ -146,12 +146,7 @@ The page will reload and you can try again.`)
}

// Alert the user about the error
alert(`Random YouTube Video:
${displayText}${error.message ? "\n" + error.message : ""}${error.reason ? "\n" + error.reason : ""}${error.solveHint ? "\n" + error.solveHint : ""}
${error.stack}`
);
alert(`Random YouTube Video:\n\n${displayText}${error.message ? "\n" + error.message : ""}${error.reason ? "\n" + error.reason : ""}${error.solveHint ? "\n" + error.solveHint : ""}${error.showTrace ? "\n\n" + error.stack : ""}`);

// Immediately display the error and stop other text changes
setDOMTextWithDelay(shuffleButtonTextElement, `&nbsp;${displayText}`, 0, changeToken, true);
Expand Down
2 changes: 1 addition & 1 deletion extension/js/popup/shufflingPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function shuffleButtonClicked() {
displayText = `Unknown Error`;
}

const errorMessage = `${error.message ?? ""}${error.reason ? "<br>" + error.reason : ""}${error.solveHint ? "<br>" + error.solveHint : ""}<br><br>${error.stack}`;
const errorMessage = `${error.message ?? ""}${error.reason ? "<br>" + error.reason : ""}${error.solveHint ? "<br>" + error.solveHint : ""}${error.showTrace ? "<br><br>" + error.stack : ""}`;

// Immediately display the error and stop other text changes
setDOMTextWithDelay(domElements.fetchPercentageNotice, displayText, 0, changeToken, true);
Expand Down
74 changes: 56 additions & 18 deletions extension/js/shuffleVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ async function chooseRandomVideo(channelId, firedFromPopup, progressTextElement)
const uploadsPlaylistId = channelId ? channelId.replace("UC", "UU") : null;
if (!uploadsPlaylistId) {
throw new RandomYoutubeVideoError(
code = "RYV-1",
message = "No channel-ID found.",
solveHint = "Please reload the page and try again. Please inform the developer if this keeps happening."
{
code: "RYV-1",
message: "No channel-ID found.",
solveHint: "Please reload the page and try again. Please inform the developer if this keeps happening.",
showTrace: false
}
);
}

Expand Down Expand Up @@ -203,9 +206,12 @@ async function getPlaylistFromAPI(playlistId, useAPIKeyAtIndex, userQuotaRemaini
if (!isCustomKey && userQuotaRemainingToday <= 0) {
console.log("You have exceeded your daily quota allocation for the YouTube API. You can try again tomorrow or provide a custom API key.");
throw new RandomYoutubeVideoError(
code = "RYV-4",
message = "You have exceeded your daily quota allocation for the YouTube API.",
solveHint = "You can try again tomorrow or provide a custom API key."
{
code: "RYV-4",
message: "You have exceeded your daily quota allocation for the YouTube API.",
solveHint: "You can try again tomorrow or provide a custom API key.",
showTrace: false
}
);
}

Expand Down Expand Up @@ -260,9 +266,12 @@ async function updatePlaylistFromAPI(playlistInfo, playlistId, useAPIKeyAtIndex,
if (!isCustomKey && userQuotaRemainingToday <= 0) {
console.log("You have exceeded your daily quota allocation for the YouTube API. You can try again tomorrow or provide a custom API key.");
throw new RandomYoutubeVideoError(
code = "RYV-4",
message = "You have exceeded your daily quota allocation for the YouTube API.",
solveHint = "You can try again tomorrow or provide a custom API key."
{
code: "RYV-4",
message: "You have exceeded your daily quota allocation for the YouTube API.",
solveHint: "You can try again tomorrow or provide a custom API key.",
showTrace: false
}
);
}

Expand Down Expand Up @@ -367,18 +376,34 @@ async function getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustom

if (keyIndex === originalKeyIndex) {
throw new RandomYoutubeVideoError(
code = "RYV-2",
message = "All API keys have exceeded the allocated quota.",
solveHint = "Please *immediately* inform the developer. You can try again tomorrow or provide a custom API key to immediately resolve this problem."
{
code: "RYV-2",
message: "All API keys have exceeded the allocated quota.",
solveHint: "Please *immediately* inform the developer. You can try again tomorrow or provide a custom API key to immediately resolve this problem.",
showTrace: false
}
);
}
} else {
throw new RandomYoutubeVideoError(
code = "RYV-5",
message = "Your custom API key has reached its daily quota allocation.",
solveHint = "You must have watched a lot of videos to have this happen, or are using the API key for something else as well. You need to wait until the quota is reset or use a different API key."
{
code: "RYV-5",
message: "Your custom API key has reached its daily quota allocation.",
solveHint: "You must have watched a lot of videos to have this happen, or are using the API key for something else as well. You need to wait until the quota is reset or use a different API key.",
showTrace: false
}
);
}
} else if (error instanceof YoutubeAPIError && error.code === 404 && error.reason === "playlistNotFound") {
throw new RandomYoutubeVideoError(
{
code: "RYV-6A",
message: "This channel has not uploaded any videos.",
showTrace: false
}
);
} else {
throw error;
}
}
}
Expand Down Expand Up @@ -418,9 +443,12 @@ async function getAPIKey(useAPIKeyAtIndex = null) {

if (!APIKey) {
throw new RandomYoutubeVideoError(
code = "RYV-3",
message = "There are no API keys available in the database, or they could not be fetched.",
solveHint = "Please *immediately* inform the developer."
{
code: "RYV-3",
message: "There are no API keys available in the database, or they could not be fetched.",
solveHint: "Please *immediately* inform the developer.",
showTrace: false
}
);
}

Expand Down Expand Up @@ -462,6 +490,16 @@ async function chooseRandomVideoFromPlaylist(playlistInfo, channelId, shouldUpda
randomVideo = videosToShuffle[Math.floor(Math.random() * videosToShuffle.length)];

console.log(`A new random video has been chosen: ${randomVideo}`);

if (randomVideo === undefined) {
throw new RandomYoutubeVideoError(
{
code: "RYV-6B",
message: "All previously uploaded videos on this channel were deleted - the channel does not have any uploads.",
showTrace: false
}
)
}
} while (!await testVideoExistence(randomVideo))

// Update the database by removing the deleted videos there as well
Expand Down
6 changes: 4 additions & 2 deletions extension/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,24 @@ class BooleanReference {
// ----- Errors -----

class RandomYoutubeVideoError extends Error {
constructor(code = "RYV-0", message = "", solveHint = "") {
constructor({ code = "RYV-0", message = "", solveHint = "", showTrace = true }) {
super(message);
this.code = code;
this.message = message;
this.solveHint = solveHint;
this.showTrace = showTrace;
this.name = "RandomYoutubeVideoError";
}
}

class YoutubeAPIError extends RandomYoutubeVideoError {
constructor(code = "YAPI-0", message = "", reason = "", solveHint = "") {
constructor(code = "YAPI-0", message = "", reason = "", solveHint = "", showTrace = true) {
super(message);
this.code = code;
this.message = message;
this.reason = reason;
this.solveHint = solveHint;
this.showTrace = showTrace;
this.name = "YoutubeAPIError";
}
}
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Random YouTube Video",
"description": "Play a random video uploaded on the current YouTube channel.",
"version": "1.4.2",
"version": "1.4.3",
"icons": {
"16": "images/icon-16-red.png",
"32": "images/icon-32-red.png",
Expand Down

0 comments on commit 5bedf5f

Please sign in to comment.