Make logger and options object optional for Settings
This commit is contained in:
parent
76b380d6c3
commit
3704ea7962
@ -7,6 +7,7 @@ import Stretch from '../../common/enums/stretch.enum';
|
|||||||
import VideoAlignment from '../../common/enums/video-alignment.enum';
|
import VideoAlignment from '../../common/enums/video-alignment.enum';
|
||||||
import ExtensionConfPatch from '../conf/ExtConfPatches';
|
import ExtensionConfPatch from '../conf/ExtConfPatches';
|
||||||
import CropModePersistence from '../../common/enums/crop-mode-persistence.enum';
|
import CropModePersistence from '../../common/enums/crop-mode-persistence.enum';
|
||||||
|
import BrowserDetect from '../conf/BrowserDetect';
|
||||||
|
|
||||||
if(process.env.CHANNEL !== 'stable'){
|
if(process.env.CHANNEL !== 'stable'){
|
||||||
console.info("Loading Settings");
|
console.info("Loading Settings");
|
||||||
@ -16,10 +17,10 @@ class Settings {
|
|||||||
|
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
// Options: activeSettings, updateCallback, logger
|
// Options: activeSettings, updateCallback, logger
|
||||||
this.logger = options.logger;
|
this.logger = options?.logger;
|
||||||
this.onSettingsChanged = options.onSettingsChanged;
|
this.onSettingsChanged = options?.onSettingsChanged;
|
||||||
this.afterSettingsSaved = options.afterSettingsSaved;
|
this.afterSettingsSaved = options?.afterSettingsSaved;
|
||||||
this.active = options.activeSettings ?? undefined;
|
this.active = options?.activeSettings ?? undefined;
|
||||||
this.default = ExtensionConf;
|
this.default = ExtensionConf;
|
||||||
this.default['version'] = this.getExtensionVersion();
|
this.default['version'] = this.getExtensionVersion();
|
||||||
this.useSync = false;
|
this.useSync = false;
|
||||||
@ -36,22 +37,22 @@ class Settings {
|
|||||||
if (!changes.uwSettings) {
|
if (!changes.uwSettings) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.logger.log('info', 'settings', "[Settings::<storage/on change>] Settings have been changed outside of here. Updating active settings. Changes:", changes, "storage area:", area);
|
this.logger?.log('info', 'settings', "[Settings::<storage/on change>] Settings have been changed outside of here. Updating active settings. Changes:", changes, "storage area:", area);
|
||||||
// if (changes['uwSettings'] && changes['uwSettings'].newValue) {
|
// if (changes['uwSettings'] && changes['uwSettings'].newValue) {
|
||||||
// this.logger.log('info', 'settings',"[Settings::<storage/on change>] new settings object:", JSON.parse(changes.uwSettings.newValue));
|
// this.logger?.log('info', 'settings',"[Settings::<storage/on change>] new settings object:", JSON.parse(changes.uwSettings.newValue));
|
||||||
// }
|
// }
|
||||||
const parsedSettings = JSON.parse(changes.uwSettings.newValue);
|
const parsedSettings = JSON.parse(changes.uwSettings.newValue);
|
||||||
this.setActive(parsedSettings);
|
this.setActive(parsedSettings);
|
||||||
|
|
||||||
this.logger.log('info', 'debug', 'Does parsedSettings.preventReload exist?', parsedSettings.preventReload, "Does callback exist?", !!this.onSettingsChanged);
|
this.logger?.log('info', 'debug', 'Does parsedSettings.preventReload exist?', parsedSettings.preventReload, "Does callback exist?", !!this.onSettingsChanged);
|
||||||
|
|
||||||
if (!parsedSettings.preventReload && this.onSettingsChanged) {
|
if (!parsedSettings.preventReload && this.onSettingsChanged) {
|
||||||
try {
|
try {
|
||||||
this.onSettingsChanged();
|
this.onSettingsChanged();
|
||||||
|
|
||||||
this.logger.log('info', 'settings', '[Settings] Update callback finished.')
|
this.logger?.log('info', 'settings', '[Settings] Update callback finished.')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.log('error', 'settings', "[Settings] CALLING UPDATE CALLBACK FAILED. Reason:", e)
|
this.logger?.log('error', 'settings', "[Settings] CALLING UPDATE CALLBACK FAILED. Reason:", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.afterSettingsSaved) {
|
if (this.afterSettingsSaved) {
|
||||||
@ -144,12 +145,12 @@ class Settings {
|
|||||||
applySettingsPatches(oldVersion, patches) {
|
applySettingsPatches(oldVersion, patches) {
|
||||||
let index = this.findFirstNecessaryPatch(oldVersion, patches);
|
let index = this.findFirstNecessaryPatch(oldVersion, patches);
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
this.logger.log('info','settings','[Settings::applySettingsPatches] There are no pending conf patches.');
|
this.logger?.log('info','settings','[Settings::applySettingsPatches] There are no pending conf patches.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply all remaining patches
|
// apply all remaining patches
|
||||||
this.logger.log('info', 'settings', `[Settings::applySettingsPatches] There are ${patches.length - index} settings patches to apply`);
|
this.logger?.log('info', 'settings', `[Settings::applySettingsPatches] There are ${patches.length - index} settings patches to apply`);
|
||||||
while (index < patches.length) {
|
while (index < patches.length) {
|
||||||
const updateFn = patches[index].updateFn;
|
const updateFn = patches[index].updateFn;
|
||||||
delete patches[index].forVersion;
|
delete patches[index].forVersion;
|
||||||
@ -163,7 +164,7 @@ class Settings {
|
|||||||
try {
|
try {
|
||||||
updateFn(this.active, this.getDefaultSettings());
|
updateFn(this.active, this.getDefaultSettings());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.log('error', 'settings', '[Settings::applySettingsPatches] Failed to execute update function. Keeping settings object as-is. Error:', e);
|
this.logger?.log('error', 'settings', '[Settings::applySettingsPatches] Failed to execute update function. Keeping settings object as-is. Error:', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,14 +182,14 @@ class Settings {
|
|||||||
const oldVersion = (settings && settings.version) || this.version;
|
const oldVersion = (settings && settings.version) || this.version;
|
||||||
|
|
||||||
if (settings) {
|
if (settings) {
|
||||||
this.logger.log('info', 'settings', "[Settings::init] Configuration fetched from storage:", settings,
|
this.logger?.log('info', 'settings', "[Settings::init] Configuration fetched from storage:", settings,
|
||||||
"\nlast saved with:", settings.version,
|
"\nlast saved with:", settings.version,
|
||||||
"\ncurrent version:", this.version
|
"\ncurrent version:", this.version
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (Debug.flushStoredSettings) {
|
// if (Debug.flushStoredSettings) {
|
||||||
// this.logger.log('info', 'settings', "%c[Settings::init] Debug.flushStoredSettings is true. Using default settings", "background: #d00; color: #ffd");
|
// this.logger?.log('info', 'settings', "%c[Settings::init] Debug.flushStoredSettings is true. Using default settings", "background: #d00; color: #ffd");
|
||||||
// Debug.flushStoredSettings = false; // don't do it again this session
|
// Debug.flushStoredSettings = false; // don't do it again this session
|
||||||
// this.active = this.getDefaultSettings();
|
// this.active = this.getDefaultSettings();
|
||||||
// this.active.version = this.version;
|
// this.active.version = this.version;
|
||||||
@ -198,7 +199,7 @@ class Settings {
|
|||||||
|
|
||||||
// if there's no settings saved, return default settings.
|
// if there's no settings saved, return default settings.
|
||||||
if(! settings || (Object.keys(settings).length === 0 && settings.constructor === Object)) {
|
if(! settings || (Object.keys(settings).length === 0 && settings.constructor === Object)) {
|
||||||
this.logger.log(
|
this.logger?.log(
|
||||||
'info',
|
'info',
|
||||||
'settings',
|
'settings',
|
||||||
'[Settings::init] settings don\'t exist. Using defaults.\n#keys:',
|
'[Settings::init] settings don\'t exist. Using defaults.\n#keys:',
|
||||||
@ -235,7 +236,7 @@ class Settings {
|
|||||||
|
|
||||||
// check if extension has been updated. If not, return settings as they were retrieved
|
// check if extension has been updated. If not, return settings as they were retrieved
|
||||||
if (this.active.version === this.version) {
|
if (this.active.version === this.version) {
|
||||||
this.logger.log('info', 'settings', "[Settings::init] extension was saved with current version of ultrawidify. Returning object as-is.");
|
this.logger?.log('info', 'settings', "[Settings::init] extension was saved with current version of ultrawidify. Returning object as-is.");
|
||||||
return this.active;
|
return this.active;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,7 +247,7 @@ class Settings {
|
|||||||
// if extension has been updated, update existing settings with any options added in the
|
// if extension has been updated, update existing settings with any options added in the
|
||||||
// new version. In addition to that, we remove old keys that are no longer used.
|
// new version. In addition to that, we remove old keys that are no longer used.
|
||||||
const patched = ObjectCopy.addNew(settings, this.default);
|
const patched = ObjectCopy.addNew(settings, this.default);
|
||||||
this.logger.log('info', 'settings',"[Settings.init] Results from ObjectCopy.addNew()?", patched, "\n\nSettings from storage", settings, "\ndefault?", this.default);
|
this.logger?.log('info', 'settings',"[Settings.init] Results from ObjectCopy.addNew()?", patched, "\n\nSettings from storage", settings, "\ndefault?", this.default);
|
||||||
|
|
||||||
if (patched) {
|
if (patched) {
|
||||||
this.active = patched;
|
this.active = patched;
|
||||||
@ -279,7 +280,7 @@ class Settings {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.log('info', 'settings', 'Got settings:', ret && ret.uwSettings && JSON.parse(ret.uwSettings));
|
this.logger?.log('info', 'settings', 'Got settings:', ret && ret.uwSettings && JSON.parse(ret.uwSettings));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return JSON.parse(ret.uwSettings);
|
return JSON.parse(ret.uwSettings);
|
||||||
@ -318,11 +319,11 @@ class Settings {
|
|||||||
|
|
||||||
this.fixSitesSettings(extensionConf.sites);
|
this.fixSitesSettings(extensionConf.sites);
|
||||||
|
|
||||||
this.logger.log('info', 'settings', "[Settings::set] setting new settings:", extensionConf)
|
this.logger?.log('info', 'settings', "[Settings::set] setting new settings:", extensionConf)
|
||||||
|
|
||||||
if (currentBrowser.firefox || currentBrowser.edge) {
|
if (BrowserDetect.firefox) {
|
||||||
return browser.storage.local.set( {'uwSettings': JSON.stringify(extensionConf)});
|
return browser.storage.local.set( {'uwSettings': JSON.stringify(extensionConf)});
|
||||||
} else if (currentBrowser.chrome) {
|
} else {
|
||||||
return chrome.storage.local.set( {'uwSettings': JSON.stringify(extensionConf)});
|
return chrome.storage.local.set( {'uwSettings': JSON.stringify(extensionConf)});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -393,7 +394,7 @@ class Settings {
|
|||||||
site = window.location.hostname;
|
site = window.location.hostname;
|
||||||
|
|
||||||
if (!site) {
|
if (!site) {
|
||||||
this.logger.log('info', 'settings', `[Settings::canStartExtension] window.location.hostname is null or undefined: ${window.location.hostname} \nactive settings:`, this.active);
|
this.logger?.log('info', 'settings', `[Settings::canStartExtension] window.location.hostname is null or undefined: ${window.location.hostname} \nactive settings:`, this.active);
|
||||||
return ExtensionMode.Disabled;
|
return ExtensionMode.Disabled;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -419,7 +420,7 @@ class Settings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch(e){
|
} catch(e){
|
||||||
this.logger.log('error', 'settings', "[Settings.js::canStartExtension] Something went wrong — are settings defined/has init() been called?\n\nerror:", e, "\n\nSettings object:", this)
|
this.logger?.log('error', 'settings', "[Settings.js::canStartExtension] Something went wrong — are settings defined/has init() been called?\n\nerror:", e, "\n\nSettings object:", this)
|
||||||
|
|
||||||
return ExtensionMode.Disabled;
|
return ExtensionMode.Disabled;
|
||||||
}
|
}
|
||||||
@ -431,7 +432,7 @@ class Settings {
|
|||||||
site = window.location.hostname;
|
site = window.location.hostname;
|
||||||
|
|
||||||
if (!site) {
|
if (!site) {
|
||||||
this.logger.log('info', 'settings', `[Settings::canStartExtension] window.location.hostname is null or undefined: ${window.location.hostname} \nactive settings:`, this.active);
|
this.logger?.log('info', 'settings', `[Settings::canStartExtension] window.location.hostname is null or undefined: ${window.location.hostname} \nactive settings:`, this.active);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -458,7 +459,7 @@ class Settings {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
this.logger.log('error', 'settings', "[Settings.js::canStartExtension] Something went wrong — are settings defined/has init() been called?\nSettings object:", this);
|
this.logger?.log('error', 'settings', "[Settings.js::canStartExtension] Something went wrong — are settings defined/has init() been called?\nSettings object:", this);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -480,7 +481,7 @@ class Settings {
|
|||||||
return this.active.sites[site].keyboardShortcutsEnabled === ExtensionMode.Enabled;
|
return this.active.sites[site].keyboardShortcutsEnabled === ExtensionMode.Enabled;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.log('info', 'settings',"[Settings.js::keyboardDisabled] something went wrong:", e);
|
this.logger?.log('info', 'settings',"[Settings.js::keyboardDisabled] something went wrong:", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -499,7 +500,7 @@ class Settings {
|
|||||||
site = window.location.hostname;
|
site = window.location.hostname;
|
||||||
|
|
||||||
if (!site) {
|
if (!site) {
|
||||||
this.logger.log('warn', ['settings', 'init', 'debug'], `[Settings::canStartAutoAr] No site — even window.location.hostname returned nothing!: ${window.location.hostname}`);
|
this.logger?.log('warn', ['settings', 'init', 'debug'], `[Settings::canStartAutoAr] No site — even window.location.hostname returned nothing!: ${window.location.hostname}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -512,7 +513,7 @@ class Settings {
|
|||||||
// const csar = this.canStartAutoAr(site);
|
// const csar = this.canStartAutoAr(site);
|
||||||
// Debug.debug = true;
|
// Debug.debug = true;
|
||||||
|
|
||||||
this.logger.log('info', ['settings', 'init', 'debug'], "[Settings::canStartAutoAr] ----------------\nCAN WE START AUTOAR ON SITE", site,
|
this.logger?.log('info', ['settings', 'init', 'debug'], "[Settings::canStartAutoAr] ----------------\nCAN WE START AUTOAR ON SITE", site,
|
||||||
"?\n\nsettings.active.sites[site]=", this.active.sites[site], "settings.active.sites[@global]=", this.active.sites['@global'],
|
"?\n\nsettings.active.sites[site]=", this.active.sites[site], "settings.active.sites[@global]=", this.active.sites['@global'],
|
||||||
"\nAutoar mode (global)?", this.active.sites['@global'].autoar,
|
"\nAutoar mode (global)?", this.active.sites['@global'].autoar,
|
||||||
`\nAutoar mode (${site})`, this.active.sites[site] ? this.active.sites[site].autoar : '<not defined>',
|
`\nAutoar mode (${site})`, this.active.sites[site] ? this.active.sites[site].autoar : '<not defined>',
|
||||||
@ -522,18 +523,18 @@ class Settings {
|
|||||||
|
|
||||||
// if site is not defined, we use default mode:
|
// if site is not defined, we use default mode:
|
||||||
if (! this.active.sites[site]) {
|
if (! this.active.sites[site]) {
|
||||||
this.logger.log('info', ['settings', 'aard', 'init', 'debug'], "[Settings::canStartAutoAr] Settings not defined for this site, returning defaults.", site, this.active.sites[site], this.active.sites);
|
this.logger?.log('info', ['settings', 'aard', 'init', 'debug'], "[Settings::canStartAutoAr] Settings not defined for this site, returning defaults.", site, this.active.sites[site], this.active.sites);
|
||||||
return this.active.sites['@global'].autoar === ExtensionMode.Enabled;
|
return this.active.sites['@global'].autoar === ExtensionMode.Enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.active.sites['@global'].autoar === ExtensionMode.Enabled) {
|
if (this.active.sites['@global'].autoar === ExtensionMode.Enabled) {
|
||||||
this.logger.log('info', ['settings', 'aard', 'init', 'debug'], `[Settings::canStartAutoAr] Aard is enabled by default. Extension can run unless disabled for this site.`, this.active.sites[site].autoar);
|
this.logger?.log('info', ['settings', 'aard', 'init', 'debug'], `[Settings::canStartAutoAr] Aard is enabled by default. Extension can run unless disabled for this site.`, this.active.sites[site].autoar);
|
||||||
return this.active.sites[site].autoar !== ExtensionMode.Disabled;
|
return this.active.sites[site].autoar !== ExtensionMode.Disabled;
|
||||||
} else if (this.active.sites['@global'].autoar === ExtensionMode.Whitelist) {
|
} else if (this.active.sites['@global'].autoar === ExtensionMode.Whitelist) {
|
||||||
this.logger.log('info', ['settings', 'init', 'debug'], "canStartAutoAr — can(not) start aard because extension is in whitelist mode, and this site is (not) equal to", ExtensionMode.Enabled)
|
this.logger?.log('info', ['settings', 'init', 'debug'], "canStartAutoAr — can(not) start aard because extension is in whitelist mode, and this site is (not) equal to", ExtensionMode.Enabled)
|
||||||
return this.active.sites[site].autoar === ExtensionMode.Enabled;
|
return this.active.sites[site].autoar === ExtensionMode.Enabled;
|
||||||
} else {
|
} else {
|
||||||
this.logger.log('info', ['settings', 'init', 'debug'], "canStartAutoAr — cannot start aard because extension is globally disabled")
|
this.logger?.log('info', ['settings', 'init', 'debug'], "canStartAutoAr — cannot start aard because extension is globally disabled")
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user