Greasy Fork

kinorium automatic theme

Automatically applies a dark/light theme matching a system theme on kinorium.com

目前为 2022-09-02 提交的版本。查看 最新版本

// ==UserScript==
// @name        kinorium automatic theme
// @name:ru     автоматическая тема для kinorium
// @description Automatically applies a dark/light theme matching a system theme on kinorium.com
// @description:ru Автоматически переключает темную/светлую тему на кинориуме в соответствии с системной темой
// @namespace   https://github.com/zenwarr
// @match       *://*.kinorium.com/*
// @grant       none
// @version     1.0
// @author      zenwarr
// @license     MIT
// ==/UserScript==

const mediaQuery = matchMedia("(prefers-color-scheme: dark)");
setTheme(mediaQuery.matches);
mediaQuery.addEventListener("change", q => setTheme(q.matches));

async function setTheme(isDark) {
  const themeCookie = await cookieStore.get("theme");
  const newThemeName = isDark ? "dark" : "light";
  if (!themeCookie) {
    const expire = new Date().valueOf() + 1000 * 60 * 60 * 24 * 30;
    await cookieStore.set({ name: "theme", value: newThemeName, path: "/", sameSite: "strict", secure: true, expires: expire });
    document.location.reload();
  } else if (themeCookie.value !== newThemeName) {
    await cookieStore.set({ ...themeCookie, value: newThemeName });
    document.location.reload();
  }
}