-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplereddit.js
199 lines (190 loc) Β· 6.02 KB
/
simplereddit.js
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
const CSP =
"default-src 'none'; img-src https://simplereddit.ethan.link/favicon.ico";
const ITEMS_LIMIT = 20;
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) =>
new Response(err.stack, {
status: 500,
headers: {
"Content-Security-Policy": CSP,
},
})
)
);
});
async function handleRequest(request) {
const url = new URL(request.url);
const pathname = url.pathname;
// Favicon
if (pathname === "/favicon.ico")
return fetch("https://files.ethan.link/simplereddit.ico");
// Robots.txt
if (pathname === "/robots.txt")
return new Response("User-agent: *\nDisallow: /");
// Subreddit pages
if (pathname.startsWith("/r/")) return subredditPage(request, url);
// Home page
if (pathname === "/") return homePage();
// Home page form submissions
if (pathname === "/form") {
return Response.redirect(
`${url.origin}/r/${url.searchParams.get("subreddit")}`,
301
);
}
// 404 catch-all
return notFoundPage();
}
function homePage() {
const html = `
<!DOCTYPE html>
<head>
<meta name="viewport" content= "width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="light dark">
<title>SimpleReddit</title>
</head>
<body>
<center>
<br><br><br>
<h1>SimpleReddit</h1>
<p>No distractions. No comments sections. No stylesheets. No JavaScript.</p>
<p>Just an HTML-only Reddit client to stop you entering an internet time vortex.</p>
<br><br>
<form action="/form" method="get">
r/<input name="subreddit" placeholder="Enter a subreddit name" autofocus required />
<button>Go to subreddit</button>
</form>
<br><br>
<p><small>Made by <a href="https://ethan.link">Ethan</a>, source code on <a href="https://github.com/Booligoosh/SimpleReddit">GitHub</a></small></p>
<p><small><small>β <a href="https://www.buymeacoffee.com/Booligoosh">Buy Me A Coffee</a> (tip jar)</small></small></p>
</center>
</body>`;
return new Response(html, {
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-cache",
"Content-Security-Policy": CSP,
},
});
}
async function subredditPage(request, url) {
const subreddit = url.pathname.split("/")[2];
const { data } = await fetch(
`https://www.reddit.com/r/${subreddit}.json`
).then((r) => r.json());
if (!data) return notFoundPage();
const name = data.children[0].data.subreddit_name_prefixed;
// Redirect to nicely capitalised version, without any trailing bits
if (url.pathname !== `/${name}`) {
return Response.redirect(`${url.origin}/${name}`, 302);
}
const html = `
<!DOCTYPE html>
<head>
<meta name="viewport" content= "width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="light dark">
<title>${name} • SimpleReddit</title>
</head>
<body>
<small><a href="/">β Homepage</a></small>
<br>
<br>
<big><big><big>
<b>${name}</b>
</big></big></big>
<br>
${formatCount(data.children[0].data.subreddit_subscribers)} members
<br><br>
<hr/>
${data.children
.filter(({ data }) => !data.title?.trim()?.endsWith("?"))
.slice(0, ITEMS_LIMIT)
.map(
({ data }) => `
<strong>
${
!data.is_self && !data.crosspost_parent_list?.[0]?.is_self
? `<a href="${
data.secure_media?.reddit_video?.fallback_url ?? data.url
}">`
: ""
}
${getTag(data)} ${data.title}
${
!data.is_self && !data.crosspost_parent_list?.[0]?.is_self
? "</a>"
: ""
}
</strong>
<br>
${new Date(data.created_utc * 1000).toLocaleString([], {
month: "short",
weekday: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
timeZone: request.cf?.timezone,
})}
${data.stickied ? "(pinned)" : ""} β’
${data.ups} upvote${data.ups !== 1 ? "s" : ""}
${
data.crosspost_parent
? `<br><em>Crossposted from ${data.crosspost_parent_list?.[0]?.subreddit_name_prefixed}</em>`
: ""
}
${
data.is_self || data.crosspost_parent_list?.[0]?.is_self
? `
<details>
<summary>Self text</summary>
${
(
data.selftext_html ||
data.crosspost_parent_list?.[0]?.selftext_html
)
?.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll("&", "&") || ""
}
</details>
`
: ""
}
<hr/>`
)
.join("")}
Ok, stop reading reddit now and go for a walk :)
</body>`;
return new Response(html, {
headers: {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-cache",
"Content-Security-Policy": CSP,
},
});
}
function notFoundPage() {
return new Response("Not found", {
status: 404,
headers: {
"Content-Security-Policy": CSP,
},
});
}
function getTag(data) {
if (data.is_self || data.crosspost_parent_list?.[0]?.is_self) return "π";
if (data.is_video) return "π½";
if (["i.redd.it", "i.imgur.com"].includes(data.domain)) return "πΈ";
return "π";
// if (data.is_self) return "[self]"
// if (data.is_video) return "[video]"
// if (data.domain === "i.redd.it") return "[image]"
// return "[link]"
}
function formatCount(count) {
if (count >= 1000000) return Math.floor(count / 1000000) + "m";
if (count >= 1000) return Math.floor(count / 1000) + "k";
else return count.toString();
}