Wikipedia Force Automatic Color Mode
This userscript forces Wikipedia to use the system color scheme instead of defaulting to the light style.
wikipedia-dark-mode.user.js
· 1.0 KiB · JavaScript
Raw
// ==UserScript==
// @name Force Wikipedia Theme to OS
// @namespace Violentmonkey Scripts
// @icon
// @version 1.0.0
//
// @match https://*.wikipedia.org/*
// @match https://*.wikimedia.org/*
// @grant none
//
// @author -
// @description
// ==/UserScript==
(function () {
'use strict';
// 1. Immediately update class on <html> to avoid initial white flash
const docEl = document.documentElement;
docEl.classList.remove('skin-theme-clientpref-day', 'skin-theme-clientpref-night');
docEl.classList.add('skin-theme-clientpref-os');
// 2. Invoke MediaWiki clientPrefs API once loaded to save state
function setMwPref() {
if (window.mw && window.mw.user && window.mw.user.clientPrefs) {
window.mw.user.clientPrefs.set('skin-theme', 'os');
} else {
setTimeout(setMwPref, 20);
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', setMwPref);
} else {
setMwPref();
}
})();
| 1 | // ==UserScript== |
| 2 | // @name Force Wikipedia Theme to OS |
| 3 | // @namespace Violentmonkey Scripts |
| 4 | // @icon |
| 5 | // @version 1.0.0 |
| 6 | // |
| 7 | // @match https://*.wikipedia.org/* |
| 8 | // @match https://*.wikimedia.org/* |
| 9 | // @grant none |
| 10 | // |
| 11 | // @author - |
| 12 | // @description |
| 13 | // ==/UserScript== |
| 14 | |
| 15 | (function () { |
| 16 | 'use strict'; |
| 17 | |
| 18 | // 1. Immediately update class on <html> to avoid initial white flash |
| 19 | const docEl = document.documentElement; |
| 20 | docEl.classList.remove('skin-theme-clientpref-day', 'skin-theme-clientpref-night'); |
| 21 | docEl.classList.add('skin-theme-clientpref-os'); |
| 22 | |
| 23 | // 2. Invoke MediaWiki clientPrefs API once loaded to save state |
| 24 | function setMwPref() { |
| 25 | if (window.mw && window.mw.user && window.mw.user.clientPrefs) { |
| 26 | window.mw.user.clientPrefs.set('skin-theme', 'os'); |
| 27 | } else { |
| 28 | setTimeout(setMwPref, 20); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | if (document.readyState === 'loading') { |
| 33 | document.addEventListener('DOMContentLoaded', setMwPref); |
| 34 | } else { |
| 35 | setMwPref(); |
| 36 | } |
| 37 | })(); |