@d-zero/puppeteer-page-scan 4.0.4 → 4.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 +31 -1
- package/dist/before-page-scan.d.ts +1 -0
- package/dist/before-page-scan.js +43 -2
- package/dist/default-sizes.d.ts +39 -0
- package/dist/default-sizes.js +37 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# `@d-zero/puppeteer-page-scan`
|
|
2
2
|
|
|
3
|
-
PuppeteerでスクリーンショットやDOM
|
|
3
|
+
PuppeteerでスクリーンショットやDOMスキャンする際に必要なヘルパー関数とデバイス設定を提供します。
|
|
4
4
|
|
|
5
5
|
## インストール
|
|
6
6
|
|
|
@@ -10,6 +10,36 @@ yarn install @d-zero/puppeteer-page-scan
|
|
|
10
10
|
|
|
11
11
|
## 使い方
|
|
12
12
|
|
|
13
|
+
### デバイスプリセット
|
|
14
|
+
|
|
15
|
+
複数のデバイスサイズ用のプリセットが利用可能です:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import {
|
|
19
|
+
devicePresets,
|
|
20
|
+
createSizesFromDevices,
|
|
21
|
+
parseDevicesOption,
|
|
22
|
+
} from '@d-zero/puppeteer-page-scan';
|
|
23
|
+
|
|
24
|
+
// 利用可能なデバイスプリセット
|
|
25
|
+
console.log(devicePresets);
|
|
26
|
+
// {
|
|
27
|
+
// desktop: { width: 1400 },
|
|
28
|
+
// tablet: { width: 768 },
|
|
29
|
+
// mobile: { width: 375, resolution: 2 },
|
|
30
|
+
// 'desktop-hd': { width: 1920 },
|
|
31
|
+
// 'desktop-compact': { width: 1280 },
|
|
32
|
+
// 'mobile-large': { width: 414, resolution: 3 },
|
|
33
|
+
// 'mobile-small': { width: 320, resolution: 2 }
|
|
34
|
+
// }
|
|
35
|
+
|
|
36
|
+
// プリセット名からSizesオブジェクトを生成
|
|
37
|
+
const sizes = createSizesFromDevices(['desktop', 'mobile']);
|
|
38
|
+
|
|
39
|
+
// CLI用のパーサー(コンマ区切りの文字列から)
|
|
40
|
+
const parsedSizes = parseDevicesOption(['desktop', 'tablet']);
|
|
41
|
+
```
|
|
42
|
+
|
|
13
43
|
### `beforePageScan`
|
|
14
44
|
|
|
15
45
|
- ビューポートの設定
|
package/dist/before-page-scan.js
CHANGED
|
@@ -10,6 +10,7 @@ export async function beforePageScan(page, url, options) {
|
|
|
10
10
|
const name = options?.name ?? 'default';
|
|
11
11
|
const width = options?.width ?? 1400;
|
|
12
12
|
const resolution = options?.resolution;
|
|
13
|
+
const timeout = options?.timeout;
|
|
13
14
|
listener?.('setViewport', { name, width, resolution });
|
|
14
15
|
await page.setViewport({
|
|
15
16
|
width,
|
|
@@ -20,11 +21,11 @@ export async function beforePageScan(page, url, options) {
|
|
|
20
21
|
});
|
|
21
22
|
if (page.url() === url) {
|
|
22
23
|
listener?.('load', { name, type: 'reaload' });
|
|
23
|
-
await page
|
|
24
|
+
await navigateWithFallback(page, url, timeout, true, listener, name);
|
|
24
25
|
}
|
|
25
26
|
else {
|
|
26
27
|
listener?.('load', { name, type: 'open' });
|
|
27
|
-
await page
|
|
28
|
+
await navigateWithFallback(page, url, timeout, false, listener, name);
|
|
28
29
|
}
|
|
29
30
|
for (const hook of options?.hooks ?? []) {
|
|
30
31
|
await hook(page, {
|
|
@@ -44,3 +45,43 @@ export async function beforePageScan(page, url, options) {
|
|
|
44
45
|
logger: (scrollY, scrollHeight, message) => listener?.('scroll', { name, scrollY, scrollHeight, message }),
|
|
45
46
|
});
|
|
46
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Navigate with fallback from networkidle0 to networkidle2 on timeout
|
|
50
|
+
* @param page
|
|
51
|
+
* @param url
|
|
52
|
+
* @param timeout
|
|
53
|
+
* @param isReload
|
|
54
|
+
* @param listener
|
|
55
|
+
* @param name
|
|
56
|
+
*/
|
|
57
|
+
async function navigateWithFallback(page, url, timeout, isReload, listener, name) {
|
|
58
|
+
try {
|
|
59
|
+
// First attempt: networkidle0 (stricter)
|
|
60
|
+
if (isReload) {
|
|
61
|
+
await page.reload({ waitUntil: 'networkidle0', timeout });
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
await page.goto(url, { waitUntil: 'networkidle0', timeout });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
// Check if it's a timeout error
|
|
69
|
+
if (error instanceof Error && error.message.includes('timeout')) {
|
|
70
|
+
listener?.('hook', {
|
|
71
|
+
name,
|
|
72
|
+
message: `networkidle0 timeout, retrying with networkidle2...`,
|
|
73
|
+
});
|
|
74
|
+
// Retry with networkidle2 (more lenient)
|
|
75
|
+
if (isReload) {
|
|
76
|
+
await page.reload({ waitUntil: 'networkidle2', timeout });
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
await page.goto(url, { waitUntil: 'networkidle2', timeout });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
// Re-throw non-timeout errors
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
package/dist/default-sizes.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Sizes } from './types.js';
|
|
1
2
|
export declare const defaultSizes: {
|
|
2
3
|
readonly desktop: {
|
|
3
4
|
readonly width: 1400;
|
|
@@ -10,3 +11,41 @@ export declare const defaultSizes: {
|
|
|
10
11
|
readonly resolution: 2;
|
|
11
12
|
};
|
|
12
13
|
};
|
|
14
|
+
export declare const devicePresets: {
|
|
15
|
+
readonly desktop: {
|
|
16
|
+
readonly width: 1400;
|
|
17
|
+
};
|
|
18
|
+
readonly tablet: {
|
|
19
|
+
readonly width: 768;
|
|
20
|
+
};
|
|
21
|
+
readonly mobile: {
|
|
22
|
+
readonly width: 375;
|
|
23
|
+
readonly resolution: 2;
|
|
24
|
+
};
|
|
25
|
+
readonly 'desktop-hd': {
|
|
26
|
+
readonly width: 1920;
|
|
27
|
+
};
|
|
28
|
+
readonly 'desktop-compact': {
|
|
29
|
+
readonly width: 1280;
|
|
30
|
+
};
|
|
31
|
+
readonly 'mobile-large': {
|
|
32
|
+
readonly width: 414;
|
|
33
|
+
readonly resolution: 3;
|
|
34
|
+
};
|
|
35
|
+
readonly 'mobile-small': {
|
|
36
|
+
readonly width: 320;
|
|
37
|
+
readonly resolution: 2;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Create sizes from device preset names
|
|
42
|
+
* @param deviceNames Array of device preset names
|
|
43
|
+
* @returns Sizes object
|
|
44
|
+
*/
|
|
45
|
+
export declare function createSizesFromDevices(deviceNames: string[]): Sizes;
|
|
46
|
+
/**
|
|
47
|
+
* Parse device names and return device presets
|
|
48
|
+
* @param deviceNames Array of device names or undefined
|
|
49
|
+
* @returns Sizes object or undefined
|
|
50
|
+
*/
|
|
51
|
+
export declare function parseDevicesOption(deviceNames?: string[]): Sizes | undefined;
|
package/dist/default-sizes.js
CHANGED
|
@@ -3,3 +3,40 @@ export const defaultSizes = {
|
|
|
3
3
|
tablet: { width: 768 },
|
|
4
4
|
mobile: { width: 375, resolution: 2 },
|
|
5
5
|
};
|
|
6
|
+
export const devicePresets = {
|
|
7
|
+
desktop: { width: 1400 },
|
|
8
|
+
tablet: { width: 768 },
|
|
9
|
+
mobile: { width: 375, resolution: 2 },
|
|
10
|
+
'desktop-hd': { width: 1920 },
|
|
11
|
+
'desktop-compact': { width: 1280 },
|
|
12
|
+
'mobile-large': { width: 414, resolution: 3 },
|
|
13
|
+
'mobile-small': { width: 320, resolution: 2 },
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Create sizes from device preset names
|
|
17
|
+
* @param deviceNames Array of device preset names
|
|
18
|
+
* @returns Sizes object
|
|
19
|
+
*/
|
|
20
|
+
export function createSizesFromDevices(deviceNames) {
|
|
21
|
+
const sizes = {};
|
|
22
|
+
for (const name of deviceNames) {
|
|
23
|
+
if (name in devicePresets) {
|
|
24
|
+
sizes[name] = devicePresets[name];
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
throw new Error(`Unknown device preset: ${name}. Available presets: ${Object.keys(devicePresets).join(', ')}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return sizes;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Parse device names and return device presets
|
|
34
|
+
* @param deviceNames Array of device names or undefined
|
|
35
|
+
* @returns Sizes object or undefined
|
|
36
|
+
*/
|
|
37
|
+
export function parseDevicesOption(deviceNames) {
|
|
38
|
+
if (deviceNames && deviceNames.length > 0) {
|
|
39
|
+
return createSizesFromDevices(deviceNames);
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { beforePageScan } from './before-page-scan.js';
|
|
2
|
-
export { defaultSizes } from './default-sizes.js';
|
|
2
|
+
export { defaultSizes, devicePresets, createSizesFromDevices, parseDevicesOption, } from './default-sizes.js';
|
|
3
3
|
export { readPageHooks } from './read-page-hooks.js';
|
|
4
4
|
export { pageScanListener, pageScanLoggers } from './page-scan-listener.js';
|
|
5
5
|
export * from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { beforePageScan } from './before-page-scan.js';
|
|
2
|
-
export { defaultSizes } from './default-sizes.js';
|
|
2
|
+
export { defaultSizes, devicePresets, createSizesFromDevices, parseDevicesOption, } from './default-sizes.js';
|
|
3
3
|
export { readPageHooks } from './read-page-hooks.js';
|
|
4
4
|
export { pageScanListener, pageScanLoggers } from './page-scan-listener.js';
|
|
5
5
|
export * from './types.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-zero/puppeteer-page-scan",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Scanning page function for puppeteer",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@d-zero/puppeteer-general-actions": "1.2.1",
|
|
27
|
-
"@d-zero/puppeteer-scroll": "3.0.
|
|
27
|
+
"@d-zero/puppeteer-scroll": "3.0.6"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"puppeteer": "24.
|
|
30
|
+
"puppeteer": "24.18.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"puppeteer": "24.
|
|
33
|
+
"puppeteer": "24.18.0"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "00ab4d99dc17e8f09f1a84e3a02f702629b642fc"
|
|
36
36
|
}
|