User:Suffusion of Yellow/AnonSettings/config.js

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.
// DO NOT INSTALL THIS IN YOUR common.js!
(function() {
	function uninstall() {
		navigator.serviceWorker.getRegistrations()
			.then(regs => regs.forEach(reg => reg.unregister().then(() => window.location = "/")));
	}

	function update() {
		let settings = {
			skin : document.getElementById('anonsettings-skin').value,
			lang : document.getElementById('anonsettings-lang').value,
			gadget : document.getElementById('anonsettings-gadget').value,
			nologin : document.getElementById('anonsettings-nologin').checked
		};
		navigator.serviceWorker.controller.postMessage({ settings });

		window.location.reload();
	}

	function setup(r) {
		if (r.query.userinfo.id !== 0) {
			document.getElementById("anonsettings-status").textContent = "Whoa there! You seem to be logged in! You probably want to uninstall this, and change your preferences instead.";
		} else
			document.getElementById("anonsettings-status").textContent = "Welcome non-logged-in user!";

		let skinSelect = document.getElementById("anonsettings-skin");
		for(let item of r.query.skins)
			if (!item.unusable) {
				let option = document.createElement("option");
				option.value = item.code;
				option.textContent = item.name;
				skinSelect.appendChild(option);
			}
		let langSelect = document.getElementById("anonsettings-lang");
		for(let item of r.query.languages) {
			let option = document.createElement("option");
			option.value = item.code;
			option.textContent = item.code + " - " + item.name;
			langSelect.appendChild(option);
		}
		let gadgetSelect = document.getElementById("anonsettings-gadget");
		for(let item of r.query.gadgets)
			if (item.metadata.settings.supportsUrlLoad && !item.metadata.settings.hidden && !item.metadata.settings.default) {
				let option = document.createElement("option");
				option.value = item.id;
				option.textContent = item.id;
				gadgetSelect.appendChild(option);
			}

		let settings = ANON_SETTINGS;

		if (settings.skin)
			document.getElementById('anonsettings-skin').value = settings.skin;
		if (settings.lang)
			document.getElementById('anonsettings-lang').value = settings.lang;
		if (settings.gadget)
			document.getElementById('anonsettings-gadget').value = settings.gadget;

		document.getElementById('anonsettings-nologin').checked = settings.nologin;

		document.getElementById('anonsettings-settings').style = "display:block;";
		document.getElementById('anonsettings-uninstall')
			.addEventListener('click', uninstall);

		document.getElementById('anonsettings-save')
			.addEventListener('click', update);

	}

	document.title = "AnonSettings setup";
	(document.getElementById("content") || document.body).innerHTML = `
<div>
  <h1>AnonSettings setup</h1>
    <div id="anonsettings-status">Fetching site information...</div>
    <div id="anonsettings-uninstall-section">
      <h2>Uninstall</h2>
      <button id="anonsettings-uninstall">Uninstall AnonSettings</button>
    </div>
    <div id="anonsettings-settings" style="display:none;">
    <h2>Settings</h2>
    <p>
      <select id="anonsettings-skin">
          <option value="">(default)</option>
      </select>
      <label for="anonsettings-skin">Skin</label>
    </p><p>
      <select id="anonsettings-lang">
         <option value="">(default)</option>
         <option value="qqx">qqx - (system messages)</option>
      </select>
      <label for="anonsettings-lang">Interface language</label>
    </p><p>
      <select id="anonsettings-gadget">
          <option value="">(none)</option>
      </select>
      <label for="anonsettings-gadget">Gadget</label>
    </p><p>
      <input id="anonsettings-nologin" type="checkbox" checked>
      <label for="anonsettings-nologin">Automatically uninstall on attempted login (recommended)</option>
    </p><p>
      <button id="anonsettings-save">Save settings</button>
    </p>
</div>
`;

	fetch("/w/api.php?action=query&list=gadgets&meta=userinfo|siteinfo&siprop=skins|languages&format=json&formatversion=2")
		.then(r => r.json())
		.then(setup);
})();
// DO NOT INSTALL THIS IN YOUR common.js!