MediaWiki:Common.js

From Mistbound
Revision as of 11:12, 28 June 2026 by Admin (talk | contribs) (Add clickable light/dark theme toggle)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump 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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function () {
	function currentTheme() {
		var h = document.documentElement;
		if (h.classList.contains('theme-dark')) { return 'dark'; }
		if (h.classList.contains('theme-light')) { return 'light'; }
		return ( window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ) ? 'dark' : 'light';
	}

	function applyTheme(t) {
		var h = document.documentElement;
		h.classList.remove('theme-dark', 'theme-light');
		h.classList.add('theme-' + t);
		try { localStorage.setItem('mb-theme', t); } catch (e) {}
	}

	mw.loader.using('mediawiki.util').then(function () {
		var link = mw.util.addPortletLink(
			'p-personal', '#', 'Theme', 'pt-theme', 'Switch between light and dark theme'
		);
		if (!link) { return; }
		link.addEventListener('click', function (e) {
			e.preventDefault();
			applyTheme(currentTheme() === 'dark' ? 'light' : 'dark');
		});
	});
}());