possumbot/modules/fedimbed.js

82 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2024-08-21 17:00:40 +02:00
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);
2024-08-21 17:00:40 +02:00
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;
}
2024-08-21 17:00:40 +02:00
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");
2024-08-22 17:34:03 +02:00
avatar.src = await client.uploadMedia(emb.thumbnail.url);
2024-08-21 17:00:40 +02:00
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;
2024-08-22 17:34:03 +02:00
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);
}
2024-08-21 17:00:40 +02:00
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;
}
2024-08-22 17:34:03 +02:00
media.src = await client.uploadMedia(file.url);
2024-08-27 17:30:14 +02:00
media.alt = media.title = file.desc ?? "";
2024-08-21 17:00:40 +02:00
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
}
}