Skip to content

Commit

Permalink
add timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed May 30, 2023
1 parent 9215fc1 commit 9b1a66e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ docker-compose up
| `input` | `string` | either a URL to navigate to or HTML to be rendered | (required) |
| `screenshotMode` | `'normal' \| 'full' \| 'element'` | - `normal`: takes a screenshot of the contents visible in the viewport (default if `input` is a url)<br/>- `full`: includes the entire page in the screenshot<br/>- `element`: mainly for when the input is html, only includes the html content and no empty space. probably won't work properly for most websites (default if `input` is html) | (depends, see description) |
| `blockAds` | `boolean` | whether to block ads using [`@cliqz/adblocker-playwright`](https://www.npmjs.com/package/@cliqz/adblocker-playwright) | `true` |
| `timeout` | `number` | the number of milliseconds to wait for the site to load before timing out | `60000` |
8 changes: 6 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Options {
input: string
screenshotMode?: 'normal' | 'full' | 'element'
blockAds?: boolean
timeout?: number
}

app.use(boolParser())
Expand All @@ -37,14 +38,17 @@ app.get('/', async (req, res) => {
}
})

const takeScreenshot = async ({ input, screenshotMode, blockAds }: Options) => {
const takeScreenshot = async ({ input, screenshotMode, blockAds, timeout }: Options) => {
const inputType: 'url' | 'html' = input.startsWith('<') ? 'html' : 'url'
if (screenshotMode === undefined) {
screenshotMode = inputType === 'url' ? 'normal' : 'element'
}
if (blockAds === undefined) {
blockAds = true
}
if (timeout === undefined) {
timeout = 60000
}
if (inputType === 'url' && screenshotMode === 'element') {
input = `<span>${input}</span>`
}
Expand All @@ -68,7 +72,7 @@ const takeScreenshot = async ({ input, screenshotMode, blockAds }: Options) => {
])
).enableBlockingInPage(page)
page.on('dialog', (dialog) => void dialog.dismiss())
const options = { waitUntil: 'networkidle' } as const
const options = { waitUntil: 'networkidle', timeout } as const
await (inputType === 'url'
? page.goto(normalizedInput, options)
: page.setContent(normalizedInput, options))
Expand Down

0 comments on commit 9b1a66e

Please sign in to comment.