Jump to content

User:BethNaught/exportUserOptions.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* global mw */

mw.loader.using(['mediawiki.user'], function () {
	"use strict";
	
	// List of options which will be not output in restricted mode. Add options containing personal data to this list.
	// Testing! Not all options which should be here are.
	var blacklist = ["watchlisttoken", "pagetriage-lastuse", "rcfilters-saved-queries", "rcfilters-wl-saved-queries"];
	
	// Alternative whitelist-based system:
	/**
	var whitelist = ["monobook-responsive", "globaluserpage", "minordefault"];
	*/

	var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'skin']);

	function filterBlacklist(key, value) {
		if (blacklist.includes(key)) {
			return undefined;
		} else {
			return value;
		}
	}

    function exportOptionsForm() {
        var bodyContent = (config.skin === "cologneblue" ? "article" : "bodyContent");
        document.getElementsByTagName("h1")[0].textContent = "Export user options";
        document.title = "Export user options";
        document.getElementById(bodyContent).innerHTML = '<div>' +
            '<form id="wpExportUserOptions" name="wpExportUserOptions">' +
            '<p>This page allows convenient export of your user preferences in JSON format.</p>' +
			'<p>You can choose whether to export a restricted set of preferences (unlikely to contain personal data) or all preferences. ' +
			'Before sharing the output of this page you should check that no data you do not wish to share is included.</p>' +
            '<input type="radio" name="personalchoice" value="restricted" id="wpExportUserOptionsRadioRestricted" checked="true">' +
			'<label for="wpExportUserOptionsRadioRestricted">Restricted</label>' +
			'<br>' +
			'<input type="radio" name="personalchoice" value="all" id="wpExportUserOptionsRadioAll">' +
			'<label for="wpExportUserOptionsRadioAll">All</label>' +
			'<br>' +
			'<input type="submit" value="Export">' +
            '</form>' +
			'</div>' +
			'<br><hr>' +
			'<div id="wpExportUserOptionsOutput">' +
			'<p>Output will appear here.</p>' +
        '</div>';
        document.getElementById("wpExportUserOptions").addEventListener("submit", function(f) {
			f.preventDefault();
			if (document.getElementById("wpExportUserOptionsRadioAll").checked) {
				document.getElementById("wpExportUserOptionsOutput").innerHTML = '<pre>' + 
					JSON.stringify(mw.user.options.values, null, '\t') + '</pre>';
				
			} else if (document.getElementById("wpExportUserOptionsRadioRestricted").checked) {
				document.getElementById("wpExportUserOptionsOutput").innerHTML = '<pre>' + 
					JSON.stringify(mw.user.options.values, filterBlacklist, '\t') + '</pre>';
				
				// Alternative whitelist-based system:
				/**
				document.getElementById("wpExportUserOptionsOutput").innerHTML = '<pre>' + 
					JSON.stringify(mw.user.options.values, whitelist, '\t') + '</pre>';
				*/
			}
		});
    }
     
    if (config.wgNamespaceNumber === -1 &&
		config.wgTitle.toLowerCase() === "exportuseroptions"
    ) {
        exportOptionsForm();
    }

});