@@ -71,61 +71,6 @@ const homeHref = withBase("/");
7171 </footer >
7272 </div >
7373 <script is:inline >
74- const formatStarCount = (value) =>
75- Number.isFinite(value) ? new Intl.NumberFormat("en-US").format(value) : null;
76- const normalizeFallback = (value) => (/^\d[\d,]*$/.test(value || "") ? value : null);
77-
78- const applyGitHubStars = async () => {
79- const nodes = Array.from(document.querySelectorAll("[data-github-stars]"));
80- const repos = [...new Set(nodes.map((node) => node.dataset.githubStars).filter(Boolean))];
81- if (!repos.length) return;
82-
83- const cacheKey = "rapidai-github-stars-v1";
84- const cacheTtl = 1000 * 60 * 30;
85- const now = Date.now();
86- let cache = {};
87-
88- try {
89- cache = JSON.parse(sessionStorage.getItem(cacheKey) || "{}");
90- } catch {
91- cache = {};
92- }
93-
94- const pending = repos.map(async (repo) => {
95- const cached = cache[repo];
96- if (cached && now - cached.timestamp < cacheTtl) {
97- return [repo, cached.value];
98- }
99-
100- try {
101- const response = await fetch(`https://api.github.com/repos/${repo}`, {
102- headers: { Accept: "application/vnd.github+json" }
103- });
104- if (!response.ok) throw new Error(`GitHub API ${response.status}`);
105- const data = await response.json();
106- const formatted = formatStarCount(data.stargazers_count);
107- if (!formatted) throw new Error("Invalid star count");
108- cache[repo] = { value: formatted, timestamp: now };
109- return [repo, formatted];
110- } catch {
111- return [repo, null];
112- }
113- });
114-
115- const entries = await Promise.all(pending);
116-
117- try {
118- sessionStorage.setItem(cacheKey, JSON.stringify(cache));
119- } catch {}
120-
121- for (const node of nodes) {
122- const repo = node.dataset.githubStars;
123- const matched = entries.find(([entryRepo]) => entryRepo === repo);
124- const value = matched?.[1] || normalizeFallback(node.dataset.fallback);
125- node.textContent = value ? `收藏 ${value}` : "仓库";
126- }
127- };
128-
12974 const applyMemberNewBadges = () => {
13075 const today = new Date();
13176 today.setHours(0, 0, 0, 0);
@@ -158,13 +103,11 @@ const homeHref = withBase("/");
158103 document.addEventListener(
159104 "DOMContentLoaded",
160105 () => {
161- applyGitHubStars();
162106 applyMemberNewBadges();
163107 },
164108 { once: true }
165109 );
166110 } else {
167- applyGitHubStars();
168111 applyMemberNewBadges();
169112 }
170113 </script >
0 commit comments