Repository navigation
Expand file tree
/
Copy pathgithub-account.html
More file actions
382 lines (349 loc) · 14.5 KB
/
Copy pathgithub-account.html
File metadata and controls
382 lines (349 loc) · 14.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>GitHub account lookup</title>
<style>
:root {
--bg: #0f172a; /* slate-900 */
--panel: #111827; /* gray-900 */
--muted: #94a3b8; /* slate-400 */
--text: #e5e7eb; /* gray-200 */
--accent: #22d3ee; /* cyan-400 */
--danger: #f87171; /* red-400 */
--ok: #34d399; /* green-400 */
}
* { box-sizing: border-box; }
body {
margin: 0; font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
background: radial-gradient(1200px 800px at 80% -20%, rgba(34,211,238,0.08), transparent 40%),
radial-gradient(1200px 800px at -10% 110%, rgba(34,197,94,0.1), transparent 40%),
var(--bg);
color: var(--text);
min-height: 100vh;
display: grid; place-items: center; padding: 24px;
}
.card {
width: 100%; max-width: 680px; background: rgba(17,24,39,0.8);
border: 1px solid rgba(148,163,184,0.2); border-radius: 16px; padding: 24px; backdrop-filter: blur(6px);
box-shadow: 0 10px 30px rgba(0,0,0,0.45);
}
h1 { margin: 0 0 6px; font-size: 24px; letter-spacing: 0.3px; }
p.sub { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
form { display: grid; gap: 12px; margin: 12px 0 8px; }
.sr-only {
position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.mode-control {
display: inline-grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px;
padding: 4px; border-radius: 12px; border: 1px solid rgba(148,163,184,0.25);
background: #0b1220; width: min(100%, 320px);
}
.mode-control input { position: absolute; opacity: 0; pointer-events: none; }
.mode-control label {
display: flex; align-items: center; justify-content: center; min-height: 36px;
padding: 8px 12px; border-radius: 8px; border: 1px solid transparent;
color: var(--muted); cursor: pointer; font-size: 14px; font-weight: 600;
}
.mode-control input:checked + label {
background: rgba(34,211,238,0.14); border-color: rgba(34,211,238,0.35); color: var(--text);
}
.mode-control input:focus-visible + label { box-shadow: 0 0 0 3px rgba(34,211,238,0.2); }
.lookup-row { display: grid; grid-template-columns: 1fr auto; gap: 12px; }
input[type="text"] {
appearance: none; -webkit-appearance: none;
width: 100%; padding: 14px 14px; border-radius: 12px; border: 1px solid rgba(148,163,184,0.25);
background: #0b1220; color: var(--text); outline: none; font-size: 16px;
}
input[type="text"]:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(34,211,238,0.2); }
button {
padding: 14px 18px; border-radius: 12px; border: 1px solid rgba(148,163,184,0.25);
background: linear-gradient(180deg, rgba(34,211,238,0.15), rgba(34,211,238,0.05)); color: var(--text);
cursor: pointer; font-weight: 600; white-space: nowrap;
}
button:disabled { opacity: 0.6; cursor: not-allowed; }
.result { margin-top: 18px; padding: 16px; border-radius: 12px; background: #0b1220; border: 1px dashed rgba(148,163,184,0.25); }
.row { display: grid; grid-template-columns: 140px minmax(0, 1fr); gap: 12px; padding: 10px 0; border-bottom: 1px solid rgba(148,163,184,0.1); }
.row:last-child { border-bottom: none; }
.label { color: var(--muted); }
.value { font-weight: 600; min-width: 0; overflow-wrap: break-word; }
.value .raw { display: block; font-weight: 400; }
.avatar {
width: 64px; height: 64px; border-radius: 50%; border: 1px solid rgba(148,163,184,0.25);
box-shadow: 0 6px 18px rgba(0,0,0,0.3); object-fit: cover;
}
.flex { display: flex; align-items: center; gap: 12px; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-weight: 600; }
.ok { color: var(--ok); font-weight: 600; }
.footer { margin-top: 12px; font-size: 12px; color: var(--muted); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
@media (max-width: 520px) {
.lookup-row { grid-template-columns: 1fr; }
button { width: 100%; }
.row { grid-template-columns: 1fr; gap: 4px; padding: 12px 0; }
}
</style>
</head>
<body>
<div class="card">
<h1>GitHub account lookup</h1>
<p class="sub">Look up account details by GitHub username or by numeric account ID.</p>
<form id="lookup-form" autocomplete="off">
<div class="mode-control" role="radiogroup" aria-label="Lookup type">
<input id="lookup-username" name="lookup-type" type="radio" value="username" checked />
<label for="lookup-username">Username</label>
<input id="lookup-account-id" name="lookup-type" type="radio" value="id" />
<label for="lookup-account-id">Account ID</label>
</div>
<div class="lookup-row">
<label id="lookup-label" class="sr-only" for="username">GitHub username</label>
<input id="username" name="username" type="text" inputmode="latin" pattern="[A-Za-z0-9-]+" placeholder="e.g. torvalds or 9599" required />
<button id="submit-btn" type="submit">Lookup</button>
</div>
</form>
<div id="result" class="result" role="status" aria-live="polite">
<span class="muted">Results will appear here.</span>
</div>
<p class="footer">Powered by <a href="https://docs.github.com/rest/users/users#get-a-user" target="_blank" rel="noreferrer noopener">GitHub REST API</a>. Unauthenticated requests are limited (HTTP 403 when rate-limited).</p>
</div>
<script>
const form = document.getElementById('lookup-form');
const result = document.getElementById('result');
const submitBtn = document.getElementById('submit-btn');
const input = document.getElementById('username');
const lookupLabel = document.getElementById('lookup-label');
const usernameMode = document.getElementById('lookup-username');
const accountIdMode = document.getElementById('lookup-account-id');
const modeInputs = document.querySelectorAll('input[name="lookup-type"]');
const RATE_LIMIT_STORAGE_KEY = 'github-account-rate-limit-reset';
function formatDate(iso) {
try {
const d = new Date(iso);
return new Intl.DateTimeFormat(undefined, {
year: 'numeric', month: 'long', day: 'numeric',
hour: 'numeric', minute: '2-digit', timeZoneName: 'short'
}).format(d);
} catch { return iso; }
}
function sanitize(text) { return String(text).replace(/[&<>"']/g, s => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[s])); }
function setLoading(isLoading) {
submitBtn.disabled = isLoading;
submitBtn.textContent = isLoading ? 'Looking…' : 'Lookup';
}
function getStoredRateLimitReset() {
let stored;
try {
stored = Number(localStorage.getItem(RATE_LIMIT_STORAGE_KEY));
} catch {
return null;
}
if (!Number.isFinite(stored) || stored <= Date.now()) {
try {
localStorage.removeItem(RATE_LIMIT_STORAGE_KEY);
} catch {}
return null;
}
return new Date(stored);
}
function getRateLimitResetFromResponse(resp) {
const retryAfter = Number(resp.headers.get('Retry-After'));
if (Number.isFinite(retryAfter) && retryAfter > 0) {
return new Date(Date.now() + retryAfter * 1000);
}
const reset = Number(resp.headers.get('X-RateLimit-Reset'));
if (Number.isFinite(reset) && reset > 0) {
const resetAt = new Date(reset * 1000);
return resetAt.getTime() > Date.now() ? resetAt : null;
}
return null;
}
function rememberRateLimit(resetAt) {
if (resetAt && resetAt.getTime() > Date.now()) {
try {
localStorage.setItem(RATE_LIMIT_STORAGE_KEY, String(resetAt.getTime()));
} catch {}
}
}
function formatReset(resetAt) {
if (!resetAt) return 'later';
return resetAt.toLocaleTimeString([], {
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short'
});
}
function showRateLimitMessage(resetAt) {
result.innerHTML = `<div class="error">GitHub API rate limit reached. Try again after ${sanitize(formatReset(resetAt))}.</div>`;
}
function getLookupType() {
return accountIdMode.checked ? 'id' : 'username';
}
function setLookupType(type) {
accountIdMode.checked = type === 'id';
usernameMode.checked = type !== 'id';
updateInputForLookupType();
}
function updateInputForLookupType() {
if (getLookupType() === 'id') {
lookupLabel.textContent = 'GitHub account ID';
input.name = 'id';
input.inputMode = 'numeric';
input.pattern = '\\d+';
input.placeholder = 'e.g. 583231';
input.title = 'Enter a numeric GitHub account ID';
} else {
lookupLabel.textContent = 'GitHub username';
input.name = 'username';
input.inputMode = 'latin';
input.pattern = '[A-Za-z0-9-]+';
input.placeholder = 'e.g. torvalds or 9599';
input.title = 'Enter a GitHub username';
}
}
function parseLookup(value, type = getLookupType()) {
const trimmed = value.trim();
return {
value: trimmed,
type,
label: type === 'id' ? 'Account ID' : 'User',
apiPath: type === 'id' ? `/user/${encodeURIComponent(trimmed)}` : `/users/${encodeURIComponent(trimmed)}`
};
}
function updateUrl(lookupInfo) {
const url = new URL(location.href);
url.searchParams.delete('username');
url.searchParams.delete('account_id');
url.searchParams.delete('id');
url.searchParams.set(lookupInfo.type, lookupInfo.value);
history.replaceState(null, '', url);
}
function getInitialLookup() {
const params = new URLSearchParams(location.search);
const accountId = params.get('id');
if (accountId) {
return parseLookup(accountId, 'id');
}
const username = params.get('username');
if (username) {
return parseLookup(username, 'username');
}
return null;
}
async function readGitHubError(resp) {
const text = await resp.text();
try {
const data = JSON.parse(text);
return data.message || text;
} catch {
return text;
}
}
async function lookup(lookupInfo) {
const url = `https://api.github.com${lookupInfo.apiPath}`;
const headers = { 'Accept': 'application/vnd.github+json' };
const started = Date.now();
try {
const resp = await fetch(url, { headers });
const elapsed = Date.now() - started;
if (resp.status === 404) {
result.innerHTML = `<span class="error">${lookupInfo.label} not found.</span>`;
return;
}
if (resp.status === 403 || resp.status === 429) {
const message = await readGitHubError(resp);
const remaining = resp.headers.get('X-RateLimit-Remaining');
const resetAt = getRateLimitResetFromResponse(resp);
const isRateLimit = resp.status === 429 || remaining === '0' || /rate limit/i.test(message);
if (isRateLimit) {
const cooldownUntil = resetAt || new Date(Date.now() + 60 * 1000);
rememberRateLimit(cooldownUntil);
showRateLimitMessage(cooldownUntil);
return;
}
result.innerHTML = `<div class="error">Request blocked (${resp.status}). ${sanitize(message).slice(0, 240)}</div>`;
return;
}
if (!resp.ok) {
const text = await resp.text();
result.innerHTML = `<div class="error">Unexpected error (${resp.status}). ${sanitize(text).slice(0, 240)}</div>`;
return;
}
const data = await resp.json();
const created = formatDate(data.created_at);
const avatar = sanitize(data.avatar_url || '');
const name = sanitize(data.name || '');
const login = sanitize(data.login || lookupInfo.value);
const htmlUrl = sanitize(data.html_url || (data.login ? `https://github.com/${data.login}` : ''));
const accountLink = htmlUrl
? `<a href="${htmlUrl}" target="_blank" rel="noreferrer noopener">${name || login}</a>`
: `${name || login}`;
result.innerHTML = `
<div class="row">
<div class="label">Account</div>
<div class="value flex">
<img class="avatar" src="${avatar}" alt="Avatar of ${login}" />
<div>
<div>${accountLink}</div>
<div class="muted">@${login}</div>
</div>
</div>
</div>
<div class="row">
<div class="label">Account ID</div>
<div class="value">${sanitize(String(data.id))}</div>
</div>
<div class="row">
<div class="label">Created</div>
<div class="value">${sanitize(created)}<span class="muted raw">raw: ${sanitize(data.created_at)}</span></div>
</div>
<div class="row">
<div class="label">API Latency</div>
<div class="value muted">~${elapsed} ms</div>
</div>
`;
} catch (err) {
result.innerHTML = `<div class="error">Network error. ${sanitize(String(err))}</div>`;
}
}
async function runLookup(lookupInfo, { shouldUpdateUrl = true } = {}) {
if (!lookupInfo.value) return;
if (shouldUpdateUrl) {
updateUrl(lookupInfo);
}
const resetAt = getStoredRateLimitReset();
if (resetAt) {
showRateLimitMessage(resetAt);
return;
}
setLoading(true);
result.innerHTML = '<span class="muted">Fetching…</span>';
try {
await lookup(lookupInfo);
} finally {
setLoading(false);
}
}
form.addEventListener('submit', (e) => {
e.preventDefault();
runLookup(parseLookup(input.value));
});
modeInputs.forEach((modeInput) => {
modeInput.addEventListener('change', updateInputForLookupType);
});
window.addEventListener('DOMContentLoaded', () => {
const lookupInfo = getInitialLookup();
if (lookupInfo) {
setLookupType(lookupInfo.type);
input.value = lookupInfo.value;
runLookup(lookupInfo, { shouldUpdateUrl: false });
} else {
updateInputForLookupType();
}
});
</script>
</body>
</html>