import fedimbed from "../lib/fedimbed.js"; import { encode } from "html-entities"; import { JSDOM } from "jsdom"; import util from "util"; async function onMessage(client, event) { const embed = await fedimbed(event.getContent().body); if(!embed) return; const dom = new JSDOM(""); const document = dom.window.document; var quote = document.createElement("blockquote"); if(!embed.embeds) { var c = util.format(embed); client.reply(event, c, `
${encode(c)}
`); return; } for(const emb of embed.embeds) { var link = document.createElement("a"); link.href = emb.url; if(emb.thumbnail && emb.thumbnail.url) { var avatar = document.createElement("img"); avatar.src = await client.uploadMedia(emb.thumbnail.url); avatar.height = "16"; link.appendChild(avatar); } var linkText = document.createElement("span"); linkText.innerHTML = ((emb.thumbnail?.url) ? " " : "") + emb.title; link.appendChild(linkText); quote.appendChild(link); var text = document.createElement("p"); text.innerHTML = emb.description; for(const emote of embed.emotes) { console.log(text.innerHTML); var img = document.createElement("img"); img.src = await client.uploadMedia(emote.url); img.height = "16"; img.alt = emote.name; text.innerHTML = text.innerHTML.replaceAll(emote.name, img.outerHTML); } quote.appendChild(text); } for(const file of embed.files) { var media; switch(file.type.split("/")[0]) { case "audio": media = document.createElement("audio"); break; case "video": media = document.createElement("video"); break; case "image": media = document.createElement("img"); break; } media.src = await client.uploadMedia(file.url); media.alt = media.title = file.desc ?? ""; quote.appendChild(media); } document.body.appendChild(quote); let x = document.createElement("small"); x.innerHTML = "Powered by HF Fedimbed"; document.body.appendChild(x); client.reply(event, "This message uses HTML, which your client does not support.", document.body.outerHTML); } export default { name: "fedimbed", desc: "embed fediverse post contents", hooks: { message: onMessage } }