-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgk.js
More file actions
143 lines (123 loc) · 4.41 KB
/
gk.js
File metadata and controls
143 lines (123 loc) · 4.41 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 选择启用动态取色功能或自动颜色调整
const useDynamicColor = true; // 若不需要动态取色,改为 false
const useLocalTime = false; // 若要使用本地时间,改为 true;否则使用服务器时间
function fetchServerTime() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://worldtimeapi.org/api/timezone/Asia/Shanghai', true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
var response = JSON.parse(xhr.responseText);
var serverTime = new Date(response.utc_datetime);
updateCountdown(serverTime);
} else {
console.error('Error fetching server time:', xhr.status);
}
};
xhr.send();
}
function padZero(num) {
return num < 10 ? '0' + num : num;
}
function updateCountdown(time) {
var today = time || new Date();
var currentYear = today.getFullYear();
var examDateStart = new Date(currentYear, 5, 7); // 6月7日,月份从0开始计数
var examDateEnd = new Date(currentYear, 5, 9, 18, 0, 0); // 6月9日 18:00
var nextYearExamDate = new Date(currentYear + 1, 5, 7); // 下一年的高考日期
if (today >= examDateStart && today <= examDateEnd) {
document.getElementById("countdown").style.display = "none";
document.getElementById("greeting").style.display = "block";
document.getElementById("greeting").innerText = "今年的高考进行中,祝考试的同学们旗开得胜,金榜题名!";
} else {
document.getElementById("greeting").style.display = "none";
document.getElementById("countdown").style.display = "block";
var timeDiff = examDateStart - today;
if (today > examDateEnd) {
timeDiff = nextYearExamDate - today;
currentYear++;
}
var days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
var hours = padZero(Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)));
var minutes = padZero(Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60)));
var seconds = padZero(Math.floor((timeDiff % (1000 * 60)) / 1000));
document.getElementById("days").innerText = days;
document.getElementById("hours").innerText = hours;
document.getElementById("minutes").innerText = minutes;
document.getElementById("seconds").innerText = seconds;
document.getElementById("year").innerText = currentYear;
}
}
function fetchHitokoto() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://v1.hitokoto.cn/', true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
var response = JSON.parse(xhr.responseText);
document.getElementById("hitokoto").innerText = response.hitokoto + " -- " + response.from;
} else {
console.error('Error fetching hitokoto:', xhr.status);
}
};
xhr.send();
}
function setAutoColor() {
const elements = document.querySelectorAll('.auto-color');
elements.forEach(el => {
const rgb = getAverageRGB(document.body);
const yiq = ((rgb.r*299)+(rgb.g*587)+(rgb.b*114))/1000;
el.style.color = (yiq >= 128) ? 'black' : 'white';
});
}
function getAverageRGB(imgEl) {
var blockSize = 5, // 取样间隔
defaultRGB = {r:255,g:255,b:255}, // 默认白色
canvas = document.createElement('canvas'),
context = canvas.getContext && canvas.getContext('2d'),
data, width, height,
i = -4,
length,
rgb = {r:0,g:0,b:0},
count = 0;
if (!context) {
return defaultRGB;
}
height = canvas.height = imgEl.naturalHeight || imgEl.offsetHeight || imgEl.height;
width = canvas.width = imgEl.naturalWidth || imgEl.offsetWidth || imgEl.width;
context.drawImage(imgEl, 0, 0);
try {
data = context.getImageData(0, 0, width, height);
} catch(e) {
return defaultRGB;
}
length = data.data.length;
while ( (i += blockSize * 4) < length ) {
++count;
rgb.r += data.data[i];
rgb.g += data.data[i+1];
rgb.b += data.data[i+2];
}
rgb.r = ~~(rgb.r/count);
rgb.g = ~~(rgb.g/count);
rgb.b = ~~(rgb.b/count);
return rgb;
}
function initCountdown() {
if (useLocalTime) {
updateCountdown(new Date());
setInterval(() => updateCountdown(new Date()), 1000);
} else {
fetchServerTime();
setInterval(fetchServerTime, 1000);
}
}
document.addEventListener("DOMContentLoaded", () => {
// 获取时间并更新倒计时
initCountdown();
// 获取一言
fetchHitokoto();
setInterval(fetchHitokoto, 3600000);
// 动态设置字体颜色
if (!useDynamicColor) {
setAutoColor();
}
});