bashtube/webroot/js/watch.js

43 lines
949 B
JavaScript

const comments = document.querySelector(".comments");
const id = new URLSearchParams(document.location.search).get("v");
function showMore(e) {
if (e.target.classList.contains("show-more")) {
e.target.classList.remove("show-more");
if(e.target.dataset.fun == "replies") {
e.target.classList.add("comment-thread");
}
fetchComments(e.target, e.target.dataset.uwu);
}
}
function fetchComments(div, cont) {
div.innerText = "Loading...";
if(!cont) {
url = "/comments.shs?v=" + id;
} else {
url = "/comments.shs?cont=" + cont;
}
fetch(url).then(
res => {
if (res.status !== 200) {
throw new Error('HTTP Error ' + res.status);
}
return res.text();
}
).then(
res => {
div.innerHTML = res;
}
).catch(
err => div.innerText = 'Error during fetching comments: ' + err.message
)
}
window.addEventListener("load", () => {
fetchComments(comments);
})
window.addEventListener("click", (e) => showMore(e));