* See README.md for usage and integration instructions.
*/
-/*jslint bitwise: false, white: false, browser: true, devel: true */
-/*global Util, window, document */
-
import { init_logging as main_init_logging } from '../core/util/logging.js';
// init log level reading the logging HTTP param
if (typeof level !== "undefined") {
main_init_logging(level);
} else {
- var param = document.location.href.match(/logging=([A-Za-z0-9\._\-]*)/);
+ var param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
main_init_logging(param || undefined);
}
-};
+}
// Read a query string variable
export function getQueryVar (name, defVal) {
} else {
return defVal;
}
-};
+}
// Read a hash fragment variable
export function getHashVar (name, defVal) {
} else {
return defVal;
}
-};
+}
// Read a variable from the fragment or the query string
// Fragment takes precedence
val = getQueryVar(name, defVal);
}
return val;
-};
+}
/*
* Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
secure = "";
}
document.cookie = name + "=" + value + expires + "; path=/" + secure;
-};
+}
export function readCookie (name, defaultValue) {
"use strict";
if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); }
}
return (typeof defaultValue !== 'undefined') ? defaultValue : null;
-};
+}
export function eraseCookie (name) {
"use strict";
createCookie(name, "", -1);
-};
+}
/*
* Setting handling.
}
});
} else {
- // No-op
+ settings = {};
if (callback) {
callback.apply(this, callbackArgs);
}
}
-};
+}
+
+// Update the settings cache, but do not write to permanent storage
+export function setSetting (name, value) {
+ settings[name] = value;
+}
// No days means only for this browser session
export function writeSetting (name, value) {
"use strict";
+ if (settings[name] === value) return;
+ settings[name] = value;
if (window.chrome && window.chrome.storage) {
- if (settings[name] !== value) {
- settings[name] = value;
- window.chrome.storage.sync.set(settings);
- }
+ window.chrome.storage.sync.set(settings);
} else {
localStorage.setItem(name, value);
}
-};
+}
export function readSetting (name, defaultValue) {
"use strict";
var value;
- if (window.chrome && window.chrome.storage) {
+ if ((name in settings) || (window.chrome && window.chrome.storage)) {
value = settings[name];
} else {
value = localStorage.getItem(name);
+ settings[name] = value;
}
if (typeof value === "undefined") {
value = null;
} else {
return value;
}
-};
+}
export function eraseSetting (name) {
"use strict";
+ // Deleting here means that next time the setting is read when using local
+ // storage, it will be pulled from local storage again.
+ // If the setting in local storage is changed (e.g. in another tab)
+ // between this delete and the next read, it could lead to an unexpected
+ // value change.
+ delete settings[name];
if (window.chrome && window.chrome.storage) {
window.chrome.storage.sync.remove(name);
- delete settings[name];
} else {
localStorage.removeItem(name);
}
-};
+}
export function injectParamIfMissing (path, param, value) {
// force pretend that we're dealing with a relative path
} else {
return elem.pathname + elem.search + elem.hash;
}
-};
+}
// sadly, we can't use the Fetch API until we decide to drop
// IE11 support or polyfill promises and fetch in IE11.