#40: Settings persist over page reload

This commit is contained in:
Tamius Han 2018-08-21 23:48:47 +02:00
parent a5aa282a01
commit 83aef6ac79
5 changed files with 27 additions and 14 deletions

View File

@ -232,6 +232,9 @@ class CommsServer {
}
if (message.cmd === 'get-config') {
if(Debug.debug) {
console.log("CommsServer: received get-config. Active settings?", this.settings.active, "\n(settings:", this.settings, ")")
}
port.postMessage({cmd: "set-config", conf: this.settings.active, site: this.server.currentSite})
} else if (message.cmd === 'set-stretch') {
this.sendToActive(message);

View File

@ -3,18 +3,18 @@ class Settings {
constructor(activeSettings) {
this.active = activeSettings ? activeSettings : {};
this.default = ExtensionConf;
this.useSync = false;
}
async init() {
const settings = await this.get('uw-settings');
const settings = await this.get();
if(Debug.debug) {
console.log("[Settings::init] Configuration fetched from background script:", settings);
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)) {
console.log("NO SETTINGS")
this.setDefaultSettings();
this.active = this.getDefaultSettings();
return this.active;
@ -50,8 +50,12 @@ class Settings {
async get() {
if (BrowserDetect.firefox || BrowserDetect.edge) {
const ret = browser.storage.sync.get('uw-settings');
return ret['uw-settings'];
const ret = this.useSync ? await browser.storage.sync.get('uw-settings') : await browser.storage.local.get('uw-settings');
try {
return JSON.parse(ret['uw-settings']);
} catch(e) {
return undefined;
}
} else if (BrowserDetect.chrome) {
const ret = chrome.storage.sync.get('uw-settings');
return ret['uw-settings'];
@ -60,7 +64,7 @@ class Settings {
async set(extensionConf) {
if (BrowserDetect.firefox || BrowserDetect.edge) {
return browser.storage.sync.set( {'uw-settings': extensionConf});
return this.useSync ? browser.storage.sync.set( {'uw-settings': JSON.stringify(extensionConf)}): browser.storage.local.set( {'uw-settings': JSON.stringify(extensionConf)});
} else if (BrowserDetect.chrome) {
return chrome.storage.sync.set( {'uw-settings': extensionConf});
}
@ -125,11 +129,11 @@ class Settings {
const cse = this.canStartExtension(site);
Debug.debug = true;
console.log("[Settings::canStartExtension] ----------------\nCAN WE START THIS EXTENSION ON SITE", site,
"?\n\nsettings.active.sites[site]=", this.active.sites[site],
"\nExtension mode?", this.active.extensionMode,
"\nCan extension be started?", cse
);
// console.log("[Settings::canStartExtension] ----------------\nCAN WE START THIS EXTENSION ON SITE", site,
// "?\n\nsettings.active.sites[site]=", this.active.sites[site],
// "\nExtension mode?", this.active.extensionMode,
// "\nCan extension be started?", cse
// );
}
try{
// if site is not defined, we use default mode:

View File

@ -228,12 +228,14 @@ class ArDetector {
);
}
scheduleFrameCheck(timeout, force_reset){
async scheduleFrameCheck(timeout, force_reset){
if(! timeout){
this.frameCheck();
return;
}
var e = await this.settings.get();
console.log("settings?", this.settings.active, e);
// run anything that needs to be run after frame check
this.postFrameCheck();

View File

@ -69,6 +69,11 @@ port.onMessage.addListener( (m,p) => processReceivedMessage(m,p));
async function processReceivedMessage(message, port){
if(message.cmd === 'set-config'){
if(Debug.debug){
console.log("[popup.js] setting config. Message was:", message)
}
this.loadConfig(message.conf, message.site);
}
}

View File

@ -257,10 +257,9 @@
<script src="../../js/conf/Debug.js"></script>
<script src="../../js/lib/BrowserDetect.js"></script>
<script src="../../js/lib/StorageManager.js"></script>
<script src="../../js/lib/Comms.js"></script>
<script src="../../js/conf/Settings.js"></script>
<script src="../../js/lib/Settings.js"></script>
<script src="./js/popup.js"></script>
</body>