From c8b16e39a34313497cad238a8f6caf0286d7b468 Mon Sep 17 00:00:00 2001 From: ailoooong Date: Sat, 15 Feb 2025 15:56:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E6=96=87=E6=A1=A3=E9=A3=8E=E6=A0=BC?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=AD=98=E5=82=A8=E5=9C=A8cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/js/docdetail.js | 43 ++++++++++++++++++++++++++++++++--------- static/js/init-theme.js | 26 +++++++++++++++++++------ 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/static/js/docdetail.js b/static/js/docdetail.js index 4c1ed9e..4423dc0 100644 --- a/static/js/docdetail.js +++ b/static/js/docdetail.js @@ -87,38 +87,63 @@ setTimeout(function () { color: '#FEB32A' }); }, 100); + +// 获取 cookie +function getCustomCookie(key) { + const name = `${encodeURIComponent(key)}=`; + const decodedCookies = decodeURIComponent(document.cookie); + const cookies = decodedCookies.split('; '); + for (let cookie of cookies) { + if (cookie.startsWith(name)) { + return cookie.substring(name.length); + } + } + + return null; +} +// 设置 cookie +function setCustomCookie(key, value, day = 1, domain = location.hostname) { + const expires = new Date(); + expires.setTime(expires.getTime() + day * 24 * 60 * 60 * 1000); + const cookie = `${encodeURIComponent(key)}=${encodeURIComponent( + value + )}; expires=${expires.toUTCString()}; path=/; domain=${domain}`; + document.cookie = cookie; +} + + // 皮肤主题切换 -$(function ($) { - const themeStyle = localStorage.getItem('lookeng-theme'); +(function () { + const domain = '.openlookeng.io'; + const APPEARANCE_KEY = 'openlookeng-theme-appearance'; + const themeStyle = getCustomCookie(APPEARANCE_KEY); const html = $('html'); if (!themeStyle) { $('.theme-change i').removeClass('light dark').addClass('light'); $('.title-h2 .icon-help').removeClass('dark'); $('.nav-menu a .h5-logo').removeClass('dark'); - html.removeClass('light dark').addClass('light'); - localStorage.getItem('lookeng-theme', 'light'); + html.removeClass('light dark').addClass('light'); } else { $('.theme-change i').removeClass('light dark').addClass(themeStyle); $('.title-h2 .icon-help').addClass(themeStyle); - $('.nav-menu a .h5-logo').addClass(themeStyle); - html.removeClass('light dark').addClass(themeStyle); + $('.nav-menu a .h5-logo').addClass(themeStyle); } $('.theme-change i').click(function () { if ($(this).hasClass('light')) { $('.title-h2 .icon-help').addClass('dark'); $('.nav-menu a .h5-logo').addClass('dark'); $(this).addClass('dark').removeClass('light'); - localStorage.setItem('lookeng-theme', 'dark'); + setCustomCookie(APPEARANCE_KEY, 'dark', 180, domain); html.addClass('dark').removeClass('light'); } else { $('.nav-menu a .h5-logo').removeClass('dark'); $('.title-h2 .icon-help').removeClass('dark'); $(this).addClass('light').removeClass('dark'); - localStorage.setItem('lookeng-theme', 'light'); + setCustomCookie(APPEARANCE_KEY, 'light', 180, domain); html.addClass('light').removeClass('dark'); } }); -}); +})() window.onload = function () { const lang = location.pathname.split('/')[1]; diff --git a/static/js/init-theme.js b/static/js/init-theme.js index 9ade9f4..f143110 100644 --- a/static/js/init-theme.js +++ b/static/js/init-theme.js @@ -1,11 +1,25 @@ /** - * @file 换肤 + * @file 换肤初始化 */ -const themeStyle = localStorage.getItem('lookeng-theme'); -const html = document.getElementsByTagName('html')[0]; +// 获取 cookie +function getCustomCookie(key) { + const name = `${encodeURIComponent(key)}=` + const decodedCookies = decodeURIComponent(document.cookie) + const cookies = decodedCookies.split('; ') + for (let cookie of cookies) { + if (cookie.startsWith(name)) { + return cookie.substring(name.length) + } + } + + return null +} +const APPEARANCE_KEY = 'openlookeng-theme-appearance' + +const themeStyle = getCustomCookie(APPEARANCE_KEY) +const html = document.getElementsByTagName('html')[0] if (!themeStyle) { - localStorage.getItem('lookeng-theme', 'light'); - html.classList.add('light'); + html.classList.add('light') } else { - html.classList.add(themeStyle); + html.classList.add(themeStyle) } -- Gitee From 8944044212de23f902a7cb96498063dad000a49c Mon Sep 17 00:00:00 2001 From: ailoooong Date: Mon, 17 Feb 2025 11:25:19 +0800 Subject: [PATCH 2/2] fix: dockerfile fix --- Dockerfile | 2 +- static/js/docdetail.js | 2 +- static/js/init-theme.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 21cc23d..6f8791f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM swr.cn-north-4.myhuaweicloud.com/opensourceway/openeuler/nginx-hugo:1.24.0-22.03-lts-sp1-0.121.1 +FROM swr.cn-north-4.myhuaweicloud.com/opensourceway/openeuler/nginx-hugo:latest COPY . /src/website/ diff --git a/static/js/docdetail.js b/static/js/docdetail.js index 4423dc0..12c3f55 100644 --- a/static/js/docdetail.js +++ b/static/js/docdetail.js @@ -115,7 +115,7 @@ function setCustomCookie(key, value, day = 1, domain = location.hostname) { // 皮肤主题切换 (function () { const domain = '.openlookeng.io'; - const APPEARANCE_KEY = 'openlookeng-theme-appearance'; + const APPEARANCE_KEY = 'openLooKeng-theme-appearance'; const themeStyle = getCustomCookie(APPEARANCE_KEY); const html = $('html'); if (!themeStyle) { diff --git a/static/js/init-theme.js b/static/js/init-theme.js index f143110..a3d710b 100644 --- a/static/js/init-theme.js +++ b/static/js/init-theme.js @@ -14,7 +14,7 @@ function getCustomCookie(key) { return null } -const APPEARANCE_KEY = 'openlookeng-theme-appearance' +const APPEARANCE_KEY = 'openLooKeng-theme-appearance' const themeStyle = getCustomCookie(APPEARANCE_KEY) const html = document.getElementsByTagName('html')[0] -- Gitee