el.xwx.moe/lib/shared/getTitle.ts
2023-12-31 10:46:09 -05:00

23 lines
586 B
TypeScript

import fetch from "node-fetch";
import https from "https";
export default async function getTitle(url: string) {
try {
const httpsAgent = new https.Agent({
rejectUnauthorized:
process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true,
});
const response = await fetch(url, {
agent: httpsAgent,
});
const text = await response.text();
// regular expression to find the <title> tag
let match = text.match(/<title.*>([^<]*)<\/title>/);
if (match) return match[1];
else return "";
} catch (err) {
console.log(err);
}
}