From 43c1c1279fb228c684c109bcc7aa61375c55e64f Mon Sep 17 00:00:00 2001 From: oc <oc@oc> Date: Tue, 21 Dec 2021 17:51:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E9=A2=98=E9=BB=98=E8=AE=A4=E8=B7=9F?= =?UTF-8?q?=E9=9A=8F=E7=B3=BB=E7=BB=9F=EF=BC=8C=E7=9B=B4=E8=87=B3=E4=BD=A0?= =?UTF-8?q?=E6=89=8B=E5=8A=A8=E9=80=89=E6=8B=A9=E4=BA=86=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +-- web/index.html | 4 +-- web/js/serverstatus.js | 55 ++++++++++++++++++++---------------------- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 64cb9b5..e135e35 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [](https://github.com/cppla/ServerStatus) [](https://github.com/cppla/ServerStatus) [](https://github.com/cppla/ServerStatus) -[](https://github.com/cppla/ServerStatus) +[](https://github.com/cppla/ServerStatus)  @@ -26,7 +26,7 @@ 【服务端】: ```bash -`onetouch`: +`OneTouch`: wget --no-check-certificate -qO serverstatus-config.json https://raw.githubusercontent.com/cppla/ServerStatus/master/server/config.json && mkdir serverstatus-monthtraffic docker run -d --restart=always --name=serverstatus -v ~/serverstatus-config.json:/ServerStatus/server/config.json -v ~/serverstatus-monthtraffic:/usr/share/nginx/html/json -p 80:80 -p 35601:35601 cppla/serverstatus:latest diff --git a/web/index.html b/web/index.html index 49a7e66..eaa8f13 100644 --- a/web/index.html +++ b/web/index.html @@ -50,8 +50,8 @@ <li class="dropdown"> <a data-toggle="dropdown" class="dropdown-toggle" href="#">风格<b class="caret"></b></a> <ul class="dropdown-menu"> - <li><a href="#" onclick="setActiveStyleSheet('dark')">黑夜</a></li> - <li><a href="#" onclick="setActiveStyleSheet('light')">白天</a></li> + <li><a href="#" onclick="setActiveStyleSheet('dark', true)">黑夜</a></li> + <li><a href="#" onclick="setActiveStyleSheet('light', true)">白天</a></li> </ul> </li> </ul> diff --git a/web/js/serverstatus.js b/web/js/serverstatus.js index cf8a519..44b0fc3 100644 --- a/web/js/serverstatus.js +++ b/web/js/serverstatus.js @@ -318,14 +318,17 @@ setInterval(updateTime, 2000); // styleswitcher.js -function setActiveStyleSheet(title) { - var i, a, main; - for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { - if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) { - a.disabled = true; - if(a.getAttribute("title") == title) a.disabled = false; - } - } +function setActiveStyleSheet(title, cookie=false) { + var i, a, main; + for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { + if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) { + a.disabled = true; + if(a.getAttribute("title") == title) a.disabled = false; + } + } + if (true==cookie) { + createCookie("style", title, 365); + } } function getActiveStyleSheet() { @@ -337,15 +340,6 @@ function getActiveStyleSheet() { return null; } -function getPreferredStyleSheet() { - var i, a; - for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { - if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) - return a.getAttribute("title"); - } -return null; -} - function createCookie(name,value,days) { if (days) { var date = new Date(); @@ -370,16 +364,19 @@ function readCookie(name) { } window.onload = function(e) { - var cookie = readCookie("style"); - var title = cookie ? cookie : getPreferredStyleSheet(); - setActiveStyleSheet(title); + var cookie = readCookie("style"); + if (cookie && cookie != 'null' ) { + setActiveStyleSheet(cookie); + } else { + function handleChange (mediaQueryListEvent) { + if (mediaQueryListEvent.matches) { + setActiveStyleSheet('dark'); + } else { + setActiveStyleSheet('light'); + } + } + const mediaQueryListDark = window.matchMedia('(prefers-color-scheme: dark)'); + setActiveStyleSheet(mediaQueryListDark.matches ? 'dark' : 'light'); + mediaQueryListDark.addEventListener("change",handleChange); + } } - -window.onunload = function(e) { - var title = getActiveStyleSheet(); - createCookie("style", title, 365); -} - -var cookie = readCookie("style"); -var title = cookie ? cookie : getPreferredStyleSheet(); -setActiveStyleSheet(title);