tootkomm/index.html

173 lines
4.7 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TootKomm</title>
</head>
<body>
<header>
</header>
<main id="comments">
<link rel="stylesheet" href="fontawesome/css/all.css">
<link rel="stylesheet" href="style.css?v=1.21">
</main>
<footer>
</footer>
<script>
function buildCommentTree(statusID){
const target = document.querySelector('#comments');
buildStatus(target, statusID);
buildDescendants(target, statusID);
}
async function getJSONObject(url){
const requestURL = url;
const request = new Request(requestURL);
const response = await fetch(request);
return await response.json();
}
async function buildArticle(target, mastopost) {
const article = document.createElement('article');
populateHeader(article, mastopost);
populateSection(article, mastopost);
populateAside(article, mastopost);
populateFooter(article, mastopost);
target.appendChild(article);
}
async function buildStatus(target, statusID) {
const url = buildStatusURL(statusID);
const mastopost = await getJSONObject(url);
buildArticle(target, mastopost);
}
async function buildDescendants(target, statusID){
const url = buildStatusURL(statusID);
const contextURL = url+'/context';
const context = await getJSONObject(contextURL);
const sorted_descendants = context.descendants.sort(
function (a, b) {
aDate = new Date(a.created_at).getTime();
bDate = new Date(b.created_at).getTime();
diff = aDate - bDate;
return diff;
});
for (const descendant of sorted_descendants) {
buildArticle(target, descendant);
}
}
function populateHeader(item, obj) {
const myHeader = document.createElement('header');
const myDateTime = document.createElement('time');
const link = document.createElement('a');
link.href = obj.url;
myDateTime.datetime = obj.created_at;
creation_date = new Date(obj.created_at);
myDateTime.textContent = creation_date.toLocaleString('de-DE',{ day: "2-digit", month: "short", year: "numeric", hour:"2-digit", minute:"2-digit"});
if(obj.spoiler_text.length > 0){
const myCW = document.createElement('summary');
const myButton = document.createElement('button');
myButton.addEventListener("click", showHide);
myButton.textContent = "\u{1F648}";
myCW.textContent = obj.spoiler_text;
myButton.classList.toggle("show");
myHeader.appendChild(myCW);
myHeader.appendChild(myButton);
}
myHeader.appendChild(link).appendChild(myDateTime);
item.appendChild(myHeader);
}
function populateSection(item, obj){
const mySection = document.createElement('section');
mySection.innerHTML = `${obj['content']}`;
if(obj.spoiler_text.length > 0){
mySection.style.display="none";
}
item.appendChild(mySection);
}
function populateAside(item, obj){
const aside = document.createElement('aside');
const myImg = document.createElement('img');
myImg.src = obj['account']['avatar'];
myDate = new Date(obj.account['created_at']);
aside.appendChild(myImg);
const myH = document.createElement('hgroup');
const myP = document.createElement('div');
myH.innerHTML = `<h1>${obj.account['display_name']}</h1> <h2>(<a href="${obj.account.url}">${obj.account['username']}</a>)</h2>`;
myP.innerHTML = `<time datetime="${obj.account['created_at']}">since ${myDate.toLocaleDateString('de-DE',{ day: "2-digit", month: "short", year: "numeric"})}</time>`;
aside.appendChild(myH);
aside.appendChild(myP);
item.appendChild(aside);
}
function populateFooter(item, obj){
const footer = document.createElement('footer');
const stats = document.createElement('ul');
stats.innerHTML = `<li class="replies fa-solid fa-retweet">${obj.replies_count}</li><li class="favs fa-solid fa-star">${obj.favourites_count}</li><li class="boosts fa-solid fa-share">${obj.reblogs_count}</li>`;
footer.appendChild(stats);
item.appendChild(footer);
}
function buildStatusURL(statusID) {
const apiURL = 'https://social.tchncs.de/api/v1/statuses/';
return apiURL + statusID;
}
buildCommentTree("107735248507147283");
// buildCommentTree("107724981153178707");
function showHide(){
button = event.srcElement;
article = button.parentNode.parentNode;
section = article.querySelector('section');
if( section.style.display == "none" ){
section.style.display = "block";
button.classList.toggle("hide");
}
else {
section.style.display = "none";
button.classList.toggle("show");
}
}
</script>
</body>
</html>