Greasy Fork is available in English.

Baidu深色模式(简易版)

一个简易版的Baidu自动深色模式

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Baidu深色模式(简易版)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  一个简易版的Baidu自动深色模式
// @author       tutu辣么可爱(greasyfork)/IcedWatermelonJuice(github)
// @run-at       document-start
// @match        *://*.baidu.com/*
// @icon         https://www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
	'use strict';
	var htmlAttr = function(k, v) {
			document.querySelector("html").setAttribute(k, v);
		},
		site = location.host.split(".")[0].trim(),
		mode = {
			on: function() {
				htmlAttr("theme", "dark");
			},
			off: function() {
				htmlAttr("theme", "normal");
			},
			auto: function() {
				var that = this,
					mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'),
					themeChange = function() {
						mediaQuery.matches ? that.on() : that.off()
					}
				themeChange(); // 判断当前模式
				mediaQuery.onchange = themeChange; // 监听模式变化
			}
		},
		css = `
			html[theme=dark]:not([site=unknown]),
			html[theme=dark]:not([site=unknown]) img,
			html[theme=dark]:not([site=unknown]) picture,
			html[theme=dark]:not([site=unknown]) video,
			html[theme=dark][site=www] .c-title::before,
			html[theme=dark][site=www] .c-title a,
			html[theme=dark][site=www] .c-icon,
			html[theme=dark][site=www] input[type=submit],
			html[theme=dark][site=image] input[type=submit],
			html[theme=dark][site=baike] .cmn-baike-logo,
			html[theme=dark][site=baike] button#search,
			html[theme=dark][site=baike] .navbar-content-box,
			html[theme=dark][site=baike] .add-video,
			html[theme=dark][site=zhidao] .logo,
			html[theme=dark][site=zhidao] button#search-btn,
			html[theme=dark][site=zhidao] .item-text,
			html[theme=dark][site=zhidao] .wgt-replyer-all-avatar{
				filter: invert(1) hue-rotate(180deg);
			}
			html[theme=dark][site=zhidao] body{
				background-color: white;
			}
		`;
	// 预处理
	htmlAttr("site", /^www|image|baike|zhidao$/i.test(site) ? site : "unknown");
	// 加载CSS
	GM_addStyle(css);
	mode.auto();
})();