mirror of
https://github.com/LBRYFoundation/Watch-on-LBRY.git
synced 2025-08-23 17:47:26 +00:00
28 lines
1.2 KiB
TypeScript
28 lines
1.2 KiB
TypeScript
import { DEFAULT_SETTINGS, ExtensionSettings, getExtensionSettingsAsync } from '../common/settings';
|
|
|
|
/** Reset settings to default value and update the browser badge text */
|
|
async function initSettings() {
|
|
const settings = await getExtensionSettingsAsync(...Object.keys(DEFAULT_SETTINGS) as Array<keyof ExtensionSettings>);
|
|
|
|
// get all the values that aren't set and use them as a change set
|
|
const invalidEntries = (Object.entries(DEFAULT_SETTINGS) as Array<[keyof ExtensionSettings, ExtensionSettings[keyof ExtensionSettings]]>)
|
|
.filter(([k]) => settings[k] === null || settings[k] === undefined);
|
|
|
|
// fix our local var and set it in storage for later
|
|
if (invalidEntries.length > 0) {
|
|
const changeSet = Object.fromEntries(invalidEntries);
|
|
Object.assign(settings, changeSet);
|
|
chrome.storage.local.set(changeSet);
|
|
}
|
|
|
|
chrome.browserAction.setBadgeText({ text: settings.redirect ? 'ON' : 'OFF' });
|
|
}
|
|
|
|
chrome.storage.onChanged.addListener((changes, areaName) => {
|
|
if (areaName !== 'local' || !changes.redirect) return;
|
|
chrome.browserAction.setBadgeText({ text: changes.redirect.newValue ? 'ON' : 'OFF' });
|
|
});
|
|
|
|
|
|
chrome.runtime.onStartup.addListener(initSettings);
|
|
chrome.runtime.onInstalled.addListener(initSettings);
|