Add top posts functionality
This commit is contained in:
parent
2c2a275a95
commit
3ced4c6885
3 changed files with 51 additions and 3 deletions
40
static/top_posts.js
Normal file
40
static/top_posts.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
(async function () {
|
||||
console.log("Starting top_posts");
|
||||
const API = window.UPVOTE_API;
|
||||
const NUM_BEST = window.NUM_BEST;
|
||||
|
||||
const res = await fetch(`${API}/posts/best?top=${NUM_BEST}`, {
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
const URL = window.location.origin;
|
||||
const ul = document.getElementById("top-posts");
|
||||
for (post of data) {
|
||||
const postUrl = post.slug.replace("post-", "");
|
||||
const pageUrl = `/posts/${postUrl}`;
|
||||
|
||||
try {
|
||||
const pageResponse = await fetch(pageUrl);
|
||||
const html = await pageResponse.text();
|
||||
const doc = new DOMParser().parseFromString(html, "text/html");
|
||||
const title =
|
||||
doc.querySelector('meta[property="og:title"]')?.content ||
|
||||
doc.title ||
|
||||
slug;
|
||||
|
||||
const li = document.createElement("li");
|
||||
|
||||
li.innerHTML = `
|
||||
<a href="/posts/${postUrl}">
|
||||
${title}
|
||||
|
||||
</a>
|
||||
`;
|
||||
ul.appendChild(li);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue