Last active 3 weeks ago

README.md Raw

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 Raw
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})();