Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ type: `string` | `string[]`</br>

Click the DOM element matching the given [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).

##### colorScheme

type: `string`</br>
default: `'no-preference'`

Sets [prefers-color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme) CSS media feature, used to detect if the user has requested the system use a `'light'` or `'dark'` color theme.

##### device

type: `string`</br>
Expand Down Expand Up @@ -654,12 +661,11 @@ const buffer = await browserless.screenshot(url.toString(), {
})
```

##### colorScheme
##### onPageRequest

type: `string`</br>
default: `'no-preference'`
type:`function`

Sets [prefers-color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme) CSS media feature, used to detect if the user has requested the system use a `'light'` or `'dark'` color theme.
Associate a handler for every request in the page.

##### scripts

Expand Down
33 changes: 21 additions & 12 deletions packages/goto/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ module.exports = ({
waitForXPath,
waitUntil = 'auto',
waitUntilAuto = _waitUntilAuto,
onPageRequest,
...args
}
) => {
Expand Down Expand Up @@ -244,19 +245,27 @@ module.exports = ({
)
}

const enableInterception =
(onPageRequest || abortTypes.length > 0) && page.setRequestInterception(true)

if (onPageRequest) {
Promise.resolve(enableInterception).then(() => page.on('request', onPageRequest))
}

if (abortTypes.length > 0) {
await page.setRequestInterception(true)
page.on('request', req => {
if (req.isInterceptResolutionHandled()) return
const resourceType = req.resourceType()
const url = truncate(req.url())

if (!abortTypes.includes(resourceType)) {
debug('continue', { url, resourceType })
return req.continue(req.continueRequestOverrides(), 0)
}
debug('abort', { url, resourceType })
return req.abort('blockedbyclient', 0)
Promise.resolve(enableInterception).then(() => {
page.on('request', req => {
if (req.isInterceptResolutionHandled()) return
const resourceType = req.resourceType()
const url = truncate(req.url())

if (!abortTypes.includes(resourceType)) {
debug('continue', { url, resourceType })
return req.continue(req.continueRequestOverrides(), 0)
}
debug('abort', { url, resourceType })
return req.abort('blockedbyclient', 0)
})
})
}

Expand Down