class Settings { constructor(activeSettings) { this.active = activeSettings ? activeSettings : undefined; this.default = ExtensionConf; this.useSync = false; this.version = undefined; const ths = this; if(BrowserDetect.firefox) { browser.storage.onChanged.addListener( (changes, area) => { if (Debug.debug) { console.log("[Settings::] Settings have been changed outside of here. Updating active settings. Changes:", changes, "storage area:", area); if (changes['uwSettings'] && changes['uwSettings'].newValue) { console.log("[Settings::] new settings object:", JSON.parse(changes.uwSettings.newValue)); } } if(changes['uwSettings'] && changes['uwSettings'].newValue) { ths.active = JSON.parse(changes.uwSettings.newValue); } }); } else if (BrowserDetect.chrome) { chrome.storage.onChanged.addListener( (changes, area) => { if (Debug.debug) { console.log("[Settings::] Settings have been changed outside of here. Updating active settings. Changes:", changes, "storage area:", area); if (changes['uwSettings'] && changes['uwSettings'].newValue) { console.log("[Settings::] new settings object:", JSON.parse(changes.uwSettings.newValue)); } } if(changes['uwSettings'] && changes['uwSettings'].newValue) { ths.active = JSON.parse(changes.uwSettings.newValue); } }) } } async init() { const settings = await this.get(); if(Debug.debug) { console.log("[Settings::init] Configuration fetched from storage:", settings); } // if there's no settings saved, return default settings. if(! settings || (Object.keys(settings).length === 0 && settings.constructor === Object)) { this.setDefaultSettings(); this.active = this.getDefaultSettings(); return this.active; } // if there's settings, set saved object as active settings this.active = settings; // check if extension has been updated. If not, return settings as they were retreived if (BrowserDetect.firefox) { this.version = browser.runtime.getManifest().version; } else if (BrowserDetect.chrome) { this.version = chrome.runtime.getManifest().version; } else if (BrowserDetect.edge) { this.version = browser.runtime.getManifest().version; } if(settings.version === this.version) { if(Debug.debug) { console.log("[Settings::init] extension was saved with current version of ultrawidify (", this.version, "). Returning object as-is."); } return this.active; } // 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. const patched = ObjectCopy.addNew(settings, this.default); if(Debug.debug) { console.log("[Settings.init] Results from ObjectCopy.addNew()?", patched, "\n\nSettings from storage", settings, "\ndefault?", this.default,); } if(patched){ this.active = patched; } else { this.active = JSON.parse(JSON.stringify(this.default)); } this.set(this.active); return this.active; } async get() { if (BrowserDetect.firefox || BrowserDetect.edge) { const ret = this.useSync ? await browser.storage.sync.get('uwSettings') : await browser.storage.local.get('uwSettings'); try { return JSON.parse(ret.uwSettings); } catch(e) { return undefined; } } else if (BrowserDetect.chrome) { const ret = new Promise( (resolve, reject) => { chrome.storage.sync.get('uwSettings', (res) => resolve(res)); }); return ret['uwSettings']; } } async set(extensionConf) { if (Debug.debug) { console.log("[Settings::set] setting new settings:", extensionConf) } if (BrowserDetect.firefox || BrowserDetect.edge) { extensionConf.version = this.version; return this.useSync ? browser.storage.sync.set( {'uwSettings': JSON.stringify(extensionConf)}): browser.storage.local.set( {'uwSettings': JSON.stringify(extensionConf)}); } else if (BrowserDetect.chrome) { return chrome.storage.sync.set( {'uwSettings': JSON.stringify(extensionConf)}); } } async setActive(activeSettings) { this.active = activeSettings; } async setProp(prop, value) { this.active[prop] = value; } async save() { if (Debug.debug) { console.log("[Settings::save] Saving active settings:", this.active); } this.set(this.active); } getDefaultSettings() { return JSON.parse(JSON.stringify(this.default)); } setDefaultSettings() { this.set(this.default); } // ----------------------------------------- // Nastavitve za posamezno stran // Config for a given page: // // : { // status: