"use strict";
/* global mw */
/** Resolve wiktgadgetprefs-.
* Requires the mediawiki.user ResourceLoader module to be loaded.
* Works for both registered and unregistered users.
* Both the namespace and key must be non-empty, and may only
consist of letters, numbers, the hyphen and the underscore.
* The namespace may not contain multiple hyphens in a row.
* The namespace is given first, and then this returns a function,
which is provided the key.
Example:
const getPrefForGadgetFoobar = getWiktGadgetPrefs("foobar");
const message = getWiktGadgetPrefs("message");
This is a snippet that can be copied from one script to another,
and it should be. */
function getWiktGadgetPrefs(namespace) {
function getWiktGadgetPrefsInNamespace(key) {
var fullKey = "wiktgadgetprefs-"
+ namespace.replace(//g, "").replace(/--+/g, "-")
+ "--" + key.replace(//g, "");
var mwOptionsValue;
try {
mwOptionsValue = mw.user.options.values;
} catch (e) { }
if (window.localStorage && window.localStorage.getItem(fullKey) != null) {
try {
return JSON.parse(window.localStorage.getItem(fullKey));
} catch (e) { }
}
if (mwOptionsValue && mwOptionsValue != null) {
try {
return JSON.parse(mwOptionsValue);
} catch (e) { }
}
return undefined;
}
return getWiktGadgetPrefsInNamespace;
}
/** Modify wiktgadgetprefs-.
Each modification given in modifications should be an object with
the following fields: namespace, key, and value.
* Requires the mediawiki.api ResourceLoader module to be loaded.
* Both the namespace and key must be non-empty, and may only
consist of letters, numbers, the hyphen and the underscore.
* The namespace may not contain multiple hyphens in a row.
* All values must be convertible into JSON with JSON.stringify.
This should not be copied into or used by gadgets.
Calls the callback with one parameters, true if successful,
or false if not successful. */
function setWiktGadgetPrefs(modifications, callback) {
var i, namespace, key, value, fullKey;
if (mw.config.get("wgUserId") != null) {
// logged in users
var optionChanges = {};
for (i = 0; i < modifications.length; i++) {
if (modifications.namespace && modifications.key
&& typeof modifications.value !== "undefined") {
namespace = modifications.namespace;
key = modifications.key;
value = modifications.value;
// ignore invalid namespaces and keys
if (namespace.match(//))
continue;
if (namespace.indexOf("--") >= 0)
continue;
if (key.match(//))
continue;
fullKey = "wiktgadgetprefs-" + namespace + "--" + key;
optionChanges = JSON.stringify(value);
}
}
new mw.Api().saveOptions(optionChanges).then(function() {
callback(true);
}, function() {
callback(false);
});
} else {
// anonymous users
for (i = 0; i < modifications.length; i++) {
if (modifications.namespace && modifications.key
&& typeof modifications.value !== "undefined") {
namespace = modifications.namespace;
key = modifications.key;
value = modifications.value;
// ignore invalid namespaces and keys
if (namespace.match(//))
continue;
if (namespace.indexOf("--") >= 0)
continue;
if (key.match(//))
continue;
fullKey = "wiktgadgetprefs-" + namespace + "--" + key;
try {
window.localStorage.setItem(fullKey, JSON.stringify(value));
} catch (e) {
callback(false);
return;
}
}
}
callback(true);
}
}