forked from AceCandy/cc-quick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
452 lines (356 loc) · 11.5 KB
/
script.js
File metadata and controls
452 lines (356 loc) · 11.5 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
(function () {
var toggle = document.getElementById("osToggle");
if (!toggle) {
return;
}
var buttons = toggle.querySelectorAll(".os-btn");
function detectOS() {
var platform = navigator.platform || "";
var userAgent = navigator.userAgent || "";
if (/Mac|iPhone|iPod|iPad/.test(platform) || /Mac/.test(userAgent)) {
return "mac";
}
return "win";
}
if (localStorage.getItem("cc-os") && !localStorage.getItem("cc-os-manual")) {
localStorage.removeItem("cc-os");
}
var saved = localStorage.getItem("cc-os") || detectOS();
function replaceModifierText(text, os) {
if (!text) {
return text;
}
if (os === "mac") {
return text.replaceAll("Shift", "⇧").replaceAll("Alt", "⌥");
}
return text.replaceAll("⇧", "Shift").replaceAll("⌥", "Alt");
}
function applyOS(os) {
buttons.forEach(function (button) {
button.classList.toggle("active", button.dataset.os === os);
if (button.setAttribute) {
button.setAttribute("aria-pressed", button.dataset.os === os ? "true" : "false");
}
});
document.querySelectorAll(".keycap").forEach(function (keycap) {
var text = keycap.textContent.trim();
if (text === "Alt" || text === "⌥") {
keycap.textContent = os === "mac" ? "⌥" : "Alt";
} else if (text === "Shift" || text === "⇧") {
keycap.textContent = os === "mac" ? "⇧" : "Shift";
}
});
// 中文页当前是纯文本快捷键,不是上游的 keycap 结构,切换时要同步替换修饰键前缀。
document.querySelectorAll(".key").forEach(function (key) {
if (key.querySelectorAll && key.querySelectorAll(".keycap").length > 0) {
return;
}
key.textContent = replaceModifierText(key.textContent, os);
});
localStorage.setItem("cc-os", os);
}
buttons.forEach(function (button) {
button.addEventListener("click", function () {
localStorage.setItem("cc-os-manual", "1");
applyOS(button.dataset.os);
});
});
applyOS(saved);
})();
(function () {
var root = document.documentElement;
var toggle = document.getElementById("themeToggle");
var buttons = toggle ? toggle.querySelectorAll(".theme-btn") : [];
var storedTheme = localStorage.getItem("cc-theme");
var systemPrefersDark = typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: dark)").matches;
var initialTheme = storedTheme || (systemPrefersDark ? "dark" : "light");
function applyTheme(theme) {
if (!root) {
return;
}
root.dataset.theme = theme;
localStorage.setItem("cc-theme", theme);
buttons.forEach(function (button) {
var active = button.dataset.theme === theme;
button.classList.toggle("active", active);
if (button.setAttribute) {
button.setAttribute("aria-pressed", active ? "true" : "false");
}
});
}
if (buttons.length > 0) {
buttons.forEach(function (button) {
button.addEventListener("click", function () {
applyTheme(button.dataset.theme);
});
});
}
applyTheme(initialTheme);
})();
function initStickyOffsets() {
var root = document.documentElement;
var header = document.querySelector(".header");
if (!root || !root.style || typeof root.style.setProperty !== "function" || !header) {
return;
}
if (typeof header.getBoundingClientRect !== "function") {
return;
}
function updateStickyOffsets() {
var headerRect = header.getBoundingClientRect();
var stickyShellTop = Math.round(Math.max(headerRect.top, 0) + headerRect.height + 12);
var stickyScrollMargin = stickyShellTop + 24;
// 顶部 header 固定后,其余 sticky 区域和锚点偏移都跟着 header 实高走,避免遮挡和错位。
root.style.setProperty("--sticky-shell-top", stickyShellTop + "px");
root.style.setProperty("--sticky-scroll-margin", stickyScrollMargin + "px");
}
updateStickyOffsets();
if (window && typeof window.addEventListener === "function") {
window.addEventListener("resize", updateStickyOffsets);
}
}
function resolveSectionPanelFromHash(hash) {
if (!hash) {
return null;
}
var hashId = hash.charAt(0) === "#" ? hash.slice(1) : hash;
if (!hashId) {
return null;
}
var target = document.getElementById(hashId);
if (!target) {
return null;
}
if (typeof target.hasAttribute === "function" && target.hasAttribute("data-section-panel")) {
return target;
}
if (typeof target.closest === "function") {
return target.closest("[data-section-panel]");
}
return null;
}
function initSectionSwitcher() {
var buttons = Array.prototype.slice.call(document.querySelectorAll(".section-switcher-btn"));
var panels = Array.prototype.slice.call(document.querySelectorAll("[data-section-panel]"));
if (buttons.length === 0 || panels.length === 0) {
return;
}
function getButtonTarget(button) {
return button.dataset.sectionTarget || button.getAttribute("data-section-target");
}
function getPanelTarget(panel) {
if (!panel) {
return "";
}
return panel.dataset && panel.dataset.sectionPanel ? panel.dataset.sectionPanel : panel.getAttribute("data-section-panel") || "";
}
function findPanelByTarget(target) {
if (!target) {
return null;
}
for (var i = 0; i < panels.length; i += 1) {
if (getPanelTarget(panels[i]) === target) {
return panels[i];
}
}
return null;
}
function syncHashWithoutScroll(activeTarget) {
if (!activeTarget) {
return;
}
if (window.history && typeof window.history.replaceState === "function") {
window.history.replaceState(null, "", "#" + activeTarget);
return;
}
window.location.hash = "#" + activeTarget;
}
function alignPanelToSidebar(activePanel) {
var sidebar = document.querySelector(".page-sidebar");
if (!sidebar || !activePanel) {
return;
}
if (typeof sidebar.getBoundingClientRect !== "function" || typeof activePanel.getBoundingClientRect !== "function") {
return;
}
if (typeof window.scrollBy !== "function") {
return;
}
var sidebarRect = sidebar.getBoundingClientRect();
var panelRect = activePanel.getBoundingClientRect();
var isSideBySide = sidebarRect.right <= panelRect.left || panelRect.right <= sidebarRect.left;
if (!isSideBySide) {
return;
}
var sidebarTop = sidebarRect.top;
var panelTop = panelRect.top;
var offset = panelTop - sidebarTop;
if (Math.abs(offset) < 2) {
return;
}
window.scrollBy({
top: offset,
left: 0,
behavior: "auto"
});
}
function setActivePanel(activePanel, syncHash) {
var activeTarget = getPanelTarget(activePanel);
panels.forEach(function (panel) {
var isActive = panel === activePanel;
panel.hidden = !isActive;
});
buttons.forEach(function (button) {
var isActive = getButtonTarget(button) === activeTarget;
button.classList.toggle("active", isActive);
button.setAttribute("aria-pressed", isActive ? "true" : "false");
});
if (syncHash) {
syncHashWithoutScroll(activeTarget);
}
}
function activateFromHash(hash, syncHash) {
var panel = resolveSectionPanelFromHash(hash) || panels[0];
if (panel) {
setActivePanel(panel, syncHash);
}
}
buttons.forEach(function (button) {
button.addEventListener("click", function () {
var target = getButtonTarget(button);
var panel = findPanelByTarget(target);
if (!panel && typeof button.closest === "function") {
panel = button.closest("[data-section-panel]");
}
if (panel) {
var wasActive = !panel.hidden;
setActivePanel(panel, true);
if (!wasActive) {
alignPanelToSidebar(panel);
}
}
});
});
window.addEventListener("hashchange", function () {
activateFromHash(window.location.hash, false);
});
activateFromHash(window.location.hash, false);
}
function initChangelogPopover() {
var trigger = document.querySelector(".changelog-trigger");
var panel = document.getElementById("changelogPanel");
if (!trigger || !panel) {
return;
}
function setOpen(open) {
panel.hidden = !open;
trigger.setAttribute("aria-expanded", open ? "true" : "false");
}
function closePopover() {
setOpen(false);
}
trigger.addEventListener("click", function (event) {
if (event && typeof event.stopPropagation === "function") {
event.stopPropagation();
}
setOpen(panel.hidden);
});
document.addEventListener("click", function (event) {
var target = event && event.target ? event.target : null;
if (!target || target === trigger) {
return;
}
if (typeof target.closest === "function") {
if (target.closest(".changelog-panel") || target.closest(".changelog-trigger")) {
return;
}
}
closePopover();
});
document.addEventListener("keydown", function (event) {
if (event && event.key === "Escape") {
closePopover();
}
});
setOpen(!panel.hidden);
}
function initSiteVisitCounter() {
var countEl = document.getElementById("siteVisitCount");
if (!countEl || typeof Counter !== "function") {
return;
}
var namespace = countEl.getAttribute("data-counter-namespace") || "cc-quick";
var name = countEl.getAttribute("data-counter-name") || "site-visits";
var cacheKey = namespace + ":siteVisitCount";
var counter = new Counter({ namespace: namespace, version: "v1" });
function setCounterState(state, text) {
countEl.textContent = text;
countEl.setAttribute("data-counter-state", state);
}
function getCachedCount() {
try {
return localStorage.getItem(cacheKey);
} catch (_error) {
return null;
}
}
function cacheCount(count) {
try {
localStorage.setItem(cacheKey, String(count));
} catch (_error) {
// 忽略缓存不可写场景,统计展示仍以本次接口结果为准。
}
}
function readCount(result) {
return result && typeof result.count === "number" ? result.count : null;
}
function renderCount(count) {
setCounterState("ready", count.toLocaleString("zh-CN"));
cacheCount(count);
}
var cachedCount = getCachedCount();
var hasVisibleCount = false;
if (cachedCount !== null) {
setCounterState("ready", Number(cachedCount).toLocaleString("zh-CN"));
hasVisibleCount = true;
}
function updateCounter(method, showFallbackError) {
return counter[method](name)
.then(function (result) {
var count = readCount(result);
if (count === null) {
if (showFallbackError && !hasVisibleCount) {
setCounterState("error", "暂不可用");
}
return;
}
hasVisibleCount = true;
renderCount(count);
})
.catch(function () {
if (showFallbackError && !hasVisibleCount) {
setCounterState("error", "暂不可用");
}
});
}
// CounterAPI v2 需要预创建 workspace;这里使用 v1 namespace,静态页可直接计数。
updateCounter("up", true);
setInterval(function () {
updateCounter("get", false);
}, 15000);
}
(function () {
initStickyOffsets();
initSectionSwitcher();
initChangelogPopover();
initSiteVisitCounter();
})();
(function () {
var now = new Date();
document.querySelectorAll(".badge-new[data-added]").forEach(function (badge) {
var added = new Date(badge.getAttribute("data-added"));
if ((now - added) / 86400000 > 14) {
badge.style.display = "none";
}
});
})();