Repository navigation
Expand file tree
/
Copy pathhn-comments-for-user.html
More file actions
102 lines (96 loc) · 4.02 KB
/
Copy pathhn-comments-for-user.html
File metadata and controls
102 lines (96 loc) · 4.02 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hacker News comments for a user</title>
<style>
body { font-family: Arial, sans-serif; background: #f5f7fa; margin: 0; padding: 20px; color: #222; }
h1 { color: #ff6600; font-size: 1.6rem; margin-bottom: 20px; }
label { font-weight: bold; margin-right: 8px; }
input { padding: 8px; border: 1px solid #ccc; border-radius: 6px; font-size: 16px; }
button { padding: 8px 12px; margin: 5px; border: none; border-radius: 6px; cursor: pointer; background: #ff6600; color: white; font-weight: bold; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
textarea { width: 100%; min-height: 400px; margin-top: 15px; padding: 10px; border-radius: 6px; border: 1px solid #ccc; font-family: monospace; font-size: 16px; }
.controls { margin-bottom: 10px; }
#note { margin-top: 10px; font-size: 14px; color: #555; }
</style>
</head>
<body>
<h1>Hacker News comments for a user</h1>
<form id="user-form" class="controls">
<label for="hn-user">Hacker News handle:</label>
<input id="hn-user" type="text" placeholder="e.g., pg" />
<button id="fetchBtn" type="submit">Fetch comments</button>
<button id="copyBtn" type="button" disabled>Copy Output</button>
</form>
<p>Fetches up to 1,000 recent comments.</p>
<textarea id="output" placeholder="Comments will appear here..."></textarea>
<div id="note"></div>
<script>
const form = document.getElementById('user-form');
const username = document.getElementById('hn-user');
const fetchBtn = document.getElementById('fetchBtn');
const copyBtn = document.getElementById('copyBtn');
const output = document.getElementById('output');
const note = document.getElementById('note');
const apiUrl = (user, page) =>
`https://hn.algolia.com/api/v1/search_by_date?tags=comment,author_${encodeURIComponent(user)}&hitsPerPage=1000&page=${page}`;
const formatDate = (iso) => {
const d = new Date(iso);
const pad = (n)=> String(n).padStart(2,'0');
return `${d.getFullYear()}-${pad(d.getMonth()+1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
};
const htmlToText = (html) => {
if (!html) return '';
const div = document.createElement('div');
div.innerHTML = html;
return (div.textContent || div.innerText || '').trim();
};
async function fetchComments(user) {
if (!user) return;
fetchBtn.disabled = true;
copyBtn.disabled = true;
output.value = '';
note.textContent = '';
let all = [];
const res = await fetch(apiUrl(user, 0));
const data = await res.json();
all = all.concat(data.hits || []);
const lines = all.map(h => {
const date = formatDate(h.created_at);
const commentUrl = `https://news.ycombinator.com/item?id=${h.objectID}`;
const threadUrl = h.story_id ? `https://news.ycombinator.com/item?id=${h.story_id}` : '';
const storyTitle = h.story_title || h.title || '(no title)';
const text = htmlToText(h.comment_text);
return [
`Date: ${date}`,
`Story: ${storyTitle}`,
`Thread: ${threadUrl}`,
`Comment: ${commentUrl}`,
`Text:`,
text || '(no text)',
`\n---\n`
].join('\n');
});
output.value = lines.join('\n');
copyBtn.disabled = all.length === 0;
fetchBtn.disabled = false;
note.textContent = `Fetched ${all.length} comment(s).`;
}
const submitUsername = () => fetchComments(username.value.trim());
form.addEventListener('submit', (event) => {
event.preventDefault();
submitUsername();
});
copyBtn.addEventListener('click', async () => {
await navigator.clipboard.writeText(output.value);
const originalText = copyBtn.textContent;
copyBtn.textContent = 'Copied!';
setTimeout(() => {
copyBtn.textContent = originalText;
}, 1500);
});
</script>
</body>
</html>