@d-zero/puppeteer-screenshot 1.0.0-alpha.2
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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/get-binary.d.ts +3 -0
- package/dist/get-binary.js +8 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/screenshot.d.ts +18 -0
- package/dist/screenshot.js +47 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +1 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 D-ZERO Co., Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# `@d-zero/puppeteer-screenshot`
|
|
2
|
+
|
|
3
|
+
Puppeteerでスクリーンショットを撮るためのユーティリティです。
|
|
4
|
+
|
|
5
|
+
## インストール
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
yarn install @d-zero/puppeteer-screenshot
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使い方
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { screenshot } from '@d-zero/puppeteer-screenshot';
|
|
15
|
+
|
|
16
|
+
const browser = await puppeteer.launch();
|
|
17
|
+
const page = await browser.newPage();
|
|
18
|
+
|
|
19
|
+
const result = await screenshot(page, 'https://example.com', {
|
|
20
|
+
sizes: {
|
|
21
|
+
desktop: { width: 1400 },
|
|
22
|
+
mobile: { width: 375, resolution: 2 },
|
|
23
|
+
},
|
|
24
|
+
listener: (phase, data) => {
|
|
25
|
+
console.log(phase, data);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { screenshot } from './screenshot.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Listener, Screenshot, Sizes } from './types.js';
|
|
2
|
+
import type { Page } from 'puppeteer';
|
|
3
|
+
type Options = {
|
|
4
|
+
sizes?: Sizes;
|
|
5
|
+
listener?: Listener;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Takes screenshots of a web page at different sizes and resolutions.
|
|
9
|
+
*
|
|
10
|
+
* @param page - The Puppeteer page object.
|
|
11
|
+
* @param url - The URL of the web page to take screenshots of.
|
|
12
|
+
* @param options - Optional settings for the screenshot process.
|
|
13
|
+
* @param options.sizes - The sizes and resolutions to take screenshots at (default: desktop, tablet, mobile).
|
|
14
|
+
* @param options.listener - A function that listens to the different phases of the screenshot process.
|
|
15
|
+
* @returns A promise that resolves to an object containing the screenshots.
|
|
16
|
+
*/
|
|
17
|
+
export declare function screenshot(page: Page, url: string, options?: Options): Promise<Record<string, Screenshot>>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { scrollAllOver } from '@d-zero/puppeteer-scroll';
|
|
2
|
+
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
|
+
/**
|
|
9
|
+
* Takes screenshots of a web page at different sizes and resolutions.
|
|
10
|
+
*
|
|
11
|
+
* @param page - The Puppeteer page object.
|
|
12
|
+
* @param url - The URL of the web page to take screenshots of.
|
|
13
|
+
* @param options - Optional settings for the screenshot process.
|
|
14
|
+
* @param options.sizes - The sizes and resolutions to take screenshots at (default: desktop, tablet, mobile).
|
|
15
|
+
* @param options.listener - A function that listens to the different phases of the screenshot process.
|
|
16
|
+
* @returns A promise that resolves to an object containing the screenshots.
|
|
17
|
+
*/
|
|
18
|
+
export async function screenshot(page, url, options) {
|
|
19
|
+
const sizes = options?.sizes ?? defaultSizes;
|
|
20
|
+
const listener = options?.listener;
|
|
21
|
+
const result = {};
|
|
22
|
+
for (const [name, { width, resolution }] of Object.entries(sizes)) {
|
|
23
|
+
listener?.('setViewport', { name, width, resolution });
|
|
24
|
+
await page.setViewport({
|
|
25
|
+
width,
|
|
26
|
+
height:
|
|
27
|
+
// Landscape or portrait
|
|
28
|
+
width > 1000 ? Math.floor(width * 0.75) : Math.floor(width * 1.5),
|
|
29
|
+
deviceScaleFactor: resolution ?? 1,
|
|
30
|
+
});
|
|
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
|
+
listener?.('scroll', { name });
|
|
40
|
+
await scrollAllOver(page);
|
|
41
|
+
listener?.('screenshotStart', { name });
|
|
42
|
+
const binary = await getBinary(page);
|
|
43
|
+
listener?.('screenshotEnd', { name, binary });
|
|
44
|
+
result[name] = { binary, width, resolution };
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
export type Sizes = Record<string, Size>;
|
|
3
|
+
export type Size = {
|
|
4
|
+
width: number;
|
|
5
|
+
resolution?: number;
|
|
6
|
+
};
|
|
7
|
+
export type Screenshot = {
|
|
8
|
+
binary: Buffer;
|
|
9
|
+
} & Size;
|
|
10
|
+
export type Phase = {
|
|
11
|
+
setViewport: {
|
|
12
|
+
name: string;
|
|
13
|
+
width: number;
|
|
14
|
+
resolution?: number;
|
|
15
|
+
};
|
|
16
|
+
load: {
|
|
17
|
+
name: string;
|
|
18
|
+
type: 'open' | 'reaload';
|
|
19
|
+
};
|
|
20
|
+
scroll: {
|
|
21
|
+
name: string;
|
|
22
|
+
};
|
|
23
|
+
screenshotStart: {
|
|
24
|
+
name: string;
|
|
25
|
+
};
|
|
26
|
+
screenshotEnd: {
|
|
27
|
+
name: string;
|
|
28
|
+
binary: Buffer;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
export type Listener = (phase: keyof Phase, data: Phase[keyof Phase]) => void;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@d-zero/puppeteer-screenshot",
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
|
+
"description": "Screenshot utility for puppeteer",
|
|
5
|
+
"author": "D-ZERO",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"private": false,
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc",
|
|
23
|
+
"clean": "tsc --build --clean"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@d-zero/puppeteer-scroll": "^1.0.0-alpha.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"puppeteer": "22.6.4"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "802df4fdb85892c8fab3bf1b376755e25ca2ca47"
|
|
32
|
+
}
|