el.xwx.moe/lib/shared/getTitle.ts

23 lines
586 B
TypeScript
Raw Normal View History

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