summaryrefslogtreecommitdiff
path: root/themer.js
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2020-08-04 19:03:55 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2020-08-04 19:03:55 +0800
commit6a765f3e8509fb62c9c6d381b57b5964b461f4b8 (patch)
tree04a0647bbd4d2c29bfc1d9c97628fb6e6c16d6aa /themer.js
parent24f3ab15a9c0b3a95d63390561d9e8efea05fc06 (diff)
downloadweb-6a765f3e8509fb62c9c6d381b57b5964b461f4b8.tar.xz
Show some love to our photophobic users.
(Always use dark theme if the user indicates their preference.) Blog posts will be updated later.
Diffstat (limited to 'themer.js')
-rw-r--r--themer.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/themer.js b/themer.js
new file mode 100644
index 0000000..b17f245
--- /dev/null
+++ b/themer.js
@@ -0,0 +1,46 @@
+var isDarkTheme=false;
+function loadTheme(){
+ var thm=document.cookie.replace(new RegExp("(?:(?:^|.*;\\s*)thm\\s*\\=\\s*([^;]*).*$)|^.*$"),"$1");
+ if(thm.length<2||'0123z'.indexOf(thm[0])==-1||'abz'.indexOf(thm[1])==-1)thm='zz';
+ if(document.getElementById('themesw'))
+ {
+ var tbtn=document.getElementById('themesw').querySelectorAll('a');
+ tbtn.forEach(function(a){a.style.borderBottomColor='rgba(255,255,255,0)'});
+ document.getElementById(`ts${thm[0]}`).style.borderBottomColor='#FFF';
+ document.getElementById(`tt${thm[1]}`).style.borderBottomColor='#FFF';
+ }
+ var ent="";
+ var d=new Date();
+ if(thm[0]=='z')
+ {
+ var m=d.getMonth()+1;
+ if(m>=3&&m<6)thm='0'+thm[1];
+ else if(m>=6&&m<9)thm='1'+thm[1];
+ else if(m>=9&&m<12)thm='2'+thm[1];
+ else thm='3'+thm[1];
+ }
+ if(thm[1]=='z')
+ {
+ if(window.matchMedia&&matchMedia('(prefers-color-scheme:dark)')&&matchMedia('(prefers-color-scheme:dark)').matches)
+ thm=thm[0]+'b';
+ else {if(d.getHours()>=18||d.getHours()<6)thm=thm[0]+'b';else thm=thm[0]+'a';}
+ }
+ if(thm[1]=='b')isDarkTheme=true;
+ ent=`theme${thm}`;
+ var R=new RegExp('theme[0-4][ab]');
+ for(var i=0;i<document.styleSheets.length;++i)
+ {
+ if(R.exec(document.styleSheets[i].ownerNode.id)!==null&&document.styleSheets[i].ownerNode.id!=ent)
+ document.styleSheets[i].disabled=true;
+ else document.styleSheets[i].disabled=false;
+ }
+ var thmcolor="";
+ switch(thm[0])
+ {
+ case '0':thmcolor=thm[1]=='a'?'#f59dda':'#2f0933';break;
+ case '1':thmcolor=thm[1]=='a'?'#9df59d':'#090933';break;
+ case '2':thmcolor=thm[1]=='a'?'#edb47b':'#1f1205';break;
+ case '3':thmcolor=thm[1]=='a'?'#a0cdfa':'#051933';break;
+ }
+ document.querySelector("meta[name=theme-color]").setAttribute('content',thmcolor);
+}