Last active
November 22, 2020 14:41
-
-
Save joelgriffith/a8fb2d3ee34aff30ecbfcaaffac2d2c5 to your computer and use it in GitHub Desktop.
Unfurls a link into semantic data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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