-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathhn.html
204 lines (200 loc) · 6.41 KB
/
hn.html
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
<!doctype html>
<html lang="en">
<head>
<meta name='viewport' content='initial-scale=1.0, minimum-scale=0.25'>
<title>Hacker News</title>
<style>
body {
font-family: sans-serif;
font-size: 1em;
}
a:link {
color: #000000;
text-decoration: none;
}
a:visited {
color: #828282;
text-decoration: none;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #121212;
color: #888;
}
a:link {
color: white;
}
}
#more {
text-align: center;
display: block;
border: 1px solid silver;
padding: 4px;
}
.item {
font-size: 1em;
padding-bottom: 1em;
border-bottom: 1px solid silver;
margin-bottom: 1em;
}
.item:last-of-type {
border-bottom: none;
}
#user {
float: right;
}
</style>
</head>
<body>
<header><a href='https://news.ycombinator.com' target='_blank' rel='noopener'>Hacker News</a><span id=user>user</span></header>
<main>
<div id=loading></div>
<div id=contents></div>
</main>
<nav>
<a id=more>More</a>
<br><br>
<a href='hn.html'>Default</a> | <a href='hn.html?small'>Small</a>
</nav>
<br><br>
<footer>
<span id=itemsSeen></span> | <a href='https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/hn.html' target='_blank'>src</a>
<br><br><br><br>
</footer>
<script>
var ids;
var n = 1;
var seen = {};
var fetchCounter = 0;
const pageLen = 6;
const baseUrl = 'https://hacker-news.firebaseio.com/v0'; // see https://github.com/HackerNews/API
const alBaseUrl = 'https://hn.algolia.com'; // see https://hn.algolia.com/api
const hnBaseUrl = 'https://news.ycombinator.com';
if (localStorage.user) {
setUser();
} else {
user.onclick = () => {
const u = prompt('username');
if (!u) return;
user.onclick = null;
localStorage.user = u;
setUser();
};
}
function setUser() {
user.innerHTML = link(`${hnBaseUrl}/threads?id=${localStorage.user}`, localStorage.user) + ' | <a id=submissions>submissions</a>';
submissions.onclick = async () => {
const r = await fetch(`${alBaseUrl}/api/v1/search_by_date?tags=story,author_${localStorage.user}`);
const u = await r.json();
const items = u.hits.map(item => '<div class=item>' + link(`${hnBaseUrl}/item?id=${item.objectID}`, item.title + '<br>' +
new Intl.DateTimeFormat(undefined, {dateStyle: 'full', timeStyle: 'long'}).format(new Date(item.created_at_i * 1000)) + ' | ' +
plu(item.points, 'point')) + '</div>');
contents.innerHTML = `<br>${localStorage.user}'s submissions<br><br>` + items.join('');
};
}
more.onclick = getItems;
if (!localStorage.seen) localStorage.seen = '{}';
fetchItems();
async function fetchItems() {
const response = await fetch(baseUrl + '/topstories.json');
ids = await response.json();
getItems();
}
// TODO: Items might render out of order. Do we care?
async function getItems() {
function showItem(item) { // other item props: kids
loading.innerHTML = '';
const hnItemUrl = `${hnBaseUrl}/item?id=${item.id}`;
item.title = item.title.replace(/</g, '<');
if (item.url) {
const url = new URL(item.url);
var site = url.hostname.replace(/^www\./, '');
var web = ' | ' + link(`https://www.google.com/search?q=${encodeURIComponent(item.title)}+site:${site}`, 'web');
if (site.includes('twitter.com')) {
var lsite = site + '/' + url.pathname.split('/')[1];
web += ' | ' + link(item.url.replace('twitter.com', 'nitter.net'), 'nitter');
} else {
lsite = site;
}
var ln1 = `${link(item.url, item.title)} (${link(`${hnBaseUrl}/from?site=${lsite}`, lsite)})`;
} else {
web = '';
ln1 = link(hnItemUrl, item.title);
}
var ago = new Date() / 1000 - item.time; // in seconds, 60 s == 1 min, 3600 s = 1 hr, etc.
if (ago < 3600) {
ago = plu(Math.round(ago / 60), 'minute');
} else if (ago < 3600 * 24) {
ago = plu(Math.round(ago / 3600), 'hour');
} else {
ago = plu(Math.round(ago / 3600 / 24), 'day');
}
ago = link(hnItemUrl, ago + ' ago');
if (item.type == 'job') {
var pts = '';
var by = '';
var ln2 = ago;
} else {
pts = plu(item.score, 'point');
by = 'by ' + link(`${hnBaseUrl}/user?id=${item.by}`, item.by);
ln2 = ago + ' | ' + link(hnItemUrl, item.descendants == 0 ? 'discuss' : plu(item.descendants, 'comment')) + web;
}
if (small) {
contents.innerHTML += `<div class=item>${ln1}<br>${ln2}</div>`;
} else {
ol.innerHTML += `<li class=item value=${n++}>${ln1}<br>${pts} ${by} ${ln2}</li>`;
}
}
const small = location.search.match('small');
if (small) {
contents.innerHTML = '<br>';
} else {
contents.innerHTML = '<ol id=ol></ol>';
}
const now = new Date().getTime();
const newSeen = JSON.parse(localStorage.seen);
for (const id in seen) {
if (!newSeen[id]) newSeen[id] = now;
}
const tenDays = 1000 * 3600 * 24 * 10;
for (const id in newSeen) {
if (now - newSeen[id] > tenDays) delete newSeen[id];
}
localStorage.seen = JSON.stringify(newSeen);
itemsSeen.innerHTML = Object.keys(newSeen).length + ' seen';
seen = {};
ids = ids.filter(id => !newSeen[id]);
if (ids.length == 0) {
if (fetchCounter == 0) {
fetchCounter = 1;
loading.innerHTML = '<br>Refreshing...'
fetchItems();
} else {
loading.innerHTML = '<br>No more';
more.style.display = 'none';
}
} else {
loading.innerHTML = '<br>Loading...';
await Promise.all(ids.slice(0, pageLen).map(async id => {
seen[id] = now;
if (!newSeen[id]) {
const r = await fetch(`${baseUrl}/item/${id}.json`);
const item = await r.json();
if (item && !item.deleted && !item.dead) showItem(item); // sometimes item === null
}
}));
for (var i = 0; i < pageLen - ids.length; i++) {
contents.innerHTML += `<p><br><br></p>`;
}
fetchCounter = 0;
}
}
function plu(n, s) {
return n + ' ' + (n == 1 ? s : s + 's');
}
function link(href, text) {
return `<a href="${href}" target='_blank' rel='noopener'>${text}</a>`;
}
</script>
</body>
</html>