Geeksforgeeks auto dark night mode - geeksforgeeks.org

sets Geekforgeeks dark theme auto.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Geeksforgeeks auto dark night mode - geeksforgeeks.org
// @namespace   marcusmors.com
// @match       https://www.geeksforgeeks.org/*
// @grant       none
// @version     1
// @author      Jose Vilca <@marcusmors>
// @description sets Geekforgeeks dark theme auto.
// @icon        https://www.google.com/s2/favicons?domain=geeksforgeeks.org
// @license     MIT
// ==/UserScript==

// cookie options: gfgThemeLight or gfgThemeDark //last checked 25/09/2022

function get_cookie_value(cookie_key) {
	let name = cookie_key + "="
	let ca = document.cookie.split(";")
	for (let i = 0; i < ca.length; i++) {
		let c = ca[i].trim()
		if (c.indexOf(name) == 0) {
			return c.substr(name.length)
		}
	}
	return null
	// https://stackoverflow.com/questions/23641531/get-a-cookie-value-javascript/23642134#23642134
}

function set_to_dark() {
	"use strict"
	const theme = get_cookie_value("gfg_theme") === "gfgThemeDark" ? "dark" : "light"

	if (theme === "dark") return

	const menu = document.querySelector(".hamburger-menu")
	if (menu) {
		// click the dark mode button inside the menu.
		menu.click()
		document.querySelector(".toggle-darkMode").click()
		menu.click()
		return
	}
	document.querySelectorAll('[data-gfg-action="toggleGFGTheme"]')[1].click()
}

jQuery(document).ready(set_to_dark)