uiharu/extract.mjs

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-10-27 22:25:17 +00:00
import * as cheerio from 'cheerio';
const readStdIn = () => {
return new Promise((resolve, reject) => {
let stdIn = '';
process.stdin.on('data', data => stdIn += data);
process.stdin.on('end', () => resolve(stdIn));
process.stdin.on('error', err => reject(err));
});
};
const $ = cheerio.load(await readStdIn());
const info = {
title: '',
metaTitle: '',
desc: '',
siteName: '',
image: '',
colour: '',
type: 'website',
};
const titleTag = $('title').first();
if(titleTag.length > 0)
info.title = titleTag.text().trim();
const metaTags = $('meta');
for(const elemInfo of metaTags) {
const elem = $(elemInfo);
const nameAttr = (elem.attr('name') ?? elem.attr('property') ?? '').trim();
if(nameAttr === '')
continue;
const valueAttr = (elem.attr('value') ?? elem.attr('content') ?? '').trim();
switch(nameAttr) {
case 'og:title':
case 'twitter:title':
if(info.metaTitle === '' || valueAttr.length > info.metaTitle.length)
info.metaTitle = valueAttr;
break;
case 'description':
case 'og:description':
case 'twitter:description':
if(info.desc === '' || valueAttr.length > info.desc.length)
info.desc = valueAttr;
break;
case 'og:site_name':
if(info.siteName === '')
info.siteName = valueAttr;
break;
case 'og:image':
case 'twitter:image':
if(info.image === '')
info.image = valueAttr;
break;
case 'theme-color':
if(info.colour === '')
info.colour = valueAttr;
break;
case 'og:type':
if(info.type === '')
info.type = `website:${valueAttr}`;
break;
}
}
console.log(JSON.stringify(info));