Skip to content

Commit

Permalink
Merge pull request #14 from choraria/dev
Browse files Browse the repository at this point in the history
adding meta.js
  • Loading branch information
choraria authored Oct 9, 2021
2 parents f434014 + 066ae78 commit ca23064
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
35 changes: 34 additions & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ a free and open-source api that runs [puppeteer](https://developers.google.com/w
2. [metrics](#metrics)
3. [trace](#trace)
4. [pdf](#pdf)
5. [version](#version)
5. [meta](#meta)
6. [version](#version)
- [contributing](#contributing)
- [credits](#credits)
- [license](#license)
Expand Down Expand Up @@ -106,6 +107,38 @@ View the trace in [timeline-viewer](https://github.com/ChromeDevTools/timeline-v
- api: `https://pptr.io/api/pdf?url=https://netflix.com`
- source: [pdf.js](/api/pdf.js)

### meta

- task: fetch meta tag data from a website
- method: `GET`
- api: `https://pptr.io/api/meta?url=https://vercel.com`
- source: [meta.js](/api/meta.js)

<details>
<summary>sample output of the meta endpoint</summary>

```json
{
"charset": "utf-8",
"viewport": "width=device-width, initial-scale=1.0",
"Content-Language": "en",
"twitter:card": "summary_large_image",
"twitter:site": "@vercel",
"twitter:image": "https://assets.vercel.com/image/upload/q_auto/front/vercel/dps.png",
"og:title": "Develop. Preview. Ship. For the best frontend teams – Vercel",
"og:url": "https://vercel.com/",
"description": "Deploy web projects with the best frontend developer experience and highest end-user performance.",
"og:description": "Deploy web projects with the best frontend developer experience and highest end-user performance.",
"og:image": "https://assets.vercel.com/image/upload/q_auto/front/vercel/dps.png",
"apple-mobile-web-app-title": "Vercel",
"theme-color": "#000",
"msapplication-TileColor": "#000000",
"next-head-count": "35"
}
```

</details>

### version

- task: fetch browser user agent / chromium version
Expand Down
50 changes: 50 additions & 0 deletions api/meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const puppeteer = require("puppeteer-core");
const chrome = require("chrome-aws-lambda");

module.exports = async (req, res) => {
try {
let url;
try {
url = new URL({ toString: () => req.query.url });
} catch (e) {
res.statusCode = 400;
res.json({
error: "Invalid URL",
});
}

const browser = await puppeteer.launch({
args: [...chrome.args, "--hide-scrollbars", "--disable-web-security"],
defaultViewport: chrome.defaultViewport,
executablePath: await chrome.executablePath,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();

await page.goto(url, {
// waitUntil: "networkidle0",
});

const metaData = await page.evaluate(() => {
const data = {};
const metaTags = document.querySelectorAll('meta');
metaTags.forEach((tag, i) => {
const key = tag.getAttribute('name') ? tag.getAttribute('name') : (tag.getAttribute('property') ? tag.getAttribute('property') : (tag.getAttribute('http-equiv') ? tag.getAttribute('http-equiv') : tag.getAttribute('itemprop')));
tag.getAttribute('charset') ? data["charset"] = tag.getAttribute('charset') : data[key == null ? i : key] = tag.getAttribute('content');
});
return data;
});

await browser.close();

res.statusCode = 200;
res.setHeader("Content-Type", `application/json`);
res.end(JSON.stringify(metaData));
} catch (err) {
console.log(err);
res.statusCode = 500;
res.json({
error: err.toString(),
});
}
};
2 changes: 1 addition & 1 deletion api/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = async (req, res) => {
const page = await browser.newPage();

await page.goto(url, {
waitUntil: "networkidle0",
// waitUntil: "networkidle0",
});
const metrics = await page.metrics();
await browser.close();
Expand Down

1 comment on commit ca23064

@vercel
Copy link

@vercel vercel bot commented on ca23064 Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.