Skip to content

Commit

Permalink
Better user setting validation on startup (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored May 25, 2024
1 parent 502a407 commit 6f04bd1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
23 changes: 22 additions & 1 deletion src/chromeStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { configSyncDefaults } from "./config.js";

// ----- Storage -----
// Validate the config in sync storage
await validateConfigSync();

export let configSync = await chrome.storage.sync.get();
Expand Down Expand Up @@ -60,4 +59,26 @@ async function validateConfigSync() {
await chrome.storage.sync.remove(key);
}
}

// Validate that dependent values are correct
// Custom API key must be set if the user has enabled the custom API key option
if (configSyncValues.useCustomApiKeyOption && !configSyncValues.customYoutubeApiKey) {
await chrome.storage.sync.set({ "useCustomApiKeyOption": false });
}
// If the user has no custom API key, they must have database sharing enabled
if (!configSyncValues.useCustomApiKeyOption && !configSyncValues.databaseSharingEnabledOption) {
await chrome.storage.sync.set({ "databaseSharingEnabledOption": true });
}

if (!configSyncValues.shuffleOpenInNewTabOption && configSyncValues.shuffleReUseNewTabOption) {
await chrome.storage.sync.set({ "shuffleReUseNewTabOption": false });
}

if (!(0 <= configSyncValues.shuffleIgnoreShortsOption && configSyncValues.shuffleIgnoreShortsOption <= 2)) {
await chrome.storage.sync.set({ "shuffleIgnoreShortsOption": 1 });
}

if (!(1 <= configSyncValues.shuffleNumVideosInPlaylist && configSyncValues.shuffleNumVideosInPlaylist <= 50)) {
await chrome.storage.sync.set({ "shuffleNumVideosInPlaylist": 10 });
}
}
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const configSyncDefaults = {
// 0 = only shorts, 1 = no option set (shorts are included), 2 = ignore shorts
"shuffleIgnoreShortsOption": 1,
"shuffleOpenAsPlaylistOption": true,
// How many random videos to add to a playlist
// How many random videos to add to a playlist (0-50)
"shuffleNumVideosInPlaylist": 10,
// If shuffled videos are opened in a new tab, save the tab ID of that tab here to reuse the tab when the user shuffles again
"shuffleTabId": null,
Expand Down
5 changes: 3 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
const oldLog = console.log;
console.log = function () {
// The last argument to console.log is a boolean that determines if the message should be shown in production
const showInProduction = arguments[arguments.length - 1] === true;
const showInProduction = (arguments.length > 1 && arguments[arguments.length - 1] === true);
// If we are either in development or the message should be shown in production, log the message
if (process.env.NODE_ENV !== 'production' || showInProduction) {
if (arguments[0] !== "[random-youtube-video]:") {
Array.prototype.unshift.call(arguments, '[random-youtube-video]:');
}
// Remove the showInProduction flag from the arguments so it doesn't get logged
if (typeof arguments[arguments.length - 1] === 'boolean') {
// Only remove the last argument if there are more than 2 arguments (prefix, normal argument, showInProduction flag)
if (arguments.length > 2 && typeof arguments[arguments.length - 1] === 'boolean') {
Array.prototype.pop.call(arguments);
}
oldLog.apply(this, arguments);
Expand Down

0 comments on commit 6f04bd1

Please sign in to comment.