-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
32 lines (27 loc) · 1.37 KB
/
script.js
File metadata and controls
32 lines (27 loc) · 1.37 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
document.addEventListener("DOMContentLoaded", function() {
// 找到所有带有 data-github-repo 属性的元素 (也就是我们的 Badge)
const repos = document.querySelectorAll("[data-github-repo]");
repos.forEach(async el => {
const repo = el.getAttribute("data-github-repo");
try {
// 请求 GitHub API
const response = await fetch(`https://api.github.com/repos/${repo}`);
if (!response.ok) throw new Error('Network response was not ok');
const data = await response.json();
const stars = data.stargazers_count;
// 格式化: 1200 -> 1.2k
const formatted = stars >= 1000 ? (stars / 1000).toFixed(1) + 'k' : stars;
// 找到内部的数字容器
const countSpan = el.querySelector(".star-count");
const suffixSpan = el.querySelector(".star-suffix");
if (countSpan && suffixSpan) {
countSpan.textContent = formatted;
// 成功获取数据后,显示整个后缀区域 (包括数字和星星图标)
suffixSpan.style.display = "inline";
}
} catch (error) {
console.log("Failed to fetch stars for " + repo);
// 失败时不做任何操作,保持默认的隐藏状态
}
});
});