User:Joritochip/ThemeSwitcher.js

$(function {   if (mw.config.get('skin') !== 'fandomdesktop' || window.ThemeSwitcherLoaded) return;	window.ThemeSwitcherLoaded = true;	var initialPreference = mw.user.options.get('theme');	var initialTheme = $('body').hasClass('theme-fandomdesktop-light') ? 'light' : 'dark';    var theme = initialTheme;    function setPreference(theme) {    	if (['light', 'dark', 'wiki'].indexOf(theme) === -1) return;    	new mw.Api.post({ action: 'options', optionname: 'theme', optionvalue: theme, token: mw.user.tokens.get('csrfToken') });   }    function applyStylesheet(theme) {    	if (['light', 'dark'].indexOf(theme) === -1) return;		$.get(mw.util.wikiScript('wikia')+'?controller=ThemeApi&method=themeVariables&variant='+theme).done(function(data) { var $s = $('#pcjThemeSwitch')[0] || $(' ').attr('id','pcjThemeSwitch').appendTo('body'); $($s).text(data); $('body').removeClass('theme-fandomdesktop-light theme-fandomdesktop-dark').addClass('theme-fandomdesktop-'+theme); });   }	var toggle = $('Toggle theme').click(function { theme = theme === 'light' ? 'dark' : 'light'; if (theme !== initialPreference) { reset.show; } else { reset.hide; }		setPreference(theme); applyStylesheet(theme); });	var reset = $(' ').hide.click(function { theme = initialTheme; setPreference(initialPreference); applyStylesheet(initialTheme); reset.hide; });	var listItem = $(' ').append(toggle, reset);   $('#WikiaBar .toolbar .tools').append(listItem); });