Skip to content

Commit

Permalink
Remember known shorts locally (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Nov 4, 2023
1 parent 89ed0d0 commit 31403be
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 237 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Changelog

## v2.2.4
## v2.3.0

<!--Releasenotes start-->
- Shuffling when excluding shorts is now a lot quicker if you have shuffled from the channel before, as the extension will remember which videos are shorts and skip them automatically.
- Added an additional message to the shuffle button if shuffling takes a bit longer due to ignoring shorts.
- Fixed a rare data inconsistency bug occurring with specific database values.
<!--Releasenotes end-->

## v2.2.4

- Fixed an alignment issue of the shuffle button on channel pages that was introduced with the latest update to the YouTube UI.
- Fixed some issues with the display of the shuffle button if the shuffle icon is not loaded in time.
<!--Releasenotes end-->

## v2.2.3

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Did you find any bugs with the version you tested? Please let me know by [openin
- The bundled extension files will be placed in the `dist/<browser>` directories.
- You can load the extension in your browser by following the instructions below.

#### Chrome/Chromium
#### Chromium

- Open the Extension Management page by navigating to `chrome://extensions`.
- Make sure that you have enabled developer mode.
Expand Down
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": "2.2.4",
"version": "2.3.0",
"description": "Play a random video uploaded on the current YouTube channel.",
"scripts": {
"dev": "concurrently \"npm run dev:chromium\" \"npm run dev:firefox\"",
Expand Down
6 changes: 3 additions & 3 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ chrome.runtime.onStartup.addListener(async function () {
console.log(`${((utilizedStorage / maxLocalStorage) * 100).toFixed(2)}% of local storage is used. (${utilizedStorage}/${maxLocalStorage} bytes)`);

if (maxLocalStorage * 0.9 < utilizedStorage) {
console.log("Local storage is over 90% utilized. Removing playlists that have not been accessed the longest...");
console.log("Local storage is over 90% utilized. Removing playlists that have not been accessed the longest to free up some space...");

// Get all playlists from local storage
const localStorageContents = await chrome.storage.local.get();

// We only need the keys that hold playlists, which is signified by the existence of the "videos" sub-key
const allPlaylists = Object.fromEntries(Object.entries(localStorageContents).filter(([k, v]) => v["videos"]));
const allPlaylists = Object.fromEntries(Object.entries(localStorageContents).filter(([key, value]) => value["videos"]));

// Sort the playlists by lastAccessedLocally value
const sortedPlaylists = Object.entries(allPlaylists).sort((a, b) => {
Expand Down Expand Up @@ -324,7 +324,7 @@ async function openVideoInTabWithId(tabId, videoUrl) {
// ---------- Local storage ----------
async function getFromLocalStorage(key) {
return await chrome.storage.local.get([key]).then((result) => {
if (result[key]) {
if (result[key] !== undefined) {
return result[key];
}
return null;
Expand Down
3 changes: 3 additions & 0 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ async function shuffleVideos() {
var hasBeenShuffled = false;
setDOMTextWithDelay(shuffleButtonTextElement, "\xa0Shuffling...", 1000, () => { return (shuffleButtonTextElement.innerText === "\xa0Shuffle" && !hasBeenShuffled); });
setDOMTextWithDelay(shuffleButtonTextElement, "\xa0Still on it...", 5000, () => { return (shuffleButtonTextElement.innerText === "\xa0Shuffling..." && !hasBeenShuffled); });
if (configSync.shuffleIgnoreShortsOption) {
setDOMTextWithDelay(shuffleButtonTextElement, "\xa0Sorting shorts...", 8000, () => { return (shuffleButtonTextElement.innerText === "\xa0Still on it..." && !hasBeenShuffled); });
}

await chooseRandomVideo(channelId, false, shuffleButtonTextElement);
hasBeenShuffled = true;
Expand Down
187 changes: 143 additions & 44 deletions src/shuffleVideo.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion static/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Random YouTube Video",
"description": "Play a random video uploaded on the current YouTube channel.",
"version": "2.2.4",
"version": "2.3.0",
"manifest_version": 3,
"content_scripts": [
{
Expand Down
343 changes: 193 additions & 150 deletions test/playlistPermutations.js

Large diffs are not rendered by default.

115 changes: 81 additions & 34 deletions test/shuffleVideo.test.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/testSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ chrome.runtime.sendMessage.callsFake((request) => {
// Only for the tests
case "setKeyInDB":
mockedDatabase[request.data.key] = request.data.val;
return "Key was removed from database.";
return "Key was set in the database (mocked for tests).";

case "getAPIKey":
return getAPIKey(false, request.data.useAPIKeyAtIndex);
Expand Down

0 comments on commit 31403be

Please sign in to comment.