"use strict"; //! {"category": "libraries", "require": ["libhttp", "libxml"], "permissions": ["network"]} let libxml = require("libxml"); let libhttp = require("libhttp"); let gEmbedIndex = 0; let gCache = {}; async function unfurl(url) { let result = {href: url}; let response = await libhttp.get(url); let parsed = libxml.StreamParser().parse(response.body); let oEmbedUrl; for (let node of parsed) { if (node.type == "element" && node.value == "link" && node.attributes.type == "application/json+oembed") { oEmbedUrl = node.attributes.href; break; } } if (oEmbedUrl) { response = await libhttp.get(oEmbedUrl); let oEmbed = JSON.parse(response.body); gEmbedIndex++; result = [{href: url}, "\n", { name: "oEmbed" + gEmbedIndex, iframe: ` ${oEmbed.html}`, width: oEmbed.width || 320, height: oEmbed.height || 120, style: "margin: 0; padding: 0; border: 0; overflow: hidden", }]; } return result; } async function test() { terminal.print(await unfurl("https://twitter.com/511nyAlbany/status/777221230915096576")); terminal.print(await unfurl("https://www.youtube.com/watch?v=pTA0DSfrGZ0")); } //test().catch(terminal.print); core.register("onMessage", async function(sender, message) { let result = message; if (gCache[message] && new Date().valueOf() < gCache[message].expires) { result = gCache[message].result; } else { try { result = await unfurl(message); } catch (error) { result = [{href: message}, " (error retrieving: ", error, ")"]; } gCache[message] = { expires: new Date().valueOf() + 7 * 24 * 60 * 60 * 1000, result: result, }; } return result; });