Danbooru theme toggle (sunrise API)

Automatically toggle native dark mode on sunset and sunrise on danbooru.donmai.us

As of 24.03.2023. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Danbooru theme toggle (sunrise API)
// @namespace    Danbooru
// @version      1
// @description  Automatically toggle native dark mode on sunset and sunrise on danbooru.donmai.us
// @author       Dramorian, fredgido
// @match        https://danbooru.donmai.us/*
// @run-at       document-start
// @license      MIT
// ==/UserScript==

// Define the URL for the API that provides sunrise and sunset times
const apiUrl = "https://api.sunrise-sunset.org/json?lat=&lng=&formatted=0";

// Fetch the sunrise and sunset times from the API
fetch(apiUrl)
  .then(response => response.json())
  .then(data => {
    // Extract the sunrise and sunset times from the API response
    const sunrise = new Date(data.results.sunrise).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit', hour12: false});
    const sunset = new Date(data.results.sunset).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit', hour12: false});

    // Get the current time in hh:mm format
    const currentTime = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit', hour12: false});

    // Check if the current time is between sunset and sunrise
    const isNightTime = (currentTime >= sunset || currentTime < sunrise);

    // Update the theme based on the time
    const currentThemeIsDark = Danbooru.CurrentUser.darkMode();
    if((isNightTime && !currentThemeIsDark) || (!isNightTime && currentThemeIsDark) ){
      Danbooru.CurrentUser.update({theme: isNightTime ? "dark" : "light"}).then(() => {
        Danbooru.Utility.notice("Theme updated.");
        window.location = window.location;
      });
    }
  })
  .catch(error => console.error(error));