Update top posts and upvote to pass/get title
This commit is contained in:
parent
92ea370e40
commit
9a66205d80
2 changed files with 8 additions and 15 deletions
|
|
@ -14,15 +14,7 @@
|
||||||
for (post of data) {
|
for (post of data) {
|
||||||
const postUrl = post.slug.replace("posts-", "");
|
const postUrl = post.slug.replace("posts-", "");
|
||||||
const pageUrl = `/posts/${postUrl}`;
|
const pageUrl = `/posts/${postUrl}`;
|
||||||
|
const title = post.title;
|
||||||
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");
|
const li = document.createElement("li");
|
||||||
|
|
||||||
|
|
@ -33,8 +25,5 @@
|
||||||
</a>
|
</a>
|
||||||
`;
|
`;
|
||||||
ul.appendChild(li);
|
ul.appendChild(li);
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,12 @@
|
||||||
const alreadyVoted = btn.hasAttribute("voted");
|
const alreadyVoted = btn.hasAttribute("voted");
|
||||||
|
|
||||||
const method = alreadyVoted ? "DELETE" : "POST";
|
const method = alreadyVoted ? "DELETE" : "POST";
|
||||||
|
|
||||||
const r = await fetch(`${API}/posts/${slug}/vote`, {
|
const r = await fetch(`${API}/posts/${slug}/vote`, {
|
||||||
method,
|
method,
|
||||||
|
body: JSON.stringify({
|
||||||
|
title: document.querySelector('meta[name="title"]').content,
|
||||||
|
}),
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue