/* globals MastodonAPI */ function fetch_mastodon_api_comments(instance, status_id) { const root = document.getElementById("mastodon-api-comments_" + status_id); let api = new MastodonAPI( { instance: "https://" + instance, api_user_token: "" }); // Get the URL of the status and write the intro. api.get("statuses/" + status_id, function(data) { root.appendChild(comments_intro(data.url)); }); // Get all the replies to the status and write them to the page. api.get("statuses/" + status_id + "/context", function(data) { write_comments(root, data); }); } function comments_intro(url) { const p = document.createElement("p"); const a = document.createElement("a"); p.appendChild(document.createTextNode("You can ")); p.setAttribute("class", "mastodon-api-comment-intro"); a.setAttribute("href", url); a.appendChild( document.createTextNode("comment on this post in the Fediverse")); p.appendChild(a); p.appendChild(document.createTextNode(".")); return p; } function write_comments(root, data) { for (const status of data.descendants) { let content = status.content; for (const emoji of status.emojis) { content = content.replace( ':' + emoji.shortcode + ':', ''); } const p = document.createElement("p"); p.setAttribute("class", "mastodon-api-comment"); p.appendChild(author_html(status.account)); p.innerHTML += content; root.appendChild(p); } } function author_html(account) { const p = document.createElement("p"); p.setAttribute("class", "mastodon-api-comment-author"); const strong = document.createElement("strong"); strong.appendChild(document.createTextNode(account.display_name)); p.appendChild(strong); p.appendChild(document.createTextNode(" (" + account.acct + ") wrote:")); return p; }