possumbot/modules/fedimbed.js
2024-08-25 18:37:24 +00:00

81 lines
2.4 KiB
JavaScript

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("<!DOCTYPE html><body></body>");
const document = dom.window.document;
var quote = document.createElement("blockquote");
if(!embed.embeds) {
var c = util.format(embed);
client.reply(event, c, `<pre><code>${encode(c)}</code></pre>`);
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) ? "&nbsp" : "") + 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 = file.desc;
quote.appendChild(media);
}
document.body.appendChild(quote);
let x = document.createElement("small");
x.innerHTML = "Powered by <a href=\"https://gitdab.com/Cynosphere/HiddenPhox/src/branch/rewrite/src/modules/fedimbed.js\">HF Fedimbed</a>";
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
}
}