Skip to content

Instantly share code, notes, and snippets.

@Nathaniel-Wu
Last active June 5, 2023 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Nathaniel-Wu/b2e6490f5ac7c35dc0cd902fda851b36 to your computer and use it in GitHub Desktop.
Save Nathaniel-Wu/b2e6490f5ac7c35dc0cd902fda851b36 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Zhihu Auto Dark Mode
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Automatically toggle hidden built-in dark mode on zhihu.com
// @author Nathaniel Wu
// @include *.zhihu.com/*
// @exclude *link.zhihu.com/*
// @exclude *www.zhihu.com/question/*/log
// @exclude *www.zhihu.com/people/*/logs
// @license Apache-2.0
// @supportURL https://gist.github.com/Nathaniel-Wu/b2e6490f5ac7c35dc0cd902fda851b36
// @grant none
// ==/UserScript==
(function () {
'use strict';
const setDarkMode = on => {
let alreadyOn = document.querySelector('html').getAttribute('data-theme') == 'dark';
if ((alreadyOn && (!on)) || ((!alreadyOn) && on)) {
let keyword, keyword_replacement;
if (on) {
keyword = 'theme=light';
keyword_replacement = 'theme=dark';
} else {
keyword = 'theme=dark';
keyword_replacement = 'theme=light';
}
if (window.location.href.match(/theme=(dark|light)/))
window.location.href = window.location.href.replace(keyword, keyword_replacement);
else {
if (window.location.href.match(/\?[^=]+=/))
window.location.href = window.location.href + `&${keyword_replacement}`;
else
window.location.href = window.location.href + `?${keyword_replacement}`;
}
}
}
const in_iframe = () => {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
if (!in_iframe()) {
if (window.matchMedia) {// if the browser/os supports system-level color scheme
setDarkMode(window.matchMedia('(prefers-color-scheme: dark)').matches);
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => setDarkMode(e.matches));
} else {// otherwise use local time to decide
let hour = (new Date()).getHours();
setDarkMode(hour > 18 || hour < 8);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment