Skip to content

Commit

Permalink
Add beforeScreenShot option (#425)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
jopemachine and sindresorhus authored Jan 23, 2022
1 parent 9c9d14a commit 3a1af89
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
28 changes: 28 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ Default: `{}`

Options passed to [`puppeteer.launch()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions).

##### beforeScreenshot

Type: `Function`

The specified function is called right before the screenshot is captured, as well as before any bounding rectangle is calculated as part of `options.element`. It receives the Puppeteer [`Page` instance](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-page) as the first argument and the [`browser` instance](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-browser) as the second argument. This gives you a lot of power to do custom stuff. The function can be async.

Note: Make sure to not call `page.close()` or `browser.close()`.

```js
const Pageres = require('pageres');

(async () => {
await new Pageres({
delay: 2,
beforeScreenshot: async (page, browser) => {
await checkSomething();
await page.click('#activate-button');
await page.waitForSelector('.finished');
}
})
.src('https://github.com/sindresorhus/pageres', ['480x320', '1024x768', 'iphone 5s'], {crop: true})
.dest(__dirname)
.run();

console.log('Finished generating screenshots!');
})();
```

### pageres.src(url, sizes, options?)

Add a page to screenshot.
Expand Down
32 changes: 31 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {promisify} from 'util';
import {parse as parseUrl} from 'url'; // eslint-disable-line node/no-deprecated-api
import type {BeforeScreenshot} from 'capture-website';
import path = require('path');
import fs = require('fs');
import os = require('os');
Expand Down Expand Up @@ -162,6 +163,34 @@ export interface Options {
@default {}
*/
readonly launchOptions?: captureWebsite.Options['launchOptions'];

/**
The specified function is called right before the screenshot is captured. It receives the Puppeteer [`Page` instance](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-page) as the first argument and the [`browser` instance](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#class-browser) as the second argument. This gives you a lot of power to do custom stuff. The function can be async.
Note: Make sure to not call `page.close()` or `browser.close()`.
@example
```
const Pageres = require('pageres');
(async () => {
await new Pageres({
delay: 2,
beforeScreenshot: async (page, browser) => {
await checkSomething();
await page.click('#activate-button');
await page.waitForSelector('.finished');
}
})
.src('https://github.com/sindresorhus/pageres', ['480x320', '1024x768', 'iphone 5s'], {crop: true})
.dest(__dirname)
.run();
console.log('Finished generating screenshots!');
})();
```
*/
readonly beforeScreenShot?: BeforeScreenshot;
}

/**
Expand Down Expand Up @@ -495,7 +524,8 @@ export default class Pageres extends EventEmitter {
userAgent: options.userAgent,
headers: options.headers,
darkMode: options.darkMode,
launchOptions: options.launchOptions
launchOptions: options.launchOptions,
beforeScreenShot: options.beforeScreenShot
};

if (options.username && options.password) {
Expand Down

0 comments on commit 3a1af89

Please sign in to comment.