irc_bot/components/UrlTitle.js

30 lines
830 B
JavaScript

import axios from "axios";
import Color from "./Color.js";
const getTitle = async (url) => {
const r = await axios({ method: "get", url });
const doc = r.data;
return doc.split("<title>")[1].split("</title>")[0];
};
const UrlTitle = (event) => {
const geturl = new RegExp(
"(^|[ \t\r\n])((ftp|http|https|gopher|gemini|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))",
"g"
);
let msg = event.message;
if (msg && msg.match(geturl)) {
let urls = msg.match(geturl);
urls.map(async (url) => {
let title = await getTitle(url.trim());
return event.reply(Color("Title") + " " + title);
});
} else {
return false;
}
};
export default UrlTitle;