class Fedicomment {
container;
postid;
mastodonurl;
constructor() {
this.container = document.getElementById("fedicomment-container");
if (this.container == null) {
console.error("Fedicomment: could not find fedicomment container");
return;
}
this.postid = this.container.dataset.postid;
if (typeof this.postid != "string") {
console.error("Fedicomment: expected postid");
let errorContainer = document.createElement("div");
errorContainer.innerHTML = "Failed to initialize Fedicomment (see the console for details)";
errorContainer.classList.add("fedicomment-alert", "fedicomment-error");
this.container.replaceChildren(errorContainer);
return;
}
this.mastodonurl = this.container.dataset.mastodonurl;
if (typeof this.mastodonurl != "string") {
console.error("Fedicomment: expected mastodonurl");
let errorContainer = document.createElement("div");
errorContainer.innerHTML = "Failed to initialize Fedicomment (see the console for details)";
errorContainer.classList.add("fedicomment-alert", "fedicomment-error");
this.container.replaceChildren(errorContainer);
return;
}
let enableButton = document.createElement("button");
enableButton.innerHTML = "Enable Fedicomment";
enableButton.id = "fedicomment-button";
enableButton.onclick = function() {
fedicomment.enable();
}
this.container.replaceChildren(enableButton);
console.log("Fedicomment successfully initialized");
}
enable() {
fetch(`${this.mastodonurl}/api/v1/statuses/${this.postid}`)
.then((resp) => {
if (resp.ok) {
resp.json().then((data) => {
this.container.innerHTML = `Interact with this post
Copy and paste this URL in your Mastodon application: ${data.url}