-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
316 lines (265 loc) · 14.8 KB
/
index.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
updateScrollPercent();
['scroll', 'resize'].forEach(e => addEventListener(e, updateScrollPercent));
function updateScrollPercent() {
document.body.style.setProperty('--scroll', scrollY / innerHeight);
}
const bg = document.querySelector('.background');
addEventListener('touchstart', () => bg.style.setProperty('--multiplier', '0'), { once: true });
let isUpdatingBgPosition = false;
let isMouseLeft = false;
addEventListener('mousemove', ({ clientX, clientY }) => {
if (isUpdatingBgPosition) return;
isUpdatingBgPosition = true;
requestAnimationFrame(() => {
if (isMouseLeft) return isUpdatingBgPosition = false;
bg.style.setProperty('--tx', `${20 * (clientX - innerWidth / 2) / innerWidth}px`);
bg.style.setProperty('--ty', `${20 * (clientY - innerHeight / 2) / innerHeight}px`);
isUpdatingBgPosition = false;
});
});
document.addEventListener('mouseleave', () => {
isMouseLeft = true;
bg.removeAttribute('style');
applyBackgroundTransition();
});
document.addEventListener('mouseenter', () => {
isMouseLeft = false;
applyBackgroundTransition();
});
function applyBackgroundTransition() {
bg.style.transition = 'transform .1s linear';
bg.addEventListener('transitionend', () => bg.style.transition = '', { once: true });
}
document.querySelector('header svg').style = `-webkit-mask-image:url('data:image/svg+xml,<!--${Math.random()}--><svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 550 100"><style>@keyframes dash{0%{stroke-dashoffset:727px;}to{stroke-dashoffset:0;};}@keyframes stroke-width{0%{stroke-width:3px;}to{stroke-width:12px;};}@keyframes fade{0%{opacity:0;}to{opacity:1;};}path{fill:none;stroke:%23fff;stroke-linecap:round;stroke-linejoin:round;stroke-width:3px;stroke-dasharray:727px;stroke-dashoffset:727px;animation:dash 1s cubic-bezier(.8,0,.2,1) var(--delay) forwards,stroke-width 1s cubic-bezier(.8,0,.2,1) calc(var(--delay) + .5s) forwards,fade .2s linear calc(var(--delay) + 0s) forwards;}</style><path d="M0 73.81 33.2.01l33.3 73.8m-54-20.7h41.6" style="--delay:0.3s"/><path d="M82 28.71v24.6q0 6.5 2.55 10.85t6.9 6.5q4.35 2.15 9.85 2.15 5.4 0 9.7-2.1 4.3-2.1 6.8-5.65 2.5-3.55 2.5-8.05v-28.3" style="--delay:0.5s"/><path d="M154 9.11v49.8q0 5.8 3.6 9.45 3.6 3.65 9.3 3.65h2.2m-29-43.3h29.1" style="--delay:0.7s"/><path d="M190 28.71v24.6q0 6.5 2.55 10.85t6.9 6.5q4.35 2.15 9.85 2.15 5.4 0 9.7-2.1 4.3-2.1 6.8-5.65 2.5-3.55 2.5-8.05v-28.3" style="--delay:0.9s"/><path d="M251 28.71v46.4-30.6q0-7 4.4-11.4t11.3-4.4q6.5 0 10.55 4.9 4.05 4.9 4.05 12.6v28.9-30.6q0-7 4.1-11.4 4.7-4.4 11.3-4.4 6.8 0 10.55 4.9 4.35 4.9 4.05 12.6l.3 28.9" style="--delay:1.1s"/><path d="M334 28.71v46.4-30.6q0-4.5 2.5-8.1 2.5-3.6 6.8-5.65 4.3-2.05 9.7-2.05 5.5 0 9.85 2.15t6.9 6.45q2.55 4.3 2.55 10.9v26.9" style="--delay:1.3s"/><path d="m388 .01 31.8 78.1L452 .01" style="--delay:1.5s"/><path d="M475 78.11V.01l56.1 78.1V.01" style="--delay:1.7s"/></svg>');`;
document.querySelector('.arrow svg').addEventListener('click', () => {
smoothScrollTo(innerWidth > 880 ? .25 * innerHeight + 30 : .25 * innerHeight + 380);
});
function smoothScrollTo(target) {
const { documentElement } = document;
const start = documentElement.scrollTop;
const startTime = performance.now();
requestAnimationFrame(scroll);
function scroll() {
const time = Math.min(1, (performance.now() - startTime) / 200);
const timeFunction = time * (2 - time);
documentElement.scrollTop = (timeFunction * (target - start)) + start;
if (Math.abs(documentElement.scrollTop - target) > 1) requestAnimationFrame(scroll);
};
}
document.querySelectorAll('.overflow').forEach(e => {
e.addEventListener('mouseenter', () => {
if (e.scrollWidth > e.clientWidth) e.title = e.textContent.trim();
});
});
fetch('/fetchgithub').then(r => r.json()).then(repos => {
const stats = repos.pop();
document.querySelectorAll('.stat').forEach((stat, i) => stat.textContent = stats[i]);
document.querySelectorAll('.star').forEach((star, i) => star.textContent = repos[i][0]);
document.querySelectorAll('.fork').forEach((fork, i) => fork.textContent = repos[i][1]);
});
const TIME_UNIT = [
['year', 60 * 60 * 24 * 365],
['month', 60 * 60 * 24 * 30],
['week', 60 * 60 * 24 * 7],
['day', 60 * 60 * 24],
['hour', 60 * 60],
['minute', 60]
];
fetch('/fetchanilist').then(r => r.json()).then(activities => {
document.querySelectorAll('.anilist .activity').forEach((a, i) => a.href = activities[i][0]);
document.querySelectorAll('.anilist .activity .image').forEach((i, j) => i.style = `background-image: url(${imageProxy(activities[j][1])})`);
document.querySelectorAll('.anilist .activity .status').forEach((s, i) => s.textContent = activities[i][2]);
document.querySelectorAll('.anilist .activity .title').forEach((t, i) => t.textContent = activities[i][3]);
document.querySelectorAll('.anilist .activity time').forEach((t, i) => {
const now = Math.floor(Date.now() / 1000);
const diff = now - activities[i][4];
let textContent = 'just now';
for (const [unit, value] of TIME_UNIT) {
const amount = Math.floor(diff / value);
if (amount > 0) {
textContent = `${amount} ${unit}${amount > 1 ? 's' : ''} ago`;
break;
}
}
t.textContent = textContent;
t.setAttribute('datetime', new Date(activities[i][4] * 1000).toISOString());
t.title = new Date(activities[i][4] * 1000).toLocaleString();
});
});
fetch('/fetchlanyard').then(r => r.json()).then(updateLanyard);
!function lanyard() {
const ws = new WebSocket('wss://api.lanyard.rest/socket');
ws.addEventListener('open', () => ws.send(JSON.stringify({ op: 2, d: { subscribe_to_id: '393694671383166998' } })));
ws.addEventListener('error', () => ws.close());
ws.addEventListener('close', () => setTimeout(lanyard, 1000));
ws.addEventListener('message', async ({ data }) => {
const { t, d } = JSON.parse(data);
if (t !== 'INIT_STATE' && t !== 'PRESENCE_UPDATE') return;
updateLanyard(d);
});
}();
const ACTIVITY_TYPE = ['Playing', 'Streaming to', 'Listening to', 'Watching', 'Custom status', 'Competing in'];
const STATUS_COLOR = { online: '#4b8', idle: '#fa1', dnd: '#f44', offline: '#778' };
const cache = new Map();
function updateLanyard({ discord_user, discord_status, activities }) {
update('.discord .avatar', 'style', `background-image: url(${imageProxy(`https://cdn.discordapp.com/avatars/${discord_user.id}/${discord_user.avatar}.webp?size=80`)})`);
update('.discord .display-name', 'textContent', discord_user.display_name);
update('.discord .color-dot', 'style', `background-color: ${STATUS_COLOR[discord_status]}`);
activities = activities.filter(a => a.type !== 4);
if (!activities.length) {
update('.discord .status', 'textContent', discord_status);
update('.discord .activity', 'textContent', '');
update('.discord .details', 'textContent', '');
update('.discord .state', 'textContent', '');
update('.discord .large-image', 'style', '');
update('.discord .large-image', 'title', '');
update('.discord .small-image', 'style', '');
update('.discord .small-image', 'title', '');
setRpcTimestamp('undefined undefined');
return;
}
const a = activities[0];
['large', 'small'].forEach(s => {
const size = s === 'large' ? 160 : 60;
let imageUrl = a.assets?.[`${s}_image`];
if (!imageUrl) {
update(`.discord .${s}-image`, 'style', '');
update(`.discord .${s}-image`, 'title', '');
if (s === 'small') update(`.discord .image-container foreignObject`, 'mask', '');
return;
}
if (imageUrl.startsWith('mp:')) imageUrl = `https://media.discordapp.net/${imageUrl.slice(3)}?width=${size}&height=${size}`;
else if (imageUrl.startsWith('spotify:')) imageUrl = `https://i.scdn.co/image/${imageUrl.slice(8)}`;
else imageUrl = `https://cdn.discordapp.com/app-assets/${a.application_id}/${imageUrl}.png?size=${size}`;
update(`.discord .${s}-image`, 'style', `background-image: url(${imageProxy(imageUrl)})`);
update(`.discord .${s}-image`, 'title', a.assets?.[`${s}_text`] || '');
if (s === 'small') update(`.discord .image-container foreignObject`, 'mask', 'url(#mask-large-image)');
});
update('.discord .status', 'textContent', ACTIVITY_TYPE[a.type]);
update('.discord .activity', 'textContent', a.name);
update('.discord .details', 'textContent', a.details);
update('.discord .state', 'textContent', a.state);
const timestamp = `${a.timestamps?.start} ${a.timestamps?.end}`;
if (cache.get('timestamp') !== timestamp) setRpcTimestamp(timestamp);
}
function imageProxy(url) {
return `https://chino.is-a.dev/cdn-cgi/image/format=avif/${url}`;
}
function update(selector, property, value) {
const key = `${selector} ${property}`;
if (cache.get(key) === value) return;
let e = cache.get(selector);
if (!e) cache.set(selector, e = document.querySelector(selector));
if (['mask', 'datetime'].includes(property)) e.setAttribute(property, value);
else e[property] = value;
cache.set(key, value);
}
function setRpcTimestamp(timestamp = 'undefined undefined') {
cache.set('timestamp', timestamp);
if (timestamp === 'undefined undefined') {
update('.discord .timestamp', 'textContent', '');
update('.discord .timebar-container', 'style', 'display: none');
return;
}
if (timestamp.includes('undefined')) {
timestamp = Number(timestamp.split(' ')[0] === 'undefined' ? timestamp.split(' ')[1] : timestamp.split(' ')[0]);
const diff = Math.abs(timestamp - Date.now());
const hour = Math.floor(diff / 1000 / 60 / 60);
const minute = Math.floor(diff / 1000 / 60) % 60;
const second = Math.floor(diff / 1000) % 60;
update('.discord .timestamp', 'textContent', `${hour ? `${padZero(hour)}:` : ''}${padZero(minute)}:${padZero(second)} ${timestamp > Date.now() ? 'left' : 'elapsed'}`);
update('.discord .timebar-container', 'style', 'display: none');
return;
}
const [start, end] = timestamp.split(' ').map(t => Number(t));
const now = Date.now();
const total = end - start;
const current = now < end ? now - start : total;
const progress = current / total * 100;
const currentHour = Math.floor(current / 1000 / 60 / 60);
const currentMinute = Math.floor(current / 1000 / 60) % 60;
const currentSecond = Math.floor(current / 1000) % 60;
const totalHour = Math.floor(total / 1000 / 60 / 60);
const totalMinute = Math.floor(total / 1000 / 60) % 60;
const totalSecond = Math.floor(total / 1000) % 60;
update('.discord .current-time', 'textContent', `${currentHour ? `${padZero(currentHour)}:` : ''}${padZero(currentMinute)}:${padZero(currentSecond)}`);
update('.discord .total-time', 'textContent', `${totalHour ? `${padZero(totalHour)}:` : ''}${padZero(totalMinute)}:${padZero(totalSecond)}`);
update('.discord .timebar-progress', 'style', `width: ${Math.max(0, Math.min(100, progress))}%`);
update('.discord .timebar-container', 'style', 'display: flex');
}
function padZero(n) {
return n.toString().padStart(2, '0');
}
const visitTime = new Date().setSeconds(0, 0);
!function setClock() {
const now = Date.now();
const [month, day, year, hour, minute, second] = new Date().toLocaleString('en-US', { timeZone: 'Asia/Ho_Chi_Minh', hour12: false }).match(/\d+/g).map(Number);
const hourOffset = -new Date().getTimezoneOffset() / 60;
const utcTime = new Date(now - now % 1000 - hourOffset * 60 * 60 * 1000);
const timezoneOffset = (new Date(year, month - 1, day, hour, minute, second) - utcTime) / 1000 / 60 / 60;
const timezoneDiff = timezoneOffset - hourOffset;
const myTime = new Date(now + timezoneDiff * 60 * 60 * 1000);
const utcOffset = `${(timezoneOffset >= 0 ? '+' : '')}${Math.floor(timezoneOffset)}:${(timezoneOffset % 1 * 60).toString().padStart(2, '0')}`;
update('.clock .hour-hand', 'style', `transform: rotate(${hour % 12 / 12 * 360 + minute / 60 * 30 + second / 60 / 60 * 30}deg)`);
update('.clock .minute-hand', 'style', `transform: rotate(${minute / 60 * 360 + second / 60 * 6}deg)`);
update('.clock .second-hand', 'style', `transform: rotate(${360 * Math.floor((now - visitTime) / 60 / 1000) + second / 60 * 360}deg)`);
update('.clock .date', 'textContent', myTime.toLocaleDateString());
update('.clock .date-container', 'datetime', myTime.toISOString().split('T')[0]);
update('.clock .hour', 'textContent', padZero(hour));
update('.clock .minute', 'textContent', padZero(minute));
update('.clock .second', 'textContent', padZero(second));
update('.clock .time-container', 'datetime', myTime.toISOString().split('T')[1].split('.')[0]);
update('.clock .timezone-diff', 'textContent', timezoneDiff === 0 ? 'same time' : (timezoneDiff > 0 ? `${formatTimezone(timezoneDiff)} ahead` : `${formatTimezone(-timezoneDiff)} behind`));
update('.clock .utc-offset', 'textContent', ` / UTC ${utcOffset}`);
update('.clock .utc-offset', 'datetime', utcOffset);
setRpcTimestamp(cache.get('timestamp'));
setTimeout(setClock, 1000 - now % 1000);
}();
function formatTimezone(timezoneDiff) {
const hour = Math.floor(Math.abs(timezoneDiff));
const minute = Math.abs(timezoneDiff % 1 * 60);
return `${timezoneDiff < 0 ? '-' : ''}${hour}h${minute ? ` ${minute}m` : ''}`;
}
document.querySelector('[href="/skin/Minimal.osk"]').addEventListener('click', e => {
e.preventDefault();
const skins = [
'Minimal.osk',
'Minimal 3.osk',
'Minimal EZ.osk',
'Minimal FL.osk',
'Minimal Instafade.osk',
'Minimal Mapping.osk'
];
for (const skin of skins) {
const a = document.createElement('a');
a.href = `/skin/${encodeURIComponent(skin)}`;
a.download = skin;
a.click();
}
});
let isRecording = false;
let recoder;
let stream;
document.querySelector('.recorder').addEventListener('click', async () => {
if (!isRecording) {
stream = await navigator.mediaDevices.getDisplayMedia({ audio: true, video: { frameRate: { ideal: 60 } } });
recoder = new MediaRecorder(stream);
const [video] = stream.getVideoTracks();
recoder.start();
isRecording = true;
video.addEventListener('ended', () => {
recoder.stop();
isRecording = false;
});
recoder.addEventListener('dataavailable', e => {
const a = document.createElement('a');
a.href = URL.createObjectURL(e.data);
a.download = 'watch if cute.webm';
a.click();
});
} else {
recoder.stop();
stream.getTracks().forEach(track => track.stop());
isRecording = false;
}
});