-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathscripts.html
More file actions
45 lines (44 loc) · 1.52 KB
/
scripts.html
File metadata and controls
45 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<script src="assets/js/main.min.js"></script>
{% include analytics.html %}
{% include fetch_google_scholar_stats.html %}
<script>
(function(){
function getPreferredTheme(){
var saved = null;
try { saved = localStorage.getItem('theme'); } catch(e) {}
if(saved === 'dark' || saved === 'light') return saved;
return 'dark';
}
function setIcon(theme){
var btn = document.getElementById('theme-toggle-btn');
if(!btn) return;
var sun = btn.querySelector('.icon-sun');
var moon = btn.querySelector('.icon-moon');
if(sun && moon){
sun.style.display = theme === 'dark' ? 'inline' : 'none';
moon.style.display = theme === 'dark' ? 'none' : 'inline';
}
btn.setAttribute('title', theme === 'dark' ? '切换为浅色' : '切换为深色');
}
function applyTheme(theme){
var root = document.documentElement;
if(theme === 'dark'){
root.classList.add('theme-dark');
} else {
root.classList.remove('theme-dark');
}
try { localStorage.setItem('theme', theme); } catch(e) {}
setIcon(theme);
}
document.addEventListener('DOMContentLoaded', function(){
applyTheme(getPreferredTheme());
var btn = document.getElementById('theme-toggle-btn');
if(btn){
btn.addEventListener('click', function(e){
e.preventDefault();
applyTheme(document.documentElement.classList.contains('theme-dark') ? 'light' : 'dark');
}, { passive: true });
}
});
})();
</script>