-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
226 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,47 @@ | ||
import chromium from '@sparticuz/chromium'; | ||
import { NextApiResponse } from 'next'; | ||
import puppeteer from 'puppeteer-core'; | ||
|
||
import { createApiHandler, respond } from '@chaindesk/lib/createa-api-handler'; | ||
import { getTextFromHTML } from '@chaindesk/lib/loaders/web-page'; | ||
import { AppNextApiRequest } from '@chaindesk/lib/types'; | ||
|
||
const handler = createApiHandler(); | ||
|
||
const LOCAL_CHROME_EXECUTABLE = '/opt/homebrew/bin/chromium'; | ||
|
||
export const browser = async (req: AppNextApiRequest, res: NextApiResponse) => { | ||
const url = req.query.url as string; | ||
|
||
const customUserAgent = | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'; | ||
|
||
const browser = await puppeteer.launch({ | ||
executablePath: await chromium.executablePath(), | ||
args: chromium.args, | ||
headless: true, | ||
}); | ||
|
||
const page = await browser.newPage(); | ||
await page.setUserAgent(customUserAgent); | ||
await page.goto(url, { waitUntil: 'networkidle0', timeout: 100000 }); | ||
|
||
let content = await page.content(); | ||
let text = (await getTextFromHTML(content))?.trim(); | ||
|
||
if (!text) { | ||
console.log("not text parssed from html, let's try again after 10 seconds"); | ||
await page.waitForTimeout(10000); | ||
} | ||
|
||
const result = await page.content(); | ||
|
||
return { | ||
html: result, | ||
text, | ||
}; | ||
}; | ||
|
||
handler.get(respond(browser)); | ||
|
||
export default handler; |
Oops, something went wrong.
3d2415e
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:
browser – ./apps/dashboard
browser-git-main-databerry.vercel.app
browser-databerry.vercel.app
browser-blue.vercel.app