From a3010ae15509dd688fae43dc2957732d328cf454 Mon Sep 17 00:00:00 2001 From: Erzbir Date: Thu, 9 Jul 2026 17:34:13 +0800 Subject: [PATCH] feat: add expiration for visitor theme preferences --- README.md | 21 ++++++---- settings.yaml | 6 +++ src/module/header.js | 28 ++++++++++--- templates/modules/layout.html | 79 +++++++++++++++++++++++++++-------- 4 files changed, 102 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index fd724dc..9140ecd 100644 --- a/README.md +++ b/README.md @@ -79,18 +79,21 @@ 3. 启用自定义字体且未开启系统字体时使用上传的字体 4. 其他情况使用内置 `JetBrains Mono` -访客手动选择的模式, 配色和像素风格会保存在浏览器的 `localStorage` 中 +访客手动选择的模式, 配色和像素风格会保存在浏览器的 `localStorage` 中. 默认有效期为 24 小时, +可通过 `style.prefer_ttl` 调整,设为 `0` 时永久保存. 偏好过期后会在下次访问时恢复默认值, +已有偏好数据会在升级后的首次访问时开始计算有效期 ### 配色设置 -| 设置 | 配置名 | 说明 | -|---------|--------------------------------------|---------------------| -| 默认配色模式 | `style.default_scheme_mode` | 可选深色, 浅色或跟随系统 | -| 访客配色切换 | `style.color_scheme_switcher_enable` | 在页头显示可用配色列表 | -| 深色配色 | `style.dark_color_scheme` | 深色模式使用的内置或自定义配色 | -| 浅色配色 | `style.light_color_scheme` | 浅色模式使用的内置或自定义配色 | -| 自定义深色名称 | `style.custom_dark` | 深色配色选择 `custom` 时填写 | -| 自定义浅色名称 | `style.custom_light` | 浅色配色选择 `custom` 时填写 | +| 设置 | 配置名 | 说明 | +|---------|--------------------------------------|-----------------------------| +| 默认配色模式 | `style.default_scheme_mode` | 可选深色, 浅色或跟随系统 | +| 访客偏好有效期 | `style.prefer_ttl` | 模式, 配色和像素风格的保存小时数, `0` 表示永久 | +| 访客配色切换 | `style.color_scheme_switcher_enable` | 在页头显示可用配色列表 | +| 深色配色 | `style.dark_color_scheme` | 深色模式使用的内置或自定义配色 | +| 浅色配色 | `style.light_color_scheme` | 浅色模式使用的内置或自定义配色 | +| 自定义深色名称 | `style.custom_dark` | 深色配色选择 `custom` 时填写 | +| 自定义浅色名称 | `style.custom_light` | 浅色配色选择 `custom` 时填写 | 访客配色列表会从页面样式表中自动发现 `html[theme-color-scheme='name']` 选择器, 列表中的 `default` 用于清除访客选择并恢复主题设置, 因此不要将 `default` 用作自定义配色名称 diff --git a/settings.yaml b/settings.yaml index 580a920..3b894a5 100644 --- a/settings.yaml +++ b/settings.yaml @@ -64,6 +64,12 @@ spec: value: "light" - label: "跟随系统" value: "system" + - $formkit: number + name: prefer_ttl + label: 访客偏好有效期 (小时) + value: 24 + min: 0 + help: 访客手动选择的模式, 配色和像素风格过期后会在下次访问时恢复默认值, 设为 0 表示永久保存 - $formkit: checkbox name: color_scheme_switcher_enable label: 启用访客配色切换 diff --git a/src/module/header.js b/src/module/header.js index 5a421e9..76fc455 100644 --- a/src/module/header.js +++ b/src/module/header.js @@ -5,6 +5,20 @@ const COLOR_SCHEME_STORAGE_KEY = "theme-color-scheme"; const PIXEL_STYLE_STORAGE_KEY = "pixel_style"; const COLOR_SCHEME_SELECTOR_PATTERN = /html\[theme-color-scheme\s*=\s*(['"]?)([^'"\]\s]+)\1\]/g; +function getStoredPreference(key) { + return window.TerminalTheme?.storage + ? window.TerminalTheme.storage.get(key) + : localStorage.getItem(key); +} + +function setStoredPreference(key, value) { + if (window.TerminalTheme?.storage) { + window.TerminalTheme.storage.set(key, value); + return; + } + localStorage.setItem(key, value); +} + function registerSearchButton() { const searchButton = document.querySelector('[data-header-action="open-search"]'); if (!searchButton) return; @@ -25,7 +39,7 @@ function registerThemeToggle() { const nextMode = currentScheme === "dark" ? "light" : "dark"; window.applyTheme(nextMode, {clearColorScheme: true}); - localStorage.setItem(THEME_MODE_STORAGE_KEY, nextMode); + setStoredPreference(THEME_MODE_STORAGE_KEY, nextMode); syncColorSchemePicker(); }); } @@ -82,13 +96,13 @@ function syncColorSchemePicker() { const picker = document.getElementById("color-scheme-picker"); if (!picker) return; - const storedScheme = localStorage.getItem(COLOR_SCHEME_STORAGE_KEY); + const storedScheme = getStoredPreference(COLOR_SCHEME_STORAGE_KEY); const options = Array.from(picker.querySelectorAll("[data-color-scheme]")); const hasStoredOption = storedScheme && options .some(option => option.dataset.colorScheme === storedScheme); if (storedScheme && !hasStoredOption) { - const storedMode = localStorage.getItem(THEME_MODE_STORAGE_KEY) + const storedMode = getStoredPreference(THEME_MODE_STORAGE_KEY) || window.TerminalThemeConfig?.defaultMode || "system"; @@ -206,10 +220,12 @@ function registerPixelToggle() { if (!pixelToggle) return; pixelToggle.addEventListener("click", function () { - const storedStatus = localStorage.getItem(PIXEL_STYLE_STORAGE_KEY); - const newStatus = storedStatus === "true" ? "false" : "true"; + const storedStatus = getStoredPreference(PIXEL_STYLE_STORAGE_KEY); + const currentStatus = storedStatus + || document.documentElement.getAttribute(PIXEL_STYLE_STORAGE_KEY); + const newStatus = currentStatus === "true" ? "false" : "true"; document.documentElement.setAttribute(PIXEL_STYLE_STORAGE_KEY, newStatus); - localStorage.setItem(PIXEL_STYLE_STORAGE_KEY, newStatus); + setStoredPreference(PIXEL_STYLE_STORAGE_KEY, newStatus); window.TerminalTheme?.syncPixelFont?.(); }); } diff --git a/templates/modules/layout.html b/templates/modules/layout.html index 67a32d2..247f75d 100644 --- a/templates/modules/layout.html +++ b/templates/modules/layout.html @@ -16,6 +16,7 @@ customDarkTheme: /*[[${theme.config.style.custom_dark}]]*/ "", customLightTheme: /*[[${theme.config.style.custom_light}]]*/ "", defaultMode: /*[[${theme.config.style.default_scheme_mode}]]*/ "system", + preferTTL: /*[[${theme.config.style.prefer_ttl ?: 24}]]*/ 24, pixelStyle: /*[[${theme.config.style.pixel_style}]]*/ false, pixelCssHref: /*[[@{/assets/dist/pixel.css?v={version}(version=${theme.spec.version})}]]*/ "", customCss: /*[[${theme.config.style.custom}]]*/ "" @@ -30,6 +31,49 @@ pixelStyle: "pixel_style", colorScheme: "theme-color-scheme" }; + const expirationSuffix = "-expires-at"; + const expirationHours = Number(config.preferTTL); + const storageTtl = Number.isFinite(expirationHours) && expirationHours > 0 + ? expirationHours * 60 * 60 * 1000 + : 0; + + function removeStoredPreference(key) { + localStorage.removeItem(key); + localStorage.removeItem(key + expirationSuffix); + } + + function getStoredPreference(key) { + const value = localStorage.getItem(key); + if (value === null) return null; + + const expirationKey = key + expirationSuffix; + if (!storageTtl) { + localStorage.removeItem(expirationKey); + return value; + } + + const expiresAt = Number(localStorage.getItem(expirationKey)); + if (!Number.isFinite(expiresAt) || expiresAt <= 0) { + localStorage.setItem(expirationKey, String(Date.now() + storageTtl)); + return value; + } + + if (expiresAt <= Date.now()) { + removeStoredPreference(key); + return null; + } + + return value; + } + + function setStoredPreference(key, value) { + localStorage.setItem(key, value); + if (storageTtl) { + localStorage.setItem(key + expirationSuffix, String(Date.now() + storageTtl)); + } else { + localStorage.removeItem(key + expirationSuffix); + } + } function resolveTheme(mode) { if (mode === "light") { @@ -39,7 +83,7 @@ } function getStoredColorScheme() { - const scheme = localStorage.getItem(storageKeys.colorScheme); + const scheme = getStoredPreference(storageKeys.colorScheme); return scheme && scheme !== "default" ? scheme : ""; } @@ -49,7 +93,7 @@ function applyTheme(mode, options = {}) { if (options.clearColorScheme) { - localStorage.removeItem(storageKeys.colorScheme); + removeStoredPreference(storageKeys.colorScheme); } const actualMode = mode === "system" ? getSystemScheme() : mode; @@ -59,12 +103,12 @@ function applyColorScheme(scheme) { if (!scheme || scheme === "default") { - localStorage.removeItem(storageKeys.colorScheme); + removeStoredPreference(storageKeys.colorScheme); syncThemeMode(); return; } - localStorage.setItem(storageKeys.colorScheme, scheme); + setStoredPreference(storageKeys.colorScheme, scheme); root.setAttribute("theme-color-scheme", scheme); } @@ -73,12 +117,8 @@ } function syncPixelStyle() { - let storedStatus = localStorage.getItem(storageKeys.pixelStyle); - - if (!storedStatus) { - storedStatus = String(config.pixelStyle); - localStorage.setItem(storageKeys.pixelStyle, storedStatus); - } + const storedStatus = getStoredPreference(storageKeys.pixelStyle) + || String(config.pixelStyle); root.setAttribute("pixel_style", storedStatus); } @@ -112,12 +152,9 @@ } function syncThemeMode() { - let storedMode = localStorage.getItem(storageKeys.themeMode); - - if (!storedMode) { - storedMode = config.defaultMode || "system"; - localStorage.setItem(storageKeys.themeMode, storedMode); - } + const storedMode = getStoredPreference(storageKeys.themeMode) + || config.defaultMode + || "system"; applyTheme(storedMode); } @@ -127,6 +164,11 @@ applyTheme, applyColorScheme, applyCustomCss, + storage: { + get: getStoredPreference, + set: setStoredPreference, + remove: removeStoredPreference + }, syncPixelFont, syncPixelStyle }; @@ -137,7 +179,10 @@ syncPixelFont(); window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => { - if (localStorage.getItem(storageKeys.themeMode) === "system") { + const mode = getStoredPreference(storageKeys.themeMode) + || config.defaultMode + || "system"; + if (mode === "system") { applyTheme("system"); } });