Skip to content

Commit

Permalink
Fixed percentage shown when sorting shorts (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored May 31, 2024
1 parent 640c4d8 commit c0311ed
Show file tree
Hide file tree
Showing 7 changed files with 3,788 additions and 12,381 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Changelog

## v3.1.4-beta
## v3.1.4

<!--Releasenotes start-->
- Fixed an animation in the popup.
- Fixed the progress percentage displayed on the button when ignoring or only shuffling from shorts.
<!--Releasenotes end-->

## v3.1.3
Expand Down
16,124 changes: 3,759 additions & 12,365 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "random-youtube-video",
"version": "3.1.3",
"version": "3.1.4",
"description": "Customize, shuffle and play random videos from any YouTube channel.",
"scripts": {
"dev": "concurrently \"npm run dev:chromium\" \"npm run dev:firefox\"",
Expand Down
1 change: 0 additions & 1 deletion src/html/shufflingPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ async function shuffleButtonClicked() {
setDOMTextWithDelay(shuffleButtonTextElement, "Still shuffling...", 20000, () => { return ((shuffleButtonTextElement.innerText === "Should be done soon..." || shuffleButtonTextElement.innerText === "Fetching: 100%")); });
}

// await delay(100000);
await chooseRandomVideo(configSync.currentChannelId, true, shuffleButtonTextElement);

// Focus this tab when the shuffle completes
Expand Down
18 changes: 13 additions & 5 deletions src/shuffleVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
} from "./utils.js";
import { configSync, setSyncStorageValue, getUserQuotaRemainingToday } from "./chromeStorage.js";

// The time when the shuffle started
let shuffleStartTime = null;

// --------------- Public ---------------
// Chooses a random video uploaded on the current YouTube channel
export async function chooseRandomVideo(channelId, firedFromPopup, progressTextElement) {
Expand All @@ -30,6 +33,8 @@ export async function chooseRandomVideo(channelId, firedFromPopup, progressTextE
}, 20000);
/* c8 ignore stop */

shuffleStartTime = new Date();

// Each user has a set amount of quota they can use per day.
// If they exceed it, they need to provide a custom API key, or wait until the quota resets the next day
let userQuotaRemainingToday = await getUserQuotaRemainingToday();
Expand Down Expand Up @@ -824,10 +829,6 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
if (playlistInfo["videos"]["unknownType"][randomVideo] !== undefined) {
const videoIsShort = await isShort(randomVideo);

// We display either the percentage of videos processed or the percentage of videos chosen (vs. needed), whichever is higher
const percentage = Math.max(Math.round(chosenVideos.length / numVideosToChoose * 100), Math.round(numVideosProcessed / initialTotalNumVideos * 100));
updateProgressTextElement(progressTextElement, `\xa0Sorting: ${percentage}%`, `${percentage}%`);

// What follows is dependent on if the video is a short or not, and the user's settings
// Case 1: !isShort && ignoreShorts => Success
if (!videoIsShort && configSync.shuffleIgnoreShortsOption == "2") {
Expand Down Expand Up @@ -879,7 +880,7 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
// We need to decrement i, as we did not choose a video in this iteration
i--;

/* c8 ignore start - This should never happen, but we want to test it anyway */
/* c8 ignore start - This should never happen */
} else {
throw new RandomYoutubeVideoError(
{
Expand All @@ -897,6 +898,13 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
chosenVideos.push(randomVideo);
videosToShuffle.splice(videosToShuffle.indexOf(randomVideo), 1);
}

// Only if the shuffle started more than 1 second ago, update the button text
if (new Date() - shuffleStartTime > 1000) {
// We display either the percentage of videos processed or the percentage of videos chosen (vs. needed), whichever is higher
const percentage = Math.max(Math.round(chosenVideos.length / numVideosToChoose * 100), Math.round(numVideosProcessed / initialTotalNumVideos * 100));
updateProgressTextElement(progressTextElement, `\xa0Sorting: ${percentage}%`, `${percentage}%`);
}
} else {
// We are not ignoring shorts and the video exists
chosenVideos.push(randomVideo);
Expand Down
4 changes: 2 additions & 2 deletions static/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Random YouTube Video",
"description": "Customize, shuffle and play random videos from any YouTube channel.",
"version": "3.1.3",
"version_name": "3.1.4-beta",
"version": "3.1.4",
"version_name": "3.1.4",
"manifest_version": 3,
"content_scripts": [
{
Expand Down
17 changes: 11 additions & 6 deletions test/shuffleVideo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('shuffleVideo', function () {
// The error is that there is no channelId
await chooseRandomVideo(null, false, domElement);
} catch (error) {
// We do no validation here, as that's not the point of this test
}
expect(configSync.userQuotaRemainingToday).to.be(199);
});
Expand Down Expand Up @@ -589,8 +590,7 @@ describe('shuffleVideo', function () {

beforeEach(function () {
date = new Date();
date.setMonth(3);
date.setDate(1);
date.setMonth(3, 1);
clock = sinon.useFakeTimers(date.getTime());
});

Expand All @@ -599,11 +599,16 @@ describe('shuffleVideo', function () {
});

it('should open a rickroll video if the user has not been rickrolled yet (openInNewTab option)', async function () {
// Set the last rickroll date to null, so the user has not been rickrolled yet
// Set the last rickroll date to the default, so the user has not been rickrolled yet
configSync.wasLastRickRolledInYear = '1970';
configSync.shuffleOpenInNewTabOption = true;

await chooseRandomVideo(input.playlistId, false, domElement);
try {
await chooseRandomVideo(input.playlistId, false, domElement);
} catch (error) {
// This is an error thrown from the jsdom library we use to mock browser behaviour
expect(error.message).to.contain('Not implemented: navigation (except hash changes)');
}

expect(windowOpenStub.calledTwice).to.be(true);
expect(windowOpenStub.args[1][0]).to.be('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
Expand All @@ -612,7 +617,7 @@ describe('shuffleVideo', function () {
});

it('should open a rickroll video if the user has not been rickrolled yet (!openInNewTab option)', async function () {
// Set the last rickroll date to null, so the user has not been rickrolled yet
// Set the last rickroll date to the default, so the user has not been rickrolled yet
configSync.wasLastRickRolledInYear = '1970';
configSync.shuffleOpenInNewTabOption = false;

Expand Down Expand Up @@ -777,7 +782,7 @@ describe('shuffleVideo', function () {
});
}
} else if (config.shuffleIgnoreShortsOption == "1") {
if (!input.playlistId.includes('LocalPlaylistContainsNoShorts') && !input.playlistId.includes('LocalPlaylistContainsOnlyShorts')) {
if (!input.playlistId.includes('LocalPlaylistContainsNoShorts') && !input.playlistId.includes('LocalPlaylistContainsOnlyShorts') && !input.playlistId.includes('LocalPlaylistContainsNoVideos') && !input.playlistId.includes('MultipleNewVideosUploaded')) {
// This works because we choose more videos than there are only videos OR shorts in the playlist, so there will always be one of each
// If this suddenly starts failing, it may be that that assumption was changed
it('should be able to choose both shorts and videos', async function () {
Expand Down

0 comments on commit c0311ed

Please sign in to comment.