Auto Dark Mode for SimpleLogin

Automatically switch the theme between light and dark, based on the browser’s color scheme preference.

// ==UserScript==
// @name               Auto Dark Mode for SimpleLogin
// @name:zh-TW         SimpleLogin 自動黑暗模式
// @description        Automatically switch the theme between light and dark, based on the browser’s color scheme preference.
// @description:zh-TW  根據瀏覽器的佈景主題設定,自動從明亮和黑暗模式間切換。
// @icon               https://icons.duckduckgo.com/ip3/app.simplelogin.io.ico
// @author             Jason Kwok
// @namespace          https://jasonhk.dev/
// @version            1.1.0
// @license            MIT
// @match              https://app.simplelogin.io/*
// @run-at             document-idle
// @inject-into        page
// @grant              none
// @require            https://update.greasyfork.org/scripts/483122/1303118/style-shims.js
// @supportURL         https://greasyfork.org/scripts/449445/feedback
// ==/UserScript==

GM.addStyle(`
    html[data-theme=dark] .header-brand
    {
        filter: grayscale(1) brightness(10);
    }
`);

if (GM.info.scriptHandler === "Greasemonkey")
{
    window.setCookie = window.eval("setCookie");
}

const query = matchMedia("(prefers-color-scheme: dark)");

query.addEventListener("change", updateTheme);
updateTheme(query);

function updateTheme({ matches: isDarkMode })
{
    const theme = isDarkMode ? "dark" : "light";

    if (document.documentElement.dataset.theme !== theme)
    {
        setCookie("dark-mode", String(isDarkMode), 30);
        document.documentElement.dataset.theme = theme;
    }
}