Site Notice

Remember to pay a visit to our Discord server and chat with our community! See here for more info.

Be sure to check out the KHUX Wiki for the latest on Kingdom Hearts Union χ and Kingdom Hearts Dark Road! Go and pitch in!

The KHWiki contains spoilers for all Kingdom Hearts games. Read at your own risk.

Please remember not to add information about unreleased games from trailers.

MediaWiki:Gadget-DarkToggle.js

From the Kingdom Hearts Wiki, the Kingdom Hearts encyclopedia
Revision as of 15:36, 5 March 2025 by Porplemontage (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// Toggle a dark theme for supported skins

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + '=' + escape(value) + ';path=/' + ((expiredays === null) ? '' : ';expires=' + exdate.toGMTString());
}
function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + '=');
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(';', c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return '';
}

$(function() {
	if (mw.user.options.get('gadget-DarkMode'))
		return;

	if (!getCookie('darkTheme')) {
		if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
			$('body').addClass('dark');
			$('body').removeClass('light');
		}
		window.matchMedia('(prefers-color-scheme: dark)').addListener(function(e) {
			if (e.matches) {
				$('body').addClass('dark');
				$('body').removeClass('light');
			} else {
				$('body').removeClass('dark');
				$('body').addClass('light');
			}
		});
	}

	var isDark = false;
	var toggleText = 'Dark mode';
	if (getCookie('darkTheme') == 'on' || !getCookie('darkTheme') && window.matchMedia('(prefers-color-scheme: dark)').matches) {
		toggleText = 'Light mode';
		isDark = true;
	}

	if (mw.config.get('skin') == 'nimbus') {
		$('#wiki-login').prepend('<div id="toggleContainer" style="float:left"><a id="toggleLink" href="javascript:;" title="Toggle night theme"><img id="toggleImage" src="https://wiki.gallery/images/dark/moon.png"/></a></div>');
	} else if (mw.config.get('skin') == 'minerva')
		$('#footer-places').after('<ul id="toggleContainer" class="minerva-toggle hlist"><li><a id="toggleLink" href="javascript:;">Toggle night theme</a></li></ul>');
	else if (mw.config.get('skin') == 'timeless' || mw.config.get('skin') == 'vector-2022')
		$('#footer-places').append('<li id="toggleContainer"><a id="toggleLink" href="javascript:;">' + toggleText + '</a></li>');
	else {
		$('#p-personal ul').prepend('<li id="toggleContainer" class="mw-list-item"><a id="toggleLink" style="background:none" href="javascript:;" title="Toggle night theme"><img id="toggleImage" src="https://wiki.gallery/images/dark/moon.png"/></a></li>');
	}

	$('#toggleLink').click(function() {
		if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
			if (isDark)
				setCookie('darkTheme', 'off', 999);
			else
				setCookie('darkTheme', '', -1);
		} else {
			if (isDark)
				setCookie('darkTheme', '', -1);
			else
				setCookie('darkTheme', 'on', 999);
		}
		if (mw.config.get('wgUserId') || location.href.includes('?'))
			location.reload();
		else
			location = location.href + '?toggle';
	});
});