@d-zero/puppeteer-page-scan 1.0.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/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,47 @@
1
+ # `@d-zero/puppeteer-page-scan`
2
+
3
+ PuppeteerでスクリーンショットやDOMスキャンする際に必要なヘルパー関数を提供します。
4
+
5
+ ## インストール
6
+
7
+ ```sh
8
+ yarn install @d-zero/puppeteer-page-scan
9
+ ```
10
+
11
+ ## 使い方
12
+
13
+ ### `beforePageScan`
14
+
15
+ - ビューポートの設定
16
+ - ページのリロード
17
+ - 任意のフック処理
18
+ - ログインなどの事前処理
19
+ - ページ全体をスクロール
20
+
21
+ などを行い、スキャンに必要な状態を整えるためのヘルパー関数です。
22
+
23
+ ```ts
24
+ import { beforePageScan } from '@d-zero/puppeteer-page-scan';
25
+
26
+ const browser = await puppeteer.launch();
27
+ const page = await browser.newPage();
28
+
29
+ await beforePageScan(page, 'https://example.com', {
30
+ name: 'desktop',
31
+ width: 1200,
32
+ resolution: 1,
33
+ listeners: {
34
+ setViewport() {},
35
+ hook() {},
36
+ load() {},
37
+ scroll() {},
38
+ },
39
+ hooks: [
40
+ async (page) => {
41
+ await page.type('#username', 'user');
42
+ await page.type('#password', 'password');
43
+ await page.click('button[type="submit"]');
44
+ },
45
+ ],
46
+ });
47
+ ```
@@ -0,0 +1,9 @@
1
+ import type { Listener, PageHook, Phase, Size } from './types.js';
2
+ import type { Page } from 'puppeteer';
3
+ type Options = {
4
+ name: string;
5
+ hooks?: readonly PageHook[];
6
+ listener?: Listener<Phase>;
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
+ }
@@ -0,0 +1,12 @@
1
+ export declare const defaultSizes: {
2
+ readonly desktop: {
3
+ readonly width: 1400;
4
+ };
5
+ readonly tablet: {
6
+ readonly width: 768;
7
+ };
8
+ readonly mobile: {
9
+ readonly width: 375;
10
+ readonly resolution: 2;
11
+ };
12
+ };
@@ -0,0 +1,5 @@
1
+ export const defaultSizes = {
2
+ desktop: { width: 1400 },
3
+ tablet: { width: 768 },
4
+ mobile: { width: 375, resolution: 2 },
5
+ };
@@ -0,0 +1,4 @@
1
+ export { beforePageScan } from './before-page-scan.js';
2
+ export { defaultSizes } from './default-sizes.js';
3
+ export { readPageHooks } from './read-page-hooks.js';
4
+ export * from './types.js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { beforePageScan } from './before-page-scan.js';
2
+ export { defaultSizes } from './default-sizes.js';
3
+ export { readPageHooks } from './read-page-hooks.js';
4
+ export * from './types.js';
@@ -0,0 +1,2 @@
1
+ import type { PageHook } from './types.js';
2
+ export declare function readPageHooks(hooks: readonly string[], baseDir: string): Promise<PageHook[]>;
@@ -0,0 +1,19 @@
1
+ import path from 'node:path';
2
+ export async function readPageHooks(hooks, baseDir) {
3
+ const pageHooks = await Promise.all(hooks.map(async (hook) => {
4
+ const hookAbsPath = path.isAbsolute(hook) ? hook : path.resolve(baseDir, hook);
5
+ const { default: mod } = await import(hookAbsPath).catch((error) => {
6
+ if (error instanceof Error &&
7
+ 'code' in error &&
8
+ error.code === 'ERR_MODULE_NOT_FOUND') {
9
+ throw new Error(`Hook: ${hook} not found`, { cause: error });
10
+ }
11
+ throw error;
12
+ });
13
+ if (typeof mod !== 'function') {
14
+ throw new TypeError(`Hook ${hook} is not a function`);
15
+ }
16
+ return mod;
17
+ }));
18
+ return pageHooks;
19
+ }
@@ -0,0 +1,29 @@
1
+ import type { Page } from 'puppeteer';
2
+ export type Listener<P> = (phase: keyof P, data: P[keyof P]) => void;
3
+ export type Sizes = Record<string, Size>;
4
+ export type Size = {
5
+ width: number;
6
+ resolution?: number;
7
+ };
8
+ export type PageHook = (page: Page, size: Size & {
9
+ name: string;
10
+ log: (message: string) => void;
11
+ }) => Promise<void>;
12
+ export type Phase = {
13
+ setViewport: {
14
+ name: string;
15
+ width: number;
16
+ resolution?: number;
17
+ };
18
+ hook: {
19
+ name: string;
20
+ message: string;
21
+ };
22
+ load: {
23
+ name: string;
24
+ type: 'open' | 'reaload';
25
+ };
26
+ scroll: {
27
+ name: string;
28
+ };
29
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@d-zero/puppeteer-page-scan",
3
+ "version": "1.0.0",
4
+ "description": "Scanning page function 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.5"
27
+ },
28
+ "devDependencies": {
29
+ "puppeteer": "23.5.0"
30
+ },
31
+ "gitHead": "e8f65086bf7c316dda6667f1173da8585a5ef19c"
32
+ }