User:Surjection/wiktgadgetprefs.js
Appearance
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This user script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
"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(/[^A-Za-z0-9_-]/g, "").replace(/--+/g, "-")
+ "--" + key.replace(/[^A-Za-z0-9_-]/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["userjs-" + fullKey] != null) {
try {
return JSON.parse(mwOptionsValue["userjs-" + fullKey]);
} 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[i].namespace && modifications[i].key
&& typeof modifications[i].value !== "undefined") {
namespace = modifications[i].namespace;
key = modifications[i].key;
value = modifications[i].value;
// ignore invalid namespaces and keys
if (namespace.match(/[^A-Za-z0-9_-]/))
continue;
if (namespace.indexOf("--") >= 0)
continue;
if (key.match(/[^A-Za-z0-9_-]/))
continue;
fullKey = "wiktgadgetprefs-" + namespace + "--" + key;
optionChanges[fullKey] = 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[i].namespace && modifications[i].key
&& typeof modifications[i].value !== "undefined") {
namespace = modifications[i].namespace;
key = modifications[i].key;
value = modifications[i].value;
// ignore invalid namespaces and keys
if (namespace.match(/[^A-Za-z0-9_-]/))
continue;
if (namespace.indexOf("--") >= 0)
continue;
if (key.match(/[^A-Za-z0-9_-]/))
continue;
fullKey = "wiktgadgetprefs-" + namespace + "--" + key;
try {
window.localStorage.setItem(fullKey, JSON.stringify(value));
} catch (e) {
callback(false);
return;
}
}
}
callback(true);
}
}