Repository navigation
Expand file tree
/
Copy pathstar-history.html
More file actions
582 lines (531 loc) · 24.8 KB
/
Copy pathstar-history.html
File metadata and controls
582 lines (531 loc) · 24.8 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
---
layout: null
title: Star History — Loop Engineering
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub Star History — Loop Engineering</title>
<meta name="description" content="View and compare GitHub star history for loop-engineering and other repos you own.">
<meta name="theme-color" content="#363636">
<link rel="icon" href="https://cdn.jsdelivr.net/gh/cobusgreyling/loop-engineering@main/assets/visuals/loop-engineering-logo.svg">
<link rel="stylesheet" href="{{ site.baseurl }}/assets/css/star-history.css">
</head>
<body>
<nav class="sh-banner">
<p>
🚨 GitHub restricts star data to repo admins & collaborators —
<a href="https://www.star-history.com/blog/github-stargazer-api-restriction" target="_blank" rel="noopener noreferrer">what it means</a>
· add a token via <button type="button" class="sh-clear-all" id="banner-token-btn" style="color:#fff;text-decoration:underline;">settings</button>
</p>
</nav>
<header class="sh-header">
<div class="sh-header-inner">
<div class="sh-header-left">
<a href="{{ site.baseurl }}/star-history.html" class="sh-logo" title="Star History">
<svg width="28" height="28" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<defs>
<filter id="xkcd-logo" filterUnits="userSpaceOnUse" x="-2" y="-2" width="28" height="28">
<feTurbulence type="fractalNoise" baseFrequency="0.04" result="noise"/>
<feDisplacementMap scale="2.5" xChannelSelector="R" yChannelSelector="G" in="SourceGraphic" in2="noise"/>
</filter>
</defs>
<g filter="url(#xkcd-logo)" stroke="#fff" stroke-width="1.4" fill="#fff">
<polygon points="12,2 15,9 22,9 16,14 18,22 12,17 6,22 8,14 2,9 9,9"/>
</g>
</svg>
<span>Star History</span>
</a>
<a href="https://www.star-history.com/compare" class="sh-nav-link hidden-md" target="_blank" rel="noopener noreferrer">Compare</a>
</div>
<div class="sh-header-center">
<a href="{{ site.baseurl }}/" class="sh-nav-link">Loop Engineering</a>
<a href="https://www.star-history.com/" class="sh-nav-link" target="_blank" rel="noopener noreferrer">star-history.com</a>
</div>
<div class="sh-header-right">
<button type="button" class="sh-icon-btn" id="open-token-btn" aria-label="Settings — access token" title="Access token">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
<circle cx="12" cy="12" r="3"/>
<path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/>
</svg>
</button>
<a href="https://github.com/cobusgreyling/loop-engineering" class="sh-icon-btn" target="_blank" rel="noopener noreferrer" aria-label="GitHub repo">
<svg width="22" height="22" viewBox="0 0 24 24" fill="currentColor">
<path d="M12 1.5C6.2 1.5 1.5 6.2 1.5 12c0 4.6 3 8.6 7.2 10 .5.1.7-.2.7-.5v-1.8c-2.9.6-3.5-1.4-3.5-1.4-.5-1.2-1.1-1.5-1.1-1.5-.9-.6.1-.6.1-.6 1 .1 1.5 1 1.5 1 .9 1.5 2.3 1.1 2.8.8.1-.6.3-1.1.6-1.3-2.3-.3-4.8-1.2-4.8-5.2 0-1.1.4-2.1 1-2.8-.1-.3-.5-1.3.1-2.8 0 0 .8-.3 2.7 1 .8-.2 1.6-.3 2.5-.3s1.7.1 2.5.3c1.9-1.3 2.7-1 2.7-1 .6 1.5.2 2.5.1 2.8.6.7 1 1.7 1 2.8 0 4-2.5 4.9-4.8 5.2.4.3.7 1 .7 2v3c0 .3.2.6.7.5 4.2-1.4 7.2-5.4 7.2-10C22.5 6.2 17.8 1.5 12 1.5z"/>
</svg>
</a>
</div>
</div>
</header>
<main class="sh-main">
<div class="sh-search-row">
<input
type="text"
id="repo-input"
class="sh-search-input"
placeholder="Enter GitHub repo — e.g. cobusgreyling/loop-engineering"
spellcheck="false"
autocomplete="off"
/>
<button type="button" class="sh-search-btn" id="load-chart-btn">View star history</button>
</div>
<div class="sh-pills" id="pills-wrap">
<button type="button" class="sh-clear-all" id="clear-all-btn" hidden>Clear all</button>
</div>
<div class="sh-controls">
<label class="sh-control">
<input type="checkbox" id="align-timeline" />
Align timeline
</label>
<label class="sh-control">
<input type="checkbox" id="log-scale" />
Log scale
</label>
<div class="sh-control-group">
<span>Legend</span>
<label class="sh-control"><input type="radio" name="legend" value="top-left" checked /> Top left</label>
<label class="sh-control"><input type="radio" name="legend" value="bottom-right" /> Bottom right</label>
</div>
</div>
<div class="sh-chart-box" id="chart-wrap">
<div class="sh-empty" id="empty-state">
<strong>☝️ Enter a GitHub repo name to get started</strong>
Default: <code>cobusgreyling/loop-engineering</code> — add comma-separated repos to compare.
<div class="sh-static-fallback" style="margin-top:24px;">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.jsdelivr.net/gh/cobusgreyling/loop-engineering@main/assets/visuals/star-history-dark.svg" />
<source media="(prefers-color-scheme: light)" srcset="https://cdn.jsdelivr.net/gh/cobusgreyling/loop-engineering@main/assets/visuals/star-history.svg" />
<img alt="Static star history (CI-updated daily)" src="https://cdn.jsdelivr.net/gh/cobusgreyling/loop-engineering@main/assets/visuals/star-history.svg" />
</picture>
<p>Static chart from CI. Live data needs your token (settings ⚙).</p>
</div>
</div>
</div>
<p class="sh-status" id="status" aria-live="polite"></p>
<div class="sh-exports">
<button type="button" class="sh-export-btn" id="export-png" disabled>Image</button>
<button type="button" class="sh-export-btn" id="export-csv" disabled>CSV</button>
<button type="button" class="sh-export-btn" id="export-link" disabled>Link</button>
<button type="button" class="sh-export-btn" id="export-share" disabled>Share</button>
</div>
<p class="sh-footer">
Inspired by <a href="https://www.star-history.com/" target="_blank" rel="noopener noreferrer">star-history.com</a>
· Part of <a href="{{ site.baseurl }}/">Loop Engineering</a>
</p>
</main>
<div class="sh-overlay" id="token-overlay" role="dialog" aria-modal="true" aria-labelledby="token-title">
<div class="sh-modal">
<h2 id="token-title">GitHub access token</h2>
<p>
Star history reads from the GitHub API. Since mid-2026, stargazer data is limited to repos you
<strong>own or collaborate on</strong> — and requires a scoped token.
</p>
<p>
Create a
<a href="https://github.com/settings/personal-access-tokens/new" target="_blank" rel="noopener noreferrer">fine-grained token</a>
with only <strong>Metadata → Read-only</strong>,
or a
<a href="https://github.com/settings/tokens/new?scopes=public_repo&description=loop-engineering-star-history" target="_blank" rel="noopener noreferrer">classic token</a>
with <code>public_repo</code>.
<a href="https://www.star-history.com/blog/how-to-use-github-star-history#how-to-add-your-github-access-token" target="_blank" rel="noopener noreferrer">More details</a>.
</p>
<p><strong>Tokens with no scopes no longer work</strong>, even for your own repos.</p>
<ul>
<li><strong>Fine-grained:</strong> Repository access → this repo. Permissions → Metadata → Read-only.</li>
<li><strong>Classic:</strong> tick <code>public_repo</code>.</li>
</ul>
<label for="token-input">Access token (stored in local storage only)</label>
<input type="password" id="token-input" placeholder="ghp_… or github_pat_…" autocomplete="off" spellcheck="false" />
<div class="sh-modal-actions">
<button type="button" class="sh-btn sh-btn-primary" id="save-token-btn">Save token</button>
<button type="button" class="sh-btn sh-btn-ghost" id="clear-token-btn">Clear</button>
<button type="button" class="sh-btn sh-btn-ghost" id="close-token-btn">Close</button>
</div>
<p class="sh-note">Never sent to Loop Engineering servers — chart requests go directly to GitHub.</p>
</div>
</div>
<script>
(function () {
const STORAGE_KEY = 'loop-engineering:github-star-history-token';
const COLORS = ['#e74c3c', '#3498db', '#2ecc71', '#f39c12'];
const MAX_REPOS = 4;
const overlay = document.getElementById('token-overlay');
const tokenInput = document.getElementById('token-input');
const chartWrap = document.getElementById('chart-wrap');
const emptyState = document.getElementById('empty-state');
const statusEl = document.getElementById('status');
const repoInput = document.getElementById('repo-input');
const pillsWrap = document.getElementById('pills-wrap');
const clearAllBtn = document.getElementById('clear-all-btn');
let repos = [];
let chartData = [];
function getToken() {
return localStorage.getItem(STORAGE_KEY) || '';
}
function setToken(value) {
const trimmed = (value || '').trim();
if (trimmed) localStorage.setItem(STORAGE_KEY, trimmed);
else localStorage.removeItem(STORAGE_KEY);
}
function openTokenPanel() {
tokenInput.value = getToken();
overlay.classList.add('open');
tokenInput.focus();
}
function closeTokenPanel() {
overlay.classList.remove('open');
}
function parseRepos(value) {
const raw = (value || '').trim();
if (!raw) return [];
return raw.split(/[,\s]+/).map((chunk) => {
const t = chunk.trim();
if (!t) return null;
const urlMatch = t.match(/github\.com\/([^/]+)\/([^/#?]+)/i);
if (urlMatch) return { owner: urlMatch[1], repo: urlMatch[2].replace(/\.git$/, '') };
const parts = t.split('/').filter(Boolean);
if (parts.length >= 2) return { owner: parts[0], repo: parts[1].replace(/\.git$/, '') };
return null;
}).filter(Boolean);
}
function repoKey(r) {
return `${r.owner}/${r.repo}`;
}
function setStatus(message, isError) {
statusEl.textContent = message || '';
statusEl.classList.toggle('error', !!isError);
}
function updateUrl() {
if (!repos.length) return;
const params = new URLSearchParams();
params.set('repos', repos.map(repoKey).join(','));
history.replaceState(null, '', `${location.pathname}?${params}`);
}
function readUrl() {
const q = new URLSearchParams(location.search).get('repos');
if (q) repoInput.value = q;
}
async function fetchStarTimestamps(owner, repo, token) {
const timestamps = [];
let page = 1;
while (true) {
const url = `https://api.github.com/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/stargazers?per_page=100&page=${page}`;
const res = await fetch(url, {
headers: {
Accept: 'application/vnd.github.v3.star+json',
Authorization: `Bearer ${token}`,
'X-GitHub-Api-Version': '2022-11-28',
},
});
if (res.status === 401) throw new Error('Token rejected (401). Use Metadata read-only or public_repo.');
if (res.status === 403) throw new Error('Rate limited or forbidden (403). Wait or check token scopes.');
if (res.status === 404) throw new Error(`Not found or hidden (404): ${owner}/${repo}`);
if (!res.ok) throw new Error(`GitHub API error ${res.status}`);
const data = await res.json();
if (!Array.isArray(data) || data.length === 0) break;
for (const row of data) {
if (row.starred_at) timestamps.push(new Date(row.starred_at));
}
const link = res.headers.get('Link') || '';
if (!link.includes('rel="next"')) break;
page += 1;
if (page > 500) break;
}
timestamps.sort((a, b) => a - b);
return timestamps;
}
function buildSeries(timestamps, alignTimeline) {
if (!timestamps.length) return [[new Date(), 0]];
const start = timestamps[0].getTime();
return timestamps.map((ts, i) => {
const x = alignTimeline ? (ts.getTime() - start) / 86400000 : ts;
return [x, i + 1];
});
}
function escapeXml(text) {
return String(text)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
function formatXLabel(value, alignTimeline) {
if (alignTimeline) {
const days = Math.round(value);
return days === 0 ? 'Day 0' : `Day ${days}`;
}
const d = value instanceof Date ? value : new Date(value);
return d.toLocaleDateString('en-US', { month: 'short', year: '2-digit' });
}
function wigglePoints(points, seed) {
return points.split(' ').map((pair, i) => {
const [x, y] = pair.split(',').map(Number);
const jx = Math.sin(seed + i * 0.7) * 1.2;
const jy = Math.cos(seed + i * 1.1) * 1.2;
return `${(x + jx).toFixed(1)},${(y + jy).toFixed(1)}`;
}).join(' ');
}
function renderChart() {
const visible = chartData.filter((d) => d.visible && d.series.length);
if (!visible.length) {
chartWrap.innerHTML = '';
chartWrap.appendChild(emptyState);
emptyState.style.display = '';
document.getElementById('export-png').disabled = true;
document.getElementById('export-csv').disabled = true;
document.getElementById('export-link').disabled = true;
document.getElementById('export-share').disabled = true;
return;
}
const alignTimeline = document.getElementById('align-timeline').checked;
const logScale = document.getElementById('log-scale').checked;
const legendPos = document.querySelector('input[name="legend"]:checked')?.value || 'top-left';
const width = 920;
const height = 400;
const margin = { top: 48, right: 32, bottom: 56, left: 72 };
const plotW = width - margin.left - margin.right;
const plotH = height - margin.top - margin.bottom;
let xMin = Infinity;
let xMax = -Infinity;
let yMax = 1;
for (const entry of visible) {
const s = entry.series;
xMin = Math.min(xMin, s[0][0] instanceof Date ? s[0][0].getTime() : s[0][0]);
xMax = Math.max(xMax, s[s.length - 1][0] instanceof Date ? s[s.length - 1][0].getTime() : s[s.length - 1][0]);
yMax = Math.max(yMax, s[s.length - 1][1]);
}
const xSpan = Math.max(xMax - xMin, 1);
const xPos = (x) => {
const v = x instanceof Date ? x.getTime() : x;
const base = alignTimeline ? 0 : xMin;
return margin.left + ((v - base) / xSpan) * plotW;
};
const yPos = (count) => {
const v = logScale ? Math.log10(Math.max(count, 1)) : count;
const max = logScale ? Math.log10(Math.max(yMax, 1)) : yMax;
return margin.top + plotH - (v / Math.max(max, 1)) * plotH;
};
let svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" id="star-chart-svg" role="img">
<defs>
<filter id="xkcd-sketch" filterUnits="userSpaceOnUse" x="-4" y="-4" width="${width + 8}" height="${height + 8}">
<feTurbulence type="fractalNoise" baseFrequency="0.03" numOctaves="3" result="noise"/>
<feDisplacementMap scale="2.8" xChannelSelector="R" yChannelSelector="G" in="SourceGraphic" in2="noise"/>
</filter>
</defs>
<rect width="100%" height="100%" fill="#fff"/>
<g filter="url(#xkcd-sketch)" font-family="Patrick Hand, cursive">
<line x1="${margin.left}" y1="${margin.top + plotH}" x2="${margin.left + plotW}" y2="${margin.top + plotH}" stroke="#333" stroke-width="1.5"/>
<line x1="${margin.left}" y1="${margin.top}" x2="${margin.left}" y2="${margin.top + plotH}" stroke="#333" stroke-width="1.5"/>`;
const yStep = yMax > 2000 ? Math.ceil(yMax / 4 / 500) * 500 : yMax > 500 ? 250 : yMax > 100 ? 100 : 25;
for (let v = 0; v <= yMax; v += yStep) {
const y = yPos(v);
svg += `<line x1="${margin.left - 5}" y1="${y.toFixed(1)}" x2="${margin.left}" y2="${y.toFixed(1)}" stroke="#999" stroke-width="1"/>`;
svg += `<text x="${margin.left - 10}" y="${(y + 4).toFixed(1)}" text-anchor="end" font-size="13" fill="#555">${v.toLocaleString()}</text>`;
}
const xTicks = 5;
for (let i = 0; i <= xTicks; i++) {
const t = xMin + (xSpan * i) / xTicks;
const x = xPos(alignTimeline ? t - xMin : t);
const label = formatXLabel(alignTimeline ? t - xMin : new Date(t), alignTimeline);
svg += `<text x="${x.toFixed(1)}" y="${(margin.top + plotH + 22).toFixed(1)}" text-anchor="middle" font-size="12" fill="#555">${escapeXml(label)}</text>`;
}
visible.forEach((entry, idx) => {
const color = entry.color;
const pts = entry.series.map(([x, c]) => `${xPos(x).toFixed(1)},${yPos(c).toFixed(1)}`).join(' ');
const wiggled = wigglePoints(pts, idx * 2.1 + 0.5);
svg += `<polyline points="${wiggled}" fill="none" stroke="${color}" stroke-width="2.5" stroke-linejoin="round" stroke-linecap="round"/>`;
const last = entry.series[entry.series.length - 1];
svg += `<circle cx="${xPos(last[0]).toFixed(1)}" cy="${yPos(last[1]).toFixed(1)}" r="4" fill="${color}"/>`;
});
const legendBaseY = legendPos === 'bottom-right' ? margin.top + plotH - visible.length * 18 : margin.top + 12;
const legendX = legendPos === 'bottom-right' ? margin.left + plotW - 12 : margin.left + 12;
visible.forEach((entry, i) => {
const ly = legendBaseY + i * 18;
const dotX = legendPos === 'bottom-right' ? legendX - 120 : legendX;
const textX = legendPos === 'bottom-right' ? legendX : legendX + 14;
const anchor = legendPos === 'bottom-right' ? 'end' : 'start';
svg += `<circle cx="${dotX}" cy="${ly}" r="5" fill="${entry.color}"/>`;
svg += `<text x="${textX}" y="${ly + 4}" text-anchor="${anchor}" font-size="13" fill="#333">${escapeXml(entry.label)} (${entry.series[entry.series.length - 1][1].toLocaleString()})</text>`;
});
svg += `</g></svg>`;
chartWrap.innerHTML = svg;
document.getElementById('export-png').disabled = false;
document.getElementById('export-csv').disabled = false;
document.getElementById('export-link').disabled = false;
document.getElementById('export-share').disabled = false;
}
function renderPills() {
pillsWrap.querySelectorAll('.sh-pill').forEach((el) => el.remove());
repos.forEach((r, i) => {
const key = repoKey(r);
const entry = chartData.find((d) => d.key === key);
const pill = document.createElement('span');
pill.className = 'sh-pill' + (entry && !entry.visible ? ' hidden-series' : '');
pill.innerHTML = `<span class="sh-pill-dot" style="background:${COLORS[i % COLORS.length]}"></span><span>${key}</span><button type="button" class="sh-pill-remove" aria-label="Remove ${key}">×</button>`;
pill.addEventListener('click', (e) => {
if (e.target.closest('.sh-pill-remove')) return;
if (entry) {
entry.visible = !entry.visible;
renderPills();
renderChart();
}
});
pill.querySelector('.sh-pill-remove').addEventListener('click', (e) => {
e.stopPropagation();
repos = repos.filter((x) => repoKey(x) !== key);
chartData = chartData.filter((d) => d.key !== key);
repoInput.value = repos.map(repoKey).join(',');
renderPills();
renderChart();
updateUrl();
});
pillsWrap.insertBefore(pill, clearAllBtn);
});
clearAllBtn.hidden = repos.length === 0;
}
async function loadChart() {
const parsed = parseRepos(repoInput.value);
if (!parsed.length) {
setStatus('Enter at least one repo as owner/repo.', true);
return;
}
if (parsed.length > MAX_REPOS) {
setStatus(`Compare up to ${MAX_REPOS} repos at once.`, true);
return;
}
const token = getToken();
if (!token) {
setStatus('Add an access token first (settings ⚙).', true);
openTokenPanel();
return;
}
repos = parsed;
updateUrl();
setStatus('Loading…');
emptyState.style.display = 'none';
const alignTimeline = document.getElementById('align-timeline').checked;
chartData = [];
try {
for (let i = 0; i < repos.length; i++) {
const r = repos[i];
const key = repoKey(r);
setStatus(`Loading ${key} (${i + 1}/${repos.length})…`);
const timestamps = await fetchStarTimestamps(r.owner, r.repo, token);
chartData.push({
key,
label: key,
color: COLORS[i % COLORS.length],
visible: true,
timestamps,
series: buildSeries(timestamps, alignTimeline),
});
}
renderPills();
renderChart();
const total = chartData.reduce((n, d) => n + d.timestamps.length, 0);
setStatus(`Loaded ${total.toLocaleString()} stars across ${chartData.length} repo(s).`);
} catch (err) {
chartWrap.innerHTML = '';
chartWrap.appendChild(emptyState);
emptyState.style.display = '';
setStatus(err.message || String(err), true);
}
}
function exportCsv() {
const rows = [['repo', 'date', 'cumulative_stars']];
for (const entry of chartData) {
entry.timestamps.forEach((ts, i) => {
rows.push([entry.label, ts.toISOString(), String(i + 1)]);
});
}
const csv = rows.map((r) => r.map((c) => `"${String(c).replace(/"/g, '""')}"`).join(',')).join('\n');
const blob = new Blob([csv], { type: 'text/csv' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'star-history.csv';
a.click();
URL.revokeObjectURL(a.href);
}
function exportPng() {
const svg = document.getElementById('star-chart-svg');
if (!svg) return;
const xml = new XMLSerializer().serializeToString(svg);
const img = new Image();
const url = URL.createObjectURL(new Blob([xml], { type: 'image/svg+xml' }));
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = svg.width.baseVal.value * 2;
canvas.height = svg.height.baseVal.value * 2;
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
canvas.toBlob((blob) => {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'star-history.png';
a.click();
URL.revokeObjectURL(a.href);
});
URL.revokeObjectURL(url);
};
img.src = url;
}
function exportLink() {
updateUrl();
navigator.clipboard.writeText(location.href).then(() => setStatus('Link copied to clipboard.'));
}
function exportShare() {
const text = `Star history: ${repos.map(repoKey).join(', ')}`;
const url = location.href;
window.open(`https://twitter.com/intent/tweet?text=${encodeURIComponent(text)}&url=${encodeURIComponent(url)}`, '_blank', 'noopener');
}
document.getElementById('open-token-btn').addEventListener('click', openTokenPanel);
document.getElementById('banner-token-btn').addEventListener('click', openTokenPanel);
document.getElementById('close-token-btn').addEventListener('click', closeTokenPanel);
document.getElementById('save-token-btn').addEventListener('click', () => {
setToken(tokenInput.value);
closeTokenPanel();
setStatus('Token saved locally.');
});
document.getElementById('clear-token-btn').addEventListener('click', () => {
setToken('');
tokenInput.value = '';
setStatus('Token cleared.');
});
document.getElementById('load-chart-btn').addEventListener('click', loadChart);
document.getElementById('clear-all-btn').addEventListener('click', () => {
repos = [];
chartData = [];
repoInput.value = '';
renderPills();
chartWrap.innerHTML = '';
chartWrap.appendChild(emptyState);
emptyState.style.display = '';
history.replaceState(null, '', location.pathname);
setStatus('');
});
document.getElementById('align-timeline').addEventListener('change', () => {
const align = document.getElementById('align-timeline').checked;
chartData.forEach((d) => { d.series = buildSeries(d.timestamps, align); });
renderChart();
});
document.getElementById('log-scale').addEventListener('change', renderChart);
document.querySelectorAll('input[name="legend"]').forEach((el) => el.addEventListener('change', renderChart));
document.getElementById('export-png').addEventListener('click', exportPng);
document.getElementById('export-csv').addEventListener('click', exportCsv);
document.getElementById('export-link').addEventListener('click', exportLink);
document.getElementById('export-share').addEventListener('click', exportShare);
overlay.addEventListener('click', (e) => { if (e.target === overlay) closeTokenPanel(); });
document.addEventListener('keydown', (e) => { if (e.key === 'Escape') closeTokenPanel(); });
repoInput.addEventListener('keydown', (e) => { if (e.key === 'Enter') loadChart(); });
readUrl();
if (!repoInput.value) repoInput.value = 'cobusgreyling/loop-engineering';
if (new URLSearchParams(location.search).get('token') === '1') openTokenPanel();
if (new URLSearchParams(location.search).get('repos')) loadChart();
})();
</script>
</body>
</html>