@d-zero/puppeteer-screenshot 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/before-page-scan.d.ts +9 -0
- package/dist/before-page-scan.js +33 -0
- package/dist/get-binary.d.ts +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/screenshot-listener.d.ts +2 -0
- package/dist/screenshot-listener.js +43 -0
- package/dist/screenshot.d.ts +5 -2
- package/dist/screenshot.js +40 -33
- package/dist/types.d.ts +25 -29
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -16,7 +16,9 @@ import { screenshot } from '@d-zero/puppeteer-screenshot';
|
|
|
16
16
|
const browser = await puppeteer.launch();
|
|
17
17
|
const page = await browser.newPage();
|
|
18
18
|
|
|
19
|
+
// スクリーンショットのバイナリデータを持つオブジェクトを返します
|
|
19
20
|
const result = await screenshot(page, 'https://example.com', {
|
|
21
|
+
path: 'path/to/save.png', // 保存先のパスを指定することでファイルに保存できます(省略可)
|
|
20
22
|
sizes: {
|
|
21
23
|
desktop: { width: 1400 },
|
|
22
24
|
mobile: { width: 375, resolution: 2 },
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Listener, PageHook, Size } from './types.js';
|
|
2
|
+
import type { Page } from 'puppeteer';
|
|
3
|
+
type Options = {
|
|
4
|
+
name: string;
|
|
5
|
+
hooks?: readonly PageHook[];
|
|
6
|
+
listener?: Listener;
|
|
7
|
+
} & Size;
|
|
8
|
+
export declare function beforePageScan(page: Page, url: string, options?: Options): Promise<void>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { scrollAllOver } from '@d-zero/puppeteer-scroll';
|
|
2
|
+
export async function beforePageScan(page, url, options) {
|
|
3
|
+
const listener = options?.listener;
|
|
4
|
+
const name = options?.name ?? 'default';
|
|
5
|
+
const width = options?.width ?? 1400;
|
|
6
|
+
const resolution = options?.resolution;
|
|
7
|
+
listener?.('setViewport', { name, width, resolution });
|
|
8
|
+
await page.setViewport({
|
|
9
|
+
width,
|
|
10
|
+
height:
|
|
11
|
+
// Landscape or portrait
|
|
12
|
+
width > 1000 ? Math.floor(width * 0.75) : Math.floor(width * 1.5),
|
|
13
|
+
deviceScaleFactor: resolution ?? 1,
|
|
14
|
+
});
|
|
15
|
+
if (page.url() === url) {
|
|
16
|
+
listener?.('load', { name, type: 'reaload' });
|
|
17
|
+
await page.reload({ waitUntil: 'networkidle0' });
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
listener?.('load', { name, type: 'open' });
|
|
21
|
+
await page.goto(url, { waitUntil: 'networkidle0' });
|
|
22
|
+
}
|
|
23
|
+
for (const hook of options?.hooks ?? []) {
|
|
24
|
+
await hook(page, {
|
|
25
|
+
name,
|
|
26
|
+
width,
|
|
27
|
+
resolution,
|
|
28
|
+
log: (message) => listener?.('hook', { name, message }),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
listener?.('scroll', { name });
|
|
32
|
+
await scrollAllOver(page);
|
|
33
|
+
}
|
package/dist/get-binary.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Page } from 'puppeteer';
|
|
2
|
-
export declare function getBinary(page: Page): Promise<
|
|
2
|
+
export declare function getBinary(page: Page): Promise<Uint8Array>;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import c from 'ansi-colors';
|
|
3
|
+
export function screenshotListener(update) {
|
|
4
|
+
return (phase, data) => {
|
|
5
|
+
const sizeLabel = c.bgMagenta(` ${data.name} `);
|
|
6
|
+
switch (phase) {
|
|
7
|
+
case 'setViewport': {
|
|
8
|
+
const { width } = data;
|
|
9
|
+
update(`${sizeLabel} ↔️ Change viewport size to ${width}px`);
|
|
10
|
+
break;
|
|
11
|
+
}
|
|
12
|
+
case 'load': {
|
|
13
|
+
const { type } = data;
|
|
14
|
+
update(`${sizeLabel} %earth% ${type === 'open' ? 'Open' : 'Reload'} page`);
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
case 'hook': {
|
|
18
|
+
const { message } = data;
|
|
19
|
+
update(`${sizeLabel} ${message}`);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case 'scroll': {
|
|
23
|
+
update(`${sizeLabel} %propeller% Scroll the page`);
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case 'screenshotStart': {
|
|
27
|
+
update(`${sizeLabel} 📸 Take a screenshot`);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case 'screenshotSaving': {
|
|
31
|
+
const { path: filePath } = data;
|
|
32
|
+
const name = path.basename(filePath);
|
|
33
|
+
update(`${sizeLabel} 🖼 Save a file ${name}`);
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case 'screenshotError': {
|
|
37
|
+
const { error } = data;
|
|
38
|
+
update(`${sizeLabel} ❌️ ${error.message}`);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
package/dist/screenshot.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Screenshot, ScreenshotListener } from './types.js';
|
|
2
|
+
import type { PageHook, Sizes } from '@d-zero/puppeteer-page-scan';
|
|
2
3
|
import type { Page } from 'puppeteer';
|
|
3
4
|
type Options = {
|
|
5
|
+
id?: string;
|
|
4
6
|
sizes?: Sizes;
|
|
5
7
|
hooks?: readonly PageHook[];
|
|
6
|
-
listener?:
|
|
8
|
+
listener?: ScreenshotListener;
|
|
7
9
|
domOnly?: boolean;
|
|
10
|
+
path?: string;
|
|
8
11
|
};
|
|
9
12
|
/**
|
|
10
13
|
* Takes screenshots of a web page at different sizes and resolutions.
|
package/dist/screenshot.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { beforePageScan, defaultSizes } from '@d-zero/puppeteer-page-scan';
|
|
2
|
+
import { urlToFileName } from '@d-zero/shared/url-to-file-name';
|
|
2
3
|
import { getBinary } from './get-binary.js';
|
|
3
|
-
const defaultSizes = {
|
|
4
|
-
desktop: { width: 1400 },
|
|
5
|
-
tablet: { width: 768 },
|
|
6
|
-
mobile: { width: 375, resolution: 2 },
|
|
7
|
-
};
|
|
8
4
|
/**
|
|
9
5
|
* Takes screenshots of a web page at different sizes and resolutions.
|
|
10
6
|
*
|
|
@@ -20,42 +16,53 @@ export async function screenshot(page, url, options) {
|
|
|
20
16
|
const listener = options?.listener;
|
|
21
17
|
const result = {};
|
|
22
18
|
for (const [name, { width, resolution }] of Object.entries(sizes)) {
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
await beforePageScan(page, url, {
|
|
20
|
+
...options,
|
|
21
|
+
name,
|
|
25
22
|
width,
|
|
26
|
-
|
|
27
|
-
// Landscape or portrait
|
|
28
|
-
width > 1000 ? Math.floor(width * 0.75) : Math.floor(width * 1.5),
|
|
29
|
-
deviceScaleFactor: resolution ?? 1,
|
|
23
|
+
resolution,
|
|
30
24
|
});
|
|
31
|
-
if (page.url() === url) {
|
|
32
|
-
listener?.('load', { name, type: 'reaload' });
|
|
33
|
-
await page.reload({ waitUntil: 'networkidle0' });
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
listener?.('load', { name, type: 'open' });
|
|
37
|
-
await page.goto(url, { waitUntil: 'networkidle0' });
|
|
38
|
-
}
|
|
39
|
-
for (const hook of options?.hooks ?? []) {
|
|
40
|
-
await hook(page, {
|
|
41
|
-
name,
|
|
42
|
-
width,
|
|
43
|
-
resolution,
|
|
44
|
-
log: (message) => listener?.('hook', { name, message }),
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
listener?.('scroll', { name });
|
|
48
|
-
await scrollAllOver(page);
|
|
49
25
|
let binary = null;
|
|
26
|
+
const filePath = options?.path?.replace(/\.png$/i, `@${name}.png`) ?? null;
|
|
50
27
|
if (!options?.domOnly) {
|
|
51
28
|
listener?.('screenshotStart', { name });
|
|
52
|
-
|
|
53
|
-
|
|
29
|
+
try {
|
|
30
|
+
if (filePath && options?.path) {
|
|
31
|
+
listener?.('screenshotSaving', { name, path: options.path });
|
|
32
|
+
await page.screenshot({
|
|
33
|
+
path: filePath,
|
|
34
|
+
fullPage: true,
|
|
35
|
+
type: 'png',
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
binary = await getBinary(page);
|
|
40
|
+
listener?.('screenshotEnd', { name, binary });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (error instanceof Error) {
|
|
45
|
+
listener?.('screenshotError', { name, error });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
54
51
|
}
|
|
55
52
|
listener?.('getDOMStart', { name });
|
|
53
|
+
const title = await page.evaluate(() => document.title);
|
|
56
54
|
const dom = await page.content();
|
|
57
55
|
listener?.('getDOMEnd', { name, dom });
|
|
58
|
-
result[name] = {
|
|
56
|
+
result[name] = {
|
|
57
|
+
id: options?.id ?? urlToFileName(url),
|
|
58
|
+
filePath,
|
|
59
|
+
url,
|
|
60
|
+
title,
|
|
61
|
+
binary,
|
|
62
|
+
dom,
|
|
63
|
+
width,
|
|
64
|
+
resolution,
|
|
65
|
+
};
|
|
59
66
|
}
|
|
60
67
|
return result;
|
|
61
68
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,36 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export type Size = {
|
|
4
|
-
width: number;
|
|
5
|
-
resolution?: number;
|
|
6
|
-
};
|
|
1
|
+
export type { PageHook } from '@d-zero/puppeteer-page-scan';
|
|
2
|
+
import type { Listener as ScanListener, Phase as ScanPhase, Size } from '@d-zero/puppeteer-page-scan';
|
|
7
3
|
export type Screenshot = {
|
|
8
|
-
|
|
4
|
+
id: string;
|
|
5
|
+
filePath: string | null;
|
|
6
|
+
url: string;
|
|
7
|
+
title: string;
|
|
8
|
+
binary: Uint8Array | null;
|
|
9
9
|
dom: string;
|
|
10
10
|
} & Size;
|
|
11
|
-
export type
|
|
12
|
-
|
|
13
|
-
name: string;
|
|
14
|
-
width: number;
|
|
15
|
-
resolution?: number;
|
|
16
|
-
};
|
|
17
|
-
hook: {
|
|
18
|
-
name: string;
|
|
19
|
-
message: string;
|
|
20
|
-
};
|
|
21
|
-
load: {
|
|
11
|
+
export type ScreenshotPhase = {
|
|
12
|
+
screenshotStart: {
|
|
22
13
|
name: string;
|
|
23
|
-
type: 'open' | 'reaload';
|
|
24
14
|
};
|
|
25
|
-
|
|
15
|
+
screenshotEnd: {
|
|
26
16
|
name: string;
|
|
17
|
+
binary: Uint8Array;
|
|
27
18
|
};
|
|
28
|
-
|
|
19
|
+
screenshotSaving: {
|
|
29
20
|
name: string;
|
|
21
|
+
path: string;
|
|
30
22
|
};
|
|
31
|
-
|
|
23
|
+
screenshotError: {
|
|
32
24
|
name: string;
|
|
33
|
-
|
|
25
|
+
error: Error;
|
|
34
26
|
};
|
|
35
27
|
getDOMStart: {
|
|
36
28
|
name: string;
|
|
@@ -39,9 +31,13 @@ export type Phase = {
|
|
|
39
31
|
name: string;
|
|
40
32
|
dom: string;
|
|
41
33
|
};
|
|
42
|
-
};
|
|
43
|
-
export type
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
34
|
+
} & ScanPhase;
|
|
35
|
+
export type ScreenshotListener = ScanListener<ScreenshotPhase>;
|
|
36
|
+
/**
|
|
37
|
+
* @deprecated
|
|
38
|
+
*/
|
|
39
|
+
export type Phase = ScreenshotPhase;
|
|
40
|
+
/**
|
|
41
|
+
* @deprecated
|
|
42
|
+
*/
|
|
43
|
+
export type Listener = ScreenshotListener;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-zero/puppeteer-screenshot",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Screenshot utility for puppeteer",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,10 +23,13 @@
|
|
|
23
23
|
"clean": "tsc --build --clean"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@d-zero/puppeteer-
|
|
26
|
+
"@d-zero/puppeteer-page-scan": "1.0.0",
|
|
27
|
+
"@d-zero/puppeteer-scroll": "1.0.5",
|
|
28
|
+
"@d-zero/shared": "0.5.0",
|
|
29
|
+
"ansi-colors": "4.1.3"
|
|
27
30
|
},
|
|
28
31
|
"devDependencies": {
|
|
29
|
-
"puppeteer": "
|
|
32
|
+
"puppeteer": "23.5.0"
|
|
30
33
|
},
|
|
31
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "e8f65086bf7c316dda6667f1173da8585a5ef19c"
|
|
32
35
|
}
|