This is an old revision of this page, as edited by MusikAnimal(talk | contribs) at 16:47, 21 December 2021(add note that messages must match the ::before content in the CSS). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.Revision as of 16:47, 21 December 2021 by MusikAnimal(talk | contribs)(add note that messages must match the ::before content in the CSS)
This page is loaded as a part of the dark-mode-toggle gadget, used by 89,143 users.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/** * Enables or disables the dark-mode gadget. * * Authors: [[User:SD0001]], [[User:Nardog]] */// 'Dark mode' and 'Light mode' messages must match the ::before content in [[MediaWiki:Gadget-dark-mode-toggle-pagestyles.css]] and [[MediaWiki:Gadget-dark-mode.css]], respectively.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','mediawiki.storage','es6-polyfills'])).then(function(){varinDarkMode=!!mw.user.options.get('gadget-dark-mode');varonOrOff=inDarkMode?'off':'on';varlabel=mw.msg('darkmode-turn-'+onOrOff+'-label');vartooltip=mw.msg('darkmode-turn-'+onOrOff+'-tooltip');varnextnode=mw.config.get('skin')==='minerva'?null:'#pt-watchlist';mw.util.addPortletLink('p-personal','#',label,'pt-darkmode',tooltip,'',nextnode);functiontoggleMode(){vardarkMode=!mw.user.options.get('gadget-dark-mode');newmw.Api().saveOption('gadget-dark-mode',darkMode?'1':'0');mw.user.options.set('gadget-dark-mode',darkMode?1:0);varonOrOff=darkMode?'off':'on';// Toggle portlet link label and tooltipvarlabelSelector=['vector','minerva'].includes(mw.config.get('skin'))?'#pt-darkmode span:not(.mw-ui-icon)':'#pt-darkmode a';$(labelSelector).text(mw.msg('darkmode-turn-'+onOrOff+'-label'));$('#pt-darkmode a').attr('title',mw.msg('darkmode-turn-'+onOrOff+'-tooltip'));// Modify the <link> element on the page to include/exclude dark-mode styles// We can't use mw.loader as it doesn't work both ways (see talk page)vargadgetsLinkElement=$('link[rel="stylesheet"][href*="ext.gadget."]').get(0);if(gadgetsLinkElement){varuri=newmw.Uri(gadgetsLinkElement.href);if(darkMode){uri.query.modules+=',dark-mode';}else{uri.query.modules=uri.query.modules.replace('ext.gadget.dark-mode','ext.gadget.')// dark-mode is first in the gadget list.replace(/,dark-mode(,|$)/,'$1');// dark-mode is in middle or end of the list}gadgetsLinkElement.href=uri.getRelativePath();}else{// No gadgets containing styles are enabled$('<link>').attr('rel','stylesheet').attr('href',mw.util.wikiScript('load')+'?lang='+mw.config.get('wgUserLanguage')+'&modules=ext.gadget.dark-mode&only=styles&skin='+mw.config.get('skin')).appendTo('head');}}$('#pt-darkmode').on('click',function(e){e.preventDefault();toggleMode();});if(window.wpDarkModeAutoToggle){vartoggleBasedOnSystemColourScheme=function(){varsystemSchemeNow=matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light';varsystemSchemeLast=mw.storage.get('dark-mode-system-scheme')||'light';varwpSchemeNow=mw.user.options.get('gadget-dark-mode')?'dark':'light';if(systemSchemeNow!==systemSchemeLast&&systemSchemeNow!==wpSchemeNow){toggleMode();}mw.storage.set('dark-mode-system-scheme',systemSchemeNow);};toggleBasedOnSystemColourScheme();// If system colour scheme changes while user is viewing, toggle immediatelyvarmediaQuery=matchMedia('(prefers-color-scheme: dark)');if(mediaQuery.addEventListener){mediaQuery.addEventListener('change',toggleBasedOnSystemColourScheme);}elseif(mediaQuery.addListener){// Safari 13 and oldermediaQuery.addListener(toggleBasedOnSystemColourScheme);}}});