Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous UI/config refactoring #134

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 79 additions & 18 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,100 @@
const CONFIG_KEY = 'ytaf-configuration';
const defaultConfig = {
enableAdBlock: true,
enableSponsorBlock: true,
enableSponsorBlockSponsor: true,
enableSponsorBlockIntro: true,
enableSponsorBlockOutro: true,
enableSponsorBlockInteraction: true,
enableSponsorBlockSelfPromo: true,
enableSponsorBlockMusicOfftopic: true
};

let localConfig;

try {
localConfig = JSON.parse(window.localStorage[CONFIG_KEY]);
} catch (err) {
console.warn('Config read failed:', err);
localConfig = defaultConfig;

export const configOptions = new Map([
['enableAdBlock', { default: true, desc: 'Enable ad blocking' }],
['enableSponsorBlock', { default: true, desc: 'Enable SponsorBlock' }],
[
'enableSponsorBlockSponsor',
{ default: true, desc: 'Skip sponsor segments' }
],
['enableSponsorBlockIntro', { default: true, desc: 'Skip intro segments' }],
['enableSponsorBlockOutro', { default: true, desc: 'Skip outro segments' }],
[
'enableSponsorBlockInteraction',
{
default: true,
desc: 'Skip interaction reminder segments'
}
],
[
'enableSponsorBlockSelfPromo',
{
default: true,
desc: 'Skip self promotion segments'
}
],
[
'enableSponsorBlockMusicOfftopic',
{
default: true,
desc: 'Skip music and off-topic segments'
}
]
]);

const defaultConfig = (() => {
let ret = {};
for (const [k, v] of configOptions) {
ret[k] = v.default;
}
return ret;
})();

function loadStoredConfig() {
const storage = window.localStorage.getItem(CONFIG_KEY);

if (storage === null) {
console.info('Config not set; using defaults.');
return null;
}

try {
return JSON.parse(storage);
} catch (err) {
console.warn('Error parsing stored config:', err);
return null;
}
}

// Use defaultConfig as a prototype so writes to localConfig don't change it.
let localConfig = loadStoredConfig() ?? Object.create(defaultConfig);

function configExists(key) {
return configOptions.has(key);
}

export function getConfigDesc(key) {
if (!configExists(key)) {
throw new Error('tried to get desc for unknown config key:', key);
}

return configOptions.get(key).desc;
}

export function configRead(key) {
if (!configExists(key)) {
throw new Error('tried to read unknown config key:', key);
}

if (localConfig[key] === undefined) {
console.warn(
'Populating key',
key,
'with default value',
defaultConfig[key]
);

localConfig[key] = defaultConfig[key];
}

return localConfig[key];
}

export function configWrite(key, value) {
if (!configExists(key)) {
throw new Error('tried to write unknown config key:', key);
}

console.info('Setting key', key, 'to', value);
localConfig[key] = value;
window.localStorage[CONFIG_KEY] = JSON.stringify(localConfig);
Expand Down
Loading