Skip to content

Commit

Permalink
Add preloadFunction option (#76)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
brandon93s and sindresorhus authored Feb 16, 2021
1 parent c4d7920 commit 9f3e274
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
9 changes: 8 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference lib="dom"/>
import {SetCookie, LaunchOptions, Page, Browser, Headers} from 'puppeteer';
import {SetCookie, LaunchOptions, Page, Browser, Headers, EvaluateFn} from 'puppeteer';

declare namespace captureWebsite {
interface Authentication {
Expand Down Expand Up @@ -164,6 +164,13 @@ declare namespace captureWebsite {
*/
readonly isJavaScriptEnabled?: boolean;

/**
Inject a function to be executed prior to navigation.
This can be useful for [altering the JavaScript environment](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pageevaluateonnewdocumentpagefunction-args). For example, you could define a global method on the `window`, overwrite `navigator.languages` to change the language presented by the browser, or mock `Math.random` to return a fixed value.
*/
readonly preloadFunction?: EvaluateFn;

/**
Inject [JavaScript modules](https://developers.google.com/web/fundamentals/primers/modules) into the page.
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ const captureWebsite = async (input, options) => {
const browser = options._browser || await puppeteer.launch(launchOptions);
const page = await browser.newPage();

if (options.preloadFunction) {
await page.evaluateOnNewDocument(options.preloadFunction);
}

await page.setJavaScriptEnabled(options.isJavaScriptEnabled);

if (options.debug) {
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,15 @@ Overwrite the destination file if it exists instead of throwing an error.

*This option applies only to `captureWebsite.file()`.*

##### preloadFunction

Type: `string | Function`\
Default: `undefined`

Inject a function to be executed prior to navigation.

This can be useful for [altering the JavaScript environment](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pageevaluateonnewdocumentpagefunction-args). For example, you could define a global method on the `window`, overwrite `navigator.languages` to change the language presented by the browser, or mock `Math.random` to return a fixed value.

### captureWebsite.devices

Type: `string[]`
Expand Down
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global document, window */
import fs from 'fs';
import test from 'ava';
import imageSize from 'image-size';
Expand Down Expand Up @@ -852,3 +853,34 @@ test('`inset` option', async t => {
});
});
});

test('`preloadFunction` option', async t => {
const server = await createTestServer();

server.get('/', async (request, response) => {
response.end(`
<body style="margin: 0;">
<div style="background-color: black; width: 100px; height: 100px;"></div>
<script>
window.toRed();
</script>
</body>
`);
});

const pixels = await getPngPixels(await instance(server.url, {
width: 100,
height: 100,
preloadFunction: () => {
window.toRed = () => {
document.querySelector('div').style.backgroundColor = 'red';
};
}
}));

t.is(pixels[0], 255);
t.is(pixels[1], 0);
t.is(pixels[2], 0);

await server.close();
});

0 comments on commit 9f3e274

Please sign in to comment.