Skip to content

Instantly share code, notes, and snippets.

@13xforever
Last active February 4, 2023 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 13xforever/c76ef4d8e874e8e51f66cb62003970a7 to your computer and use it in GitHub Desktop.
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
// ==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