-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from choraria/dev
adding meta.js
- Loading branch information
Showing
3 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
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
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
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(), | ||
}); | ||
} | ||
}; |
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
ca23064
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: