Skip to content

Commit

Permalink
Update puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 26, 2022
1 parent 626c1ad commit 06c42d9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 42 deletions.
23 changes: 8 additions & 15 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
import type {Buffer} from 'node:buffer';
import type {
LaunchOptions as PuppeteerLaunchOptions,
BrowserLaunchArgumentOptions,
BrowserConnectOptions,
PuppeteerLaunchOptions,
Page,
Browser,
EvaluateFunc,
Expand All @@ -12,13 +10,6 @@ import type {
BoundingBox,
} from 'puppeteer';

export type LaunchOptions = PuppeteerLaunchOptions &
BrowserLaunchArgumentOptions &
BrowserConnectOptions & {
product?: Product;
extraPrefsFirefox?: Record<string, unknown>;
};

export type Authentication = {
readonly username: string;
readonly password?: string;
Expand Down Expand Up @@ -239,7 +230,7 @@ export type Options = {
/**
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.
This can be useful for [altering the JavaScript environment](https://pptr.dev/api/puppeteer.page.evaluateonnewdocument). 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?: EvaluateFunc<unknown[]>;

Expand Down Expand Up @@ -318,7 +309,7 @@ export type Options = {
readonly userAgent?: string;

/**
Set cookies in [browser string format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) or [object format](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies).
Set cookies in [browser string format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) or [object format](https://pptr.dev/api/puppeteer.page.setcookie).
Tip: Go to the website you want a cookie for and [copy-paste it from DevTools](https://stackoverflow.com/a/24961735/64949).
Expand Down Expand Up @@ -349,7 +340,7 @@ export type Options = {
readonly authentication?: Authentication;

/**
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.
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://pptr.dev/api/puppeteer.page) as the first argument and the [`browser` instance](https://pptr.dev/api/puppeteer.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()`.
Expand Down Expand Up @@ -386,13 +377,13 @@ export type Options = {
readonly darkMode?: boolean;

/**
Options passed to [`puppeteer.launch()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions).
Options passed to [`puppeteer.launch()`](https://pptr.dev/api/puppeteer.puppeteernodelaunchoptions).
Note: Some of the launch options are overridden by the `debug` option.
@default {}
*/
readonly launchOptions?: LaunchOptions;
readonly launchOptions?: PuppeteerLaunchOptions;

/**
Inset the bounding box of the screenshot.
Expand Down Expand Up @@ -488,3 +479,5 @@ declare const captureWebsite: {
};

export default captureWebsite;

export {type PuppeteerLaunchOptions as LaunchOptions} from 'puppeteer';
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import process from 'node:process';
import {promises as fs} from 'node:fs';
import path from 'node:path';
import fileUrl from 'file-url';
import puppeteer from 'puppeteer';
import puppeteer, {KnownDevices} from 'puppeteer';
import toughCookie from 'tough-cookie';
import {PuppeteerBlocker} from '@cliqz/adblocker-puppeteer';
import fetch from 'node-fetch';
import fetch from 'node-fetch'; // TODO: Use the core Fetch method when targeting Node.js 18.

const isUrl = string => /^(https?|file):\/\/|^data:/.test(string);

Expand Down Expand Up @@ -267,11 +267,11 @@ const internalCaptureWebsiteCore = async (input, options, page, browser) => {
await page.setViewport(viewportOptions);

if (options.emulateDevice) {
if (!(options.emulateDevice in puppeteer.devices)) {
if (!(options.emulateDevice in KnownDevices)) {
throw new Error(`The device name \`${options.emulateDevice}\` is not supported`);
}

await page.emulate(puppeteer.devices[options.emulateDevice]);
await page.emulate(KnownDevices[options.emulateDevice]);
}

await page.emulateMediaFeatures([{
Expand Down Expand Up @@ -461,4 +461,4 @@ if (process.env.NODE_ENV === 'test') {

export default captureWebsite;

export const devices = Object.values(puppeteer.devices).map(device => device.name);
export const devices = Object.values(KnownDevices).map(device => device.name);
22 changes: 7 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava && tsd"
"test": "xo && ava --timeout=10m && tsd"
},
"files": [
"index.js",
Expand All @@ -39,31 +39,23 @@
"jpeg"
],
"dependencies": {
"@cliqz/adblocker-puppeteer": "^1.24.0",
"@cliqz/adblocker-puppeteer": "^1.25.1",
"file-url": "^4.0.0",
"puppeteer": "^17.0.0",
"puppeteer": "^19.2.0",
"node-fetch": "^3.2.10",
"tough-cookie": "^4.1.2"
},
"devDependencies": {
"ava": "^4.3.3",
"ava": "^5.0.1",
"create-test-server": "^3.0.1",
"delay": "^5.0.0",
"image-size": "^1.0.2",
"is-jpg": "^3.0.0",
"is-png": "^3.0.1",
"pify": "^6.0.0",
"pify": "^6.1.0",
"png-js": "^1.0.0",
"tempy": "^3.0.0",
"tsd": "^0.23.0",
"xo": "^0.52.2"
},
"tsd": {
"compilerOptions": {
"lib": [
"dom",
"es2020"
]
}
"tsd": "^0.24.1",
"xo": "^0.52.4"
}
}
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Set a custom [user agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Head

Type: `Array<string | object>`

Set cookies in [browser string format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) or [object format](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies).
Set cookies in [browser string format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) or [object format](https://pptr.dev/api/puppeteer.page.setcookie).

Tip: Go to the website you want a cookie for and [copy-paste it from DevTools](https://stackoverflow.com/a/24961735/64949).

Expand Down Expand Up @@ -380,7 +380,7 @@ Type: `string`

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.
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://pptr.dev/api/puppeteer.page) as the first argument and the [`browser` instance](https://pptr.dev/api/puppeteer.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()`.

Expand Down Expand Up @@ -456,7 +456,7 @@ await captureWebsite.file('index.html', 'screenshot.png', {
Type: `object`\
Default: `{}`

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

Note: Some of the launch options are overridden by the `debug` option.

Expand All @@ -476,7 +476,7 @@ 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.
This can be useful for [altering the JavaScript environment](https://pptr.dev/api/puppeteer.page.evaluateonnewdocument). 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.

##### clip

Expand Down Expand Up @@ -551,7 +551,7 @@ await Promise.all(items.map(([url, filename]) => {

### I'm getting a sandbox-related error

If you get an error like `No usable sandbox!` or `Running as root without --no-sandbox is not supported`, you need to properly [set up sandboxing](https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#setting-up-chrome-linux-sandbox) on your Linux instance.
If you get an error like `No usable sandbox!` or `Running as root without --no-sandbox is not supported`, you need to properly [set up sandboxing](https://pptr.dev/troubleshooting#setting-up-chrome-linux-sandbox) on your Linux instance.

Alternatively, if you completely trust the content, you can disable sandboxing (strongly discouraged):

Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {temporaryFile} from 'tempy';
import delay from 'delay';
import toughCookie from 'tough-cookie';
import fileUrl from 'file-url';
import puppeteer from 'puppeteer';
import {KnownDevices} from 'puppeteer';
import captureWebsite from './index.js';

const defaultResponse = (() => {
Expand Down Expand Up @@ -136,7 +136,7 @@ test('`scaleFactor` option', async t => {
});

test('`emulateDevice` option', async t => {
const device = puppeteer.devices['iPhone X'];
const device = KnownDevices['iPhone X'];

const size = imageSize(await instance(server.url, {
emulateDevice: device.name,
Expand Down

0 comments on commit 06c42d9

Please sign in to comment.