url-metadata
Version:
Request a url and scrape the metadata from its HTML using Node.js or the browser.
47 lines (41 loc) • 1.06 kB
text/typescript
import urlMetadata from 'url-metadata';
(async function () {
console.log('-- running 2 tests ---')
try {
const metadata = await urlMetadata('./metadata.html', {
mode: 'same-origin',
includeResponseBody: true
});
console.log('2/ fetch local metadata.html:', metadata);
} catch(err) {
console.log(err);
}
})();
(async function () {
try {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Metadata page</title>
<meta name="author" content="foobar">
<meta name="keywords" content="HTML, CSS, JavaScript">
</head>
<body>
<h1>Metadata page</h1>
</body>
</html>
`
const response = new Response(html, {
headers: {
'Content-Type': 'text/html'
}
})
// pass null `url` param & response object as option
const metadata = await urlMetadata(null, { parseResponseObject: response })
console.log('1/ html string:', metadata);
} catch(err) {
console.log(err);
}
})();