-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
469 lines (437 loc) · 14.7 KB
/
index.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
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
<!DOCTYPE html>
<head>
<title>dumppit</title>
<meta charset="utf8">
<meta name="referrer" content="no-referrer">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="A reddit firehose">
<meta property="og:type" content="website">
<meta property="og:image" content="/terlet.png">
<meta property="og:url" content="https://dumppit.hotgarba.ge">
<meta name="twitter:card" content="summary_large_image">
<meta name="theme-color" content="#000000">
<link rel="icon" href="/terlet.png">
<link rel="apple-touch-icon" href="/terlet.png">
<link rel="manifest" href="/manifest.json">
<style>
html {
/* scroll-snap-type: y mandatory; */
color: white;
background: black;
-webkit-text-size-adjust: 100%;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
/* scroll-snap-type: y mandatory; */
display: flex;
flex-direction: column;
min-height: 100dvh;
line-height: 1.2;
}
body>header {
display: flex;
flex-flow: row wrap;
align-items: baseline;
padding: .25rem .5rem;
/* scroll-snap-align: start; */
}
body>footer {
position: fixed;
bottom: 0;
right: 0;
padding: .5rem;
}
h1,
h2 {
margin: 0;
}
h1 {
font-size: 1.3rem;
}
h2 {
display: inline;
font-size: 1.2rem;
}
a {
color: lightblue;
}
main {
display: flex;
flex-wrap: wrap;
flex: 1 0 auto;
}
article {
width: 100vmin;
max-height: 100vh;
flex: 1 0 auto;
padding: .5rem;
box-sizing: border-box;
display: flex;
flex-direction: column;
/* scroll-snap-align: start; */
}
article>.content {
flex: 1 1 content;
display: flex;
overflow: hidden;
}
article>.content>a {
display: contents;
}
article header,
article footer {
word-wrap: break-word;
margin-bottom: .25rem;
}
img,
video {
width: 100%;
height: 100%;
object-fit: contain;
}
.gallery {
display: flex;
height: 100%;
width: 100%;
overflow: hidden;
overflow-x: auto;
}
.gallery>img {
width: auto;
max-width: 100%;
}
</style>
</head>
<body>
<header>
<h1><a href="/">dumppit</a></h2>
<div style='margin-left: auto'>
<label>Text Posts: <input type='checkbox' id='selfPosts' checked></label>
<label>Show NSFW:
<select id="nsfw">
<option>No</option>
<option>Yes</option>
<option>Only</option>
</select>
</label>
</div>
</header>
<main>
<div style="width: 100%; font-size: 75vmin; text-align: center; align-self: end">🚽</div>
</main>
<footer>
<a target="_blank" href="https://github.com/hotgarbo/dumppit">About</a>
</footer>
<script type="text/javascript">
const domain = "https://old.reddit.com";
let urls = {};
let lastId = 0;
let isLoading = false;
window.onload = () => {
document.getElementById("nsfw").value = window.localStorage.getItem("nsfw") || "No";
document.getElementById("selfPosts").checked = window.localStorage.getItem("selfPosts") == "false" ? false : true;
let hash = window.location.hash.replace("#", "");
if (!hash) {
// Find latest post ID
fetchUrl(`${domain}/new.json?limit=2&jsonp=latest&ts=${Date.now()}`);
} else {
if (parseInt(hash)) {
// Load posts from provided ID
document.querySelector('main').innerHTML = '';
fetchPosts(hash);
} else {
// Load user/subreddit URL
fetchSubreddit(hash);
}
}
}
document.getElementById('selfPosts').onchange = (evt) => {
window.localStorage.setItem("selfPosts", evt.target.checked);
location.reload();
}
document.getElementById('nsfw').onchange = (evt) => {
if (evt.target.value != "No") {
if (!window.confirm("If you are under the age of 18 or find this content offensive, please don't look at it. Thank you!")) {
evt.target.value = "No";
window.localStorage.setItem("nsfw", evt.target.value);
return;
}
}
window.localStorage.setItem("nsfw", evt.target.value);
location.reload();
}
window.onhashchange = (evt) => {
console.debug(evt);
let hash = window.location.hash.replace("#", "");
document.querySelector('main').innerHTML = '';
urls = {};
if (parseInt(hash)) {
fetchPosts(hash);
} else {
fetchSubreddit(hash);
}
}
function latest(json) {
if (!json || !json.data || !json.data.after) {
alert("Failed to load latest post ID");
location.reload();
}
let id = base36ToInt(json.data.after);
console.debug(id);
document.querySelector('main').innerHTML = '';
fetchPosts(id);
}
function base36ToInt(b36) {
return parseInt(b36.replace("t3_", ""), 36);
}
function generateNextPosts(id) {
let next_ids = [];
for (let i = 0; i < 100; i++) {
id++;
next_ids.push("t3_" + id.toString(36));
}
return next_ids.join(',');
}
function fetchPosts(id) {
if (isLoading) return;
isLoading = true;
let next_ids = generateNextPosts(id);
let now = Date.now();
const url = `${domain}/api/info.json?id=${next_ids}&jsonp=processPosts&ts=${Math.round(now / 1000) * 1000}`;
fetchUrl(url);
}
function fetchSubreddit(url, after) {
if (isLoading) return;
isLoading = true;
if (!after) {
document.querySelector("main").innerHTML = "";
after = "";
}
let now = Date.now();
fetchUrl(`${domain}${url}.json?limit=100&after=${after}&jsonp=processSubreddit&ts=${Math.round(now / 1000) * 1000}`)
}
function fetchUrl(url) {
let jsonp = document.createElement("script");
jsonp.src = url;
jsonp.onerror = () => {
alert("Failed to load posts\nIf using Firefox, disable Enhanced Tracking Protection to allow loading external content from reddit");
};
document.body.appendChild(jsonp);
}
function processSubreddit(results) {
isLoading = false;
if (!results || !results.data.children.length) {
return
}
const posts = results.data.children;
for (let i = 0; i < posts.length; i++) {
addPost(posts[i]);
}
let scrollEndObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach(e => {
if (e.isIntersecting) {
observer.disconnect();
e.target.remove();
if (results.data.after) {
fetchSubreddit(window.location.hash.replace("#", ""), results.data.after);
}
}
})
},
{ threshold: 1.0 }
);
let scrollEndDiv = document.createElement('div');
scrollEndDiv.className = 'scrollEnd';
document.querySelector('main').appendChild(scrollEndDiv);
scrollEndObserver.observe(scrollEndDiv);
}
function processPosts(results) {
isLoading = false;
if (!results || !results.data.children.length) {
setTimeout(fetchPosts, 1000, lastId);
return
}
const posts = results.data.children;
for (let i = 0; i < posts.length; i++) {
lastId = base36ToInt(posts[i].data.id);
addPost(posts[i]);
}
let scrollEndObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach(e => {
if (e.isIntersecting) {
observer.disconnect();
e.target.remove();
if (lastId) {
fetchPosts(lastId);
}
}
})
},
{ threshold: 1.0 }
);
let scrollEndDiv = document.createElement('div');
scrollEndDiv.className = 'scrollEnd';
document.querySelector('main').appendChild(scrollEndDiv);
scrollEndObserver.observe(scrollEndDiv);
}
function addPost(post) {
try {
let data = post.data;
let nsfw = document.getElementById('nsfw').value;
switch (nsfw) {
case "No":
if (data.over_18) return;
break;
case "Only":
if (!data.over_18) return;
break;
}
let url = htmlDecodeString(data.url);
if (urls[url]) return;
urls[url] = true;
console.debug(post);
let article = document.createElement('article');
article.id = data.id;
article.className = data.over_18 ? 'nsfw' : 'sfw';
if (data.is_self) article.className += ' self';
article.innerHTML += `
<header>
<h2>${data.over_18 ? '[NSFW] ' : ''}<a target='_blank' href='${!data.is_self && data.domain.startsWith('self.') ? domain : ''}${url}'>${data.title}</a></h2> (${data.domain})
</header>
`;
article.innerHTML += `
<footer>
<a href='#/user/${data.author}/submitted/' target='_blank'>${data.author}</a> -- <a href='#/${data.subreddit_name_prefixed}/new/' target='_blank'>${data.subreddit_name_prefixed}</a> ${(new Date(data.created_utc * 1000)).toLocaleString()}
</footer>
`;
let content = document.createElement('div');
content.className = 'content';
if ((data.preview && data.preview.reddit_video_preview) || (data.media && data.media.reddit_video)) {
let vid_url;
if (data.preview.reddit_video_preview) {
vid_url = htmlDecodeString(data.preview.reddit_video_preview.fallback_url);
} else if (data.media.reddit_video) {
vid_url = htmlDecodeString(data.media.reddit_video.fallback_url);
}
let vid = document.createElement('video');
vid.preload = 'none';
vid.src = vid_url;
vid.title = `${data.title} - ${data.subreddit_name_prefixed}`
vid.controls = true;
vid.muted = true;
vid.loop = true;
vid.autoplay = false;
vid.playsInline = true;
vid.poster = data.thumbnail
if (data.preview.images[0]) {
if (data.preview.images[0].resolutions) {
let resolutions = data.preview.images[0].resolutions;
let idx = resolutions.length - 1;
vid.poster = htmlDecodeString(resolutions[idx].url);
vid.width = resolutions[idx].width
vid.height = resolutions[idx].height
}
}
content.appendChild(vid);
} else if (data.preview) {
for (let img of data.preview.images) {
let img_url = htmlDecodeString(img.source.url);
let width = img.source.width;
let height = img.source.height;
let resolutions = img.resolutions;
let isMp4 = false;
if (img.variants && img.variants.mp4) {
isMp4 = true;
resolutions = img.variants.mp4.resolutions;
} else if (img.variants && img.variants.gif) {
resolutions = img.variants.gif.resolutions;
}
if (resolutions) {
let idx = resolutions.length - 1;
img_url = htmlDecodeString(resolutions[idx].url);
width = resolutions[idx].width;
height = resolutions[idx].height;
}
let el;
if (isMp4) {
el = document.createElement('video');
el.preload = 'none';
el.src = img_url;
el.controls = true;
el.muted = true;
el.loop = true;
el.autoplay = false;
el.playsInline = true;
el.poster = htmlDecodeString(img.resolutions[img.resolutions.length - 1].url);
el.loop = true;
} else {
el = document.createElement('img');
el.loading = 'lazy';
el.src = img_url;
}
el.title = `${data.title} - ${data.subreddit_name_prefixed}`;
el.width = width;
el.height = height;
content.appendChild(el);
}
} else if (data.is_gallery && data.media_metadata) {
let container = document.createElement('div');
container.className = 'gallery';
for (let id of Object.keys(data.media_metadata)) {
let img = data.media_metadata[id];
let img_url = htmlDecodeString(img.s.u);
let width = img.s.x;
let height = img.s.y;
let resolutions = img.p;
if (resolutions) {
let idx = resolutions.length - 1;
img_url = htmlDecodeString(resolutions[idx].u);
width = resolutions[idx].x;
height = resolutions[idx].y;
}
let imgEl = document.createElement('img');
imgEl.loading = 'lazy';
imgEl.src = img_url;
imgEl.title = `${data.title} - ${data.subreddit_name_prefixed}`;
imgEl.width = width;
imgEl.height = height;
container.appendChild(imgEl);
}
content.appendChild(container);
} else if (data.media_embed && data.media_embed.content) {
let embed = htmlDecodeElement(data.media_embed.content);
embed.innerHTML = embed.innerText.replace("iframe src", "iframe loading='lazy' src");
embed.firstChild.style.position = 'relative';
embed.firstChild.style.height = '100vmin';
embed.firstChild.style.maxHeight = '100%';
embed.firstChild.loading = 'lazy';
content.appendChild(embed.firstChild);
} else if (data.is_reddit_media_domain || data.url.toLowerCase().endsWith('.jpg') || data.url.toLowerCase().endsWith('.jpeg') || data.url.toLowerCase().endsWith('.gif') || data.url.toLowerCase().endsWith('.gifv') || data.url.toLowerCase().endsWith('.png')) {
let imgEl = document.createElement('img');
imgEl.loading = 'lazy';
imgEl.src = data.url;
imgEl.title = `${data.title} - ${data.subreddit_name_prefixed}`;
content.appendChild(imgEl);
} else {
if (!document.getElementById('selfPosts').checked) return;
content.innerHTML += `<div style='overflow: scroll; white-space: pre-wrap'>${htmlDecodeString(data.selftext)}</div>`;
}
article.appendChild(content);
document.querySelector('main').appendChild(article);
} catch (err) {
console.error("Error adding post", err, post);
}
}
function htmlDecodeElement(str) {
let div = document.createElement('div');
div.innerHTML = str;
return div;
}
function htmlDecodeString(str) {
return htmlDecodeElement(str).innerText;
}
</script>
</body>