User:Eraloiz/fandomdesktop.js

/* Adds buttons to switch themes and a button to swap theme colors (without saving) */ $(document).ready(function {	/* Add swap theme colors button */	addDropdownOption("Swap theme colors", "Swaps theme colors without saving or reloading the page", function { var targetTheme = document.getElementsByClassName('theme-fandomdesktop-dark').length > 0 ? "light" : "dark";

$.get(mw.util.wikiScript("wikia") + "?controller=ThemeApi&method=themeVariables&variant=" + targetTheme).done(function(data) {			var $s = $("#themeSwapper")[0] || $(" ").attr("id", "themeSwapper").appendTo("body");			$($s).text(data);			$("body").removeClass("theme-fandomdesktop-light theme-fandomdesktop-dark").addClass("theme-fandomdesktop-" + targetTheme);		}); });

/* Add switch theme buttons */ const themeNames = ["wiki", "light", "dark"]; new mw.Api.get({		action: "query",		meta: "userinfo",		uiprop: "options"	}).done(function(data) {		const currentTheme = data.query.userinfo.options.theme;		themeNames.filter(function(i) { return i != currentTheme; }).forEach(function(targetTheme) { addDropdownOption("Switch to " + targetTheme + " theme", "Switches to " + targetTheme + " theme and reloads the page", function {				switchToTheme(targetTheme);			}); });	}); });

function addDropdownOption(label, hoverText, click) { $(".wiki-tools > .wds-dropdown > .wds-dropdown__content > .wds-list").each(function {		var newLi = document.createElement("li");		var newA = document.createElement("a");		newA.href = "javascript:void(0);";		newA.textContent = label;		newA.title = hoverText;		newLi.appendChild(newA);		$(newLi).click(click);		this.appendChild(newLi);	}); }

function switchToTheme(theme) { new mw.Api.postWithToken('csrf', {		action: "options",		change: "theme=" + theme	}).done(function(data) {		location.reload;	}).fail(function(code, data) {		alert("Error while switching theme: " + code);	}); }