User:SD0001/dark-mode-toggle.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:SD0001/dark-mode-toggle. |
/**
* Enables or disables the dark-mode gadget.
*
* Authors: [[User:SD0001]], [[User:Nardog]]
*/
mw.messages.set({
'darkmode-turn-on-label': 'Dark mode',
'darkmode-turn-on-tooltip': 'Turn on dark mode',
'darkmode-turn-off-label': 'Light mode',
'darkmode-turn-off-tooltip': 'Turn off dark mode',
});
$.when($.ready, mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.Uri'])).then(function() {
var inDarkMode = !!mw.user.options.get('gadget-dark-mode');
var onOrOff = inDarkMode ? 'off' : 'on';
var label = mw.msg('darkmode-turn-' + onOrOff + '-label');
var tooltip = mw.msg('darkmode-turn-' + onOrOff + '-tooltip');
mw.util.addPortletLink('p-cactions', '#', label, 'pt-darkmode', tooltip);
$('#pt-darkmode').on('click', function(e) {
e.preventDefault();
var darkMode = !mw.user.options.get('gadget-dark-mode');
new mw.Api().saveOption('gadget-dark-mode', darkMode ? '1' : '0');
var onOrOff = darkMode ? 'off' : 'on';
$('#pt-darkmode span').text(mw.msg('darkmode-turn-' + onOrOff + '-label'));
$('#pt-darkmode a').attr('title', mw.msg('darkmode-turn-' + onOrOff + '-tooltip'));
mw.user.options.set('gadget-dark-mode', darkMode ? 1 : 0);
if (mw.loader.moduleRegistry['ext.gadget.dark-mode'].style) {
var css = mw.loader.moduleRegistry['ext.gadget.dark-mode'].style.css[0];
$('style').filter(function () {
return this.textContent.indexOf(css) !== -1;
}).prop('disabled', !darkMode);
} else {
if (darkMode) {
mw.loader.load('ext.gadget.dark-mode');
} else {
$('link[rel="stylesheet"][href*="dark-mode"]').attr('href', function() {
var uri = new mw.Uri(this.href);
uri.query.modules = uri.query.modules
.replace('ext.gadget.dark-mode', 'ext.gadget.') // dark-mode is first in the gadget list
.replace(/,dark-mode,/, ',') // dark-mode is in middle of the list
.replace(/,dark-mode$/, ''); // dark-mode is at the end of list
return uri.getRelativePath();
});
mw.loader.moduleRegistry['ext.gadget.dark-mode'].state = 'registered'; // unset "ready" state
}
}
});
});