1
0
mirror of https://github.com/jiahaog/Nativefier synced 2024-06-26 07:38:59 +02:00
Nativefier/src/infer/inferTitle.ts
Ronan Jouchet b0a953eb2d Get rid of cheerio
Not sure this one will stick, maybe my regex is too naive.
Works for common websites. Let's see
2021-01-15 22:15:48 -05:00

21 lines
698 B
TypeScript

import axios from 'axios';
import * as log from 'loglevel';
const USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36';
export async function inferTitle(url: string): Promise<string> {
const { data } = await axios.get(url, {
headers: {
// Fake user agent for pages like http://messenger.com
'User-Agent': USER_AGENT,
},
});
log.debug(`Fetched ${(data.length / 1024).toFixed(1)} kb page at`, url);
const inferredTitle =
/<\s*title.*?>(?<title>.+?)<\s*\/title\s*?>/i.exec(data).groups?.title ||
'Webapp';
log.debug('Inferred title:', inferredTitle);
return inferredTitle;
}