Skip to content

Instantly share code, notes, and snippets.

@noaione
Created June 3, 2024 15:48
Show Gist options
  • Save noaione/09a2a367fcd3d5128b963bfe052a1aa3 to your computer and use it in GitHub Desktop.
Save noaione/09a2a367fcd3d5128b963bfe052a1aa3 to your computer and use it in GitHub Desktop.
can someone turn on the light? no?
// ==UserScript==
// @name Nyaa Always Dark
// @namespace noaione-nyaa-script
// @version 0.1.0
// @description Force always darkmode
// @author N4O
// @match https://nyaa.si/*
// @match https://sukebei.nyaa.si/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=nyaa.si
// @grant GM.getValue
// @grant GM.setValue
// ==/UserScript==
(function() {
"use strict";
function getDarkThemeUrl() {
const t = Math.ceil((new Date()).getTime() / 1000);
return `/static/css/bootstrap-dark.min.css?t=${t}`;
}
// Using window.setThemeDark is unrealiable, it's better if we did it ourselves.
function setThemeDarkProxy() {
const themeLink = document.getElementById("bsThemeLink");
const body = document.body;
themeLink.href = getDarkThemeUrl();
try {
localStorage.setItem("theme", "dark");
} catch (e) {console.error("[NyaaAlwaysDark] setThemeDarkProxy(): unable to set dark localStorage, ignoring...")};
if (body !== null) {
body.classList.add("dark");
} else {
console.warn("[NyaaAlwaysDark] setThemeDarkProxy(): body is missing???")
}
}
function main() {
// check local storage
const isDark = localStorage.getItem("theme") === "dark";
if (!isDark) setThemeDarkProxy();
}
// on load
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoad", () => {
main();
})
} else {
main();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment