Last active
February 4, 2023 06:38
-
-
Save 13xforever/c76ef4d8e874e8e51f66cb62003970a7 to your computer and use it in GitHub Desktop.
Tampermonkey script to automatically toggle Light/Dark theme on J-Novel Club website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name J-Novel Club Auto Theme | |
// @version 0.1 | |
// @description Automatically switches between light and dark themes on j-novel.club based on system settings. | |
// @author 13xforever | |
// @match https://j-novel.club/* | |
// @run-at document-idle | |
// @license MIT | |
// ==/UserScript== | |
(function() { | |
"use strict"; | |
if (!window.matchMedia) { | |
return; | |
} | |
const LIGHT = "Let there be light!"; | |
const DARK = "Lights out!"; | |
const currentThemeSelector = 'div.theme div.button-group div.disabled'; | |
const switchSelector = 'div.theme div.button-group div.type-text'; | |
function checkMode(q) { | |
try { | |
let currentNightMode = document.querySelector(currentThemeSelector).title; | |
let newNightMode = q.matches ? DARK : LIGHT; | |
if (newNightMode !== currentNightMode) { | |
document.querySelector(switchSelector).click(); | |
} | |
} catch (ex) { | |
console.log(`Failed: ${ex}`); | |
} | |
} | |
const query = window.matchMedia('(prefers-color-scheme: dark)'); | |
checkMode(query); | |
query.addEventListener('change', checkMode); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment