Skip to content

Commit

Permalink
Merge e417660 into 70de732
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Nov 25, 2023
2 parents 70de732 + e417660 commit 91a51a6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

<!--Releasenotes start-->
- Added a new option to shuffle only from shorts.
- Added some animations to the custom API key input field, and made it more clear when the input is invalid.
- Fixed bugs that would prevent some initialization logic to run whenever the extension is updated.
- Reduced the time it takes to shuffle, as the extension now uses a more sophisticated way to decide whether or not to check if a video has been deleted or not.
- The popup now feels smoother in some places, and will make it clearer when some inputs are invalid.
- Fixed bugs that would prevent some initialization logic to run after the extension is updated.
<!--Releasenotes end-->

## v2.3.0
Expand Down
26 changes: 22 additions & 4 deletions src/shuffleVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,27 @@ async function getPlaylistSnippetFromAPI(playlistId, pageToken, APIKey, isCustom
}

// ---------- Utility ----------
async function testVideoExistence(videoId) {
let videoExists;
async function testVideoExistence(videoId, uploadTime) {
let videoAgeInDays = Math.floor((new Date() - new Date(uploadTime)) / (1000 * 60 * 60 * 24));

let checkProbability;
// For very new videos, we have just fetched them (we check every 48 hours), so they are very likely not deleted
if (videoAgeInDays <= 2) {
checkProbability = 0;
} else if (videoAgeInDays <= 7) {
checkProbability = 1;
} else if (videoAgeInDays <= 60) {
checkProbability = 0.5;
} else {
checkProbability = 0.2;
}

if (Math.random() > checkProbability) {
console.log("Skipping check if video is deleted or not.");
return true;
}

let videoExists;
try {
let response = await fetch(`https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=${videoId}&format=json`, {
method: "HEAD"
Expand Down Expand Up @@ -734,7 +752,7 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
randomVideo = videosToShuffle[Math.floor(Math.random() * videosToShuffle.length)];

// If the video does not exist, remove it from the playlist and choose a new one, until we find one that exists
if (!await testVideoExistence(randomVideo)) {
if (!await testVideoExistence(randomVideo, allVideos[randomVideo])) {
encounteredDeletedVideos = true;
// Update the database by removing the deleted videos there as well
shouldUpdateDatabase = true;
Expand Down Expand Up @@ -767,7 +785,7 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
}
}
/* c8 ignore stop */
} while (!await testVideoExistence(randomVideo))
} while (!await testVideoExistence(randomVideo, allVideos[randomVideo]))
}

// 0 = only shorts, 1 = no option set (shorts are included), 2 = ignore shorts
Expand Down
2 changes: 1 addition & 1 deletion static/html/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h3>General Video Settings</h3>

<!-- Shuffling: Open in new tab option toggle -->
<div class="toggle optionsRow-item">
<label id="shuffleReUseNewTabOption" title="If this is enabled, the first new tab opened by this extension when shuffling will be reused, as long as it is still on a youtube.com page.">
<label id="shuffleReUseNewTabOption" title="If this is enabled, the first new tab opened by the extension when shuffling will be reused, as long as it is still on a youtube subpage.">
Reuse new tab
<input type="checkbox" id="shuffleReUseNewTabOptionToggle" />
<span class="slider"></span>
Expand Down
17 changes: 15 additions & 2 deletions test/chromeStorage.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import expect from 'expect.js';

import { configSync, setSyncStorageValue, getUserQuotaRemainingToday } from '../src/chromeStorage.js';
import { configSync, setSyncStorageValue, removeSyncStorageValue, getUserQuotaRemainingToday } from '../src/chromeStorage.js';
import { configSyncDefaults } from '../src/config.js';

describe('chromeStorage', function () {
Expand Down Expand Up @@ -41,7 +41,6 @@ describe('chromeStorage', function () {
});

context('setSyncStorageValue()', function () {

it('should set the value in the configSync object', async function () {
await setSyncStorageValue("testKey1", "testValue1");

Expand Down Expand Up @@ -75,6 +74,20 @@ describe('chromeStorage', function () {
});
});

context('removeSyncStorageValue()', function () {

it('should remove the value from the configSync object', async function () {
await setSyncStorageValue("testKey4", "testValue4");

expect(configSync).to.have.key("testKey4");
expect(configSync.testKey4).to.be("testValue4");

await removeSyncStorageValue("testKey4");

expect(configSync).to.not.have.key("testKey4");
});

});
context('getUserQuotaRemainingToday()', function () {

it('should return the number of requests the user can still make to the Youtube API today', async function () {
Expand Down
20 changes: 10 additions & 10 deletions test/playlistPermutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,23 @@ const defaultLocalShorts = {
"LOC_S_00002": daysAgo(4).substring(0, 10),
"LOC_S_00003": daysAgo(10).substring(0, 10),
"LOC_S_00004": sixDaysAgo.substring(0, 10),
"LOC_S_00005": daysAgo(12).substring(0, 10),
"LOC_S_00005": daysAgo(72).substring(0, 10),
};

const defaultLocalVideos = {
"LOC_V_00006": threeDaysAgo.substring(0, 10),
"LOC_V_00007": daysAgo(4).substring(0, 10),
"LOC_V_00008": daysAgo(10).substring(0, 10),
"LOC_V_00009": sixDaysAgo.substring(0, 10),
"LOC_V_00010": daysAgo(12).substring(0, 10),
"LOC_V_00010": daysAgo(72).substring(0, 10),
};

const defaultLocalDeletedVideos = {
"DEL_LOC_S_1": fourteenDaysAgo.substring(0, 10),
"DEL_LOC_S_2": fourteenDaysAgo.substring(0, 10),
"DEL_LOC_V_3": fourteenDaysAgo.substring(0, 10),
"DEL_LOC_V_4": fourteenDaysAgo.substring(0, 10),
"DEL_LOC_V_5": fourteenDaysAgo.substring(0, 10)
"DEL_LOC_S_1": threeDaysAgo.substring(0, 10),
"DEL_LOC_S_2": threeDaysAgo.substring(0, 10),
"DEL_LOC_V_3": threeDaysAgo.substring(0, 10),
"DEL_LOC_V_4": threeDaysAgo.substring(0, 10),
"DEL_LOC_V_5": threeDaysAgo.substring(0, 10)
};

const defaultDBVideos = {
Expand All @@ -273,9 +273,9 @@ const defaultDBVideos = {
};

const defaultDBDeletedVideos = {
"DEL_DB_V_01": fourteenDaysAgo.substring(0, 10),
"DEL_DB_V_02": fourteenDaysAgo.substring(0, 10),
"DEL_DB_V_03": fourteenDaysAgo.substring(0, 10)
"DEL_DB_V_01": threeDaysAgo.substring(0, 10),
"DEL_DB_V_02": threeDaysAgo.substring(0, 10),
"DEL_DB_V_03": threeDaysAgo.substring(0, 10)
};

// The YT API returns a full-length timestamp
Expand Down

0 comments on commit 91a51a6

Please sign in to comment.