Skip to content

Instantly share code, notes, and snippets.

@joelgriffith
Last active November 22, 2020 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joelgriffith/a8fb2d3ee34aff30ecbfcaaffac2d2c5 to your computer and use it in GitHub Desktop.
Save joelgriffith/a8fb2d3ee34aff30ecbfcaaffac2d2c5 to your computer and use it in GitHub Desktop.
Unfurls a link into semantic data
import puppeteer from 'puppeteer';
function getTitle() {
if (document.querySelector('meta[property="og:title"]')) {
return document.querySelector('meta[property="og:title"]').content;
}
if (document.querySelector('[itemprop="name"]')) {
return document.querySelector('[itemprop="name"]').text;
}
if (document.querySelector('title')) {
return document.querySelector('title').text;
}
return window.location.href; // Print URL as a fallback
}
function getDescription() {
if (document.querySelector('meta[property="og:description"]')) {
return document.querySelector('meta[property="og:description"]').content;
}
if (document.querySelector('[itemprop="description"]')) {
return document.querySelector('[itemprop="description"]').text;
}
if (document.querySelector('meta[name="description"]')) {
return document.querySelector('meta[name="description"]').content;
}
return document.body.innerText.substring(0, 180) + '...';
}
function getImage() {
if (document.querySelector('meta[property="og:image"]')) {
return document.querySelector('meta[property="og:image"]').content;
}
if (document.querySelector('[itemprop="image"]')) {
return document.querySelector('[itemprop="image"]').text;
}
return null;
}
export async function unfurl(link) {
const browser = await puppeteer.connect({
browserWSEndpoint: 'wss://chrome.browserless.io?token=YOUR_API_TOKEN'
});
const page = await browser.newPage();
await page.goto(link);
const title = await page.evaluate(getTitle);
const description = await page.evaluate(getDescription);
const image = await page.evaluate(getImage) || await page.screenshot({ path: 'temp.png' });
browser.close();
return {
title,
description,
image,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment