@d-zero/puppeteer-screenshot 3.0.0 → 3.1.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.
@@ -1,6 +1,7 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
1
+ import type { ElementHandle, Page, ScreenshotOptions } from 'puppeteer';
2
2
  /**
3
3
  *
4
- * @param page
4
+ * @param scope
5
+ * @param options
5
6
  */
6
- export declare function getBinary(page: Page): Promise<Uint8Array<ArrayBufferLike>>;
7
+ export declare function getBinary(scope: Page | ElementHandle<Element>, options?: Readonly<ScreenshotOptions>): Promise<Uint8Array<ArrayBufferLike>>;
@@ -1,10 +1,11 @@
1
1
  /**
2
2
  *
3
- * @param page
3
+ * @param scope
4
+ * @param options
4
5
  */
5
- export async function getBinary(page) {
6
- const buffer = await page.screenshot({
7
- fullPage: true,
6
+ export async function getBinary(scope, options) {
7
+ const buffer = await scope.screenshot({
8
+ ...options,
8
9
  type: 'png',
9
10
  encoding: 'binary',
10
11
  });
@@ -4,12 +4,12 @@ import { pageScanLoggers } from '@d-zero/puppeteer-page-scan';
4
4
  export const screenshotListener = createListener((log) => {
5
5
  return {
6
6
  ...pageScanLoggers(log),
7
- screenshotStart() {
8
- log(`📸 Take a screenshot`);
7
+ screenshotStart({ selector }) {
8
+ log(`📸 Take a screenshot` + (selector ? ` for ${selector}` : ''));
9
9
  },
10
- screenshotSaving({ path: filePath }) {
10
+ screenshotSaving({ path: filePath, selector }) {
11
11
  const name = path.basename(filePath);
12
- log(`🖼 Save a file ${name}`);
12
+ log(`🖼 Save a file ${name}` + (selector ? ` for ${selector}` : ''));
13
13
  },
14
14
  screenshotError({ error }) {
15
15
  log(`❌️ ${error.message}`);
@@ -1,7 +1,7 @@
1
1
  import type { Screenshot, ScreenshotPhase } from './types.js';
2
2
  import type { Listener } from '@d-zero/puppeteer-general-actions';
3
- import type { Page } from '@d-zero/puppeteer-page';
4
3
  import type { PageHook, Sizes } from '@d-zero/puppeteer-page-scan';
4
+ import type { Page } from 'puppeteer';
5
5
  type Options = {
6
6
  id?: string;
7
7
  sizes?: Sizes;
@@ -9,6 +9,8 @@ type Options = {
9
9
  listener?: Listener<ScreenshotPhase>;
10
10
  domOnly?: boolean;
11
11
  path?: string;
12
+ selector?: string;
13
+ ignore?: string;
12
14
  };
13
15
  /**
14
16
  * Takes screenshots of a web page at different sizes and resolutions.
@@ -24,21 +24,51 @@ export async function screenshot(page, url, options) {
24
24
  listener?.(phase, data);
25
25
  },
26
26
  });
27
+ if (options?.ignore) {
28
+ await page.evaluate((ignore) => {
29
+ const scope = document.body;
30
+ const nodes = scope.querySelectorAll(ignore);
31
+ for (const node of nodes) {
32
+ const box = node.getBoundingClientRect();
33
+ const replacement = document.createElement('div');
34
+ replacement.style.position = node.style.position;
35
+ replacement.style.top = node.style.top;
36
+ replacement.style.left = node.style.left;
37
+ replacement.style.right = node.style.right;
38
+ replacement.style.bottom = node.style.bottom;
39
+ replacement.style.zIndex = node.style.zIndex;
40
+ replacement.style.margin = node.style.margin;
41
+ replacement.style.border = node.style.border;
42
+ replacement.style.width = `${box.width}px`;
43
+ replacement.style.height = `${box.height}px`;
44
+ node.replaceWith(replacement);
45
+ }
46
+ }, options?.ignore);
47
+ }
27
48
  let binary = null;
28
49
  const filePath = options?.path?.replace(/\.png$/i, `@${name}.png`) ?? null;
29
50
  if (!options?.domOnly) {
30
51
  listener?.('screenshotStart', { name });
52
+ const scope = options?.selector
53
+ ? await page.waitForSelector(options.selector)
54
+ : page;
55
+ const fullPage = options?.selector ? false : true;
56
+ if (!scope) {
57
+ throw new Error(`Element not found: ${options?.selector}`);
58
+ }
31
59
  try {
32
60
  if (filePath && options?.path) {
33
61
  listener?.('screenshotSaving', { name, path: options.path });
34
- await page.screenshot({
35
- path: filePath,
36
- fullPage: true,
62
+ await scope.screenshot({
63
+ fullPage,
37
64
  type: 'png',
65
+ path: filePath,
38
66
  });
39
67
  }
40
68
  else {
41
- binary = await getBinary(page);
69
+ binary = await getBinary(scope, {
70
+ fullPage,
71
+ });
42
72
  listener?.('screenshotEnd', { name, binary });
43
73
  }
44
74
  }
@@ -54,6 +84,28 @@ export async function screenshot(page, url, options) {
54
84
  listener?.('getDOMStart', { name });
55
85
  const title = await page.evaluate(() => document.title);
56
86
  const dom = await page.content();
87
+ const text = await page.evaluate((selector) => {
88
+ const scope = selector
89
+ ? (document.querySelector(selector) ?? document.body)
90
+ : document.body;
91
+ // Normalize text content for diff accuracy by adding line breaks between block elements
92
+ // This ensures consistent text extraction regardless of HTML formatting
93
+ const lineBreaks = scope.querySelectorAll('div, h1, h2, h3, h4, h5, h6, br, p, li, dt, dd, th, td');
94
+ for (const node of lineBreaks) {
95
+ node.append('\n');
96
+ }
97
+ const textContent = scope.textContent ?? '';
98
+ const altTextList = [...(scope.querySelectorAll('img') ?? [])]
99
+ .map((img) => {
100
+ const alt = img.getAttribute('alt');
101
+ return alt ?? '';
102
+ })
103
+ .filter((alt) => alt !== '');
104
+ return {
105
+ textContent,
106
+ altTextList,
107
+ };
108
+ }, options?.selector);
57
109
  listener?.('getDOMEnd', { name, dom });
58
110
  result[name] = {
59
111
  id: options?.id ?? urlToFileName(url),
@@ -62,6 +114,7 @@ export async function screenshot(page, url, options) {
62
114
  title,
63
115
  binary,
64
116
  dom,
117
+ text,
65
118
  width,
66
119
  resolution,
67
120
  };
package/dist/types.d.ts CHANGED
@@ -8,10 +8,15 @@ export type Screenshot = {
8
8
  title: string;
9
9
  binary: Uint8Array | null;
10
10
  dom: string;
11
+ text: {
12
+ textContent: string;
13
+ altTextList: readonly string[];
14
+ };
11
15
  } & Size;
12
16
  export type ScreenshotPhase = {
13
17
  screenshotStart: {
14
18
  name: string;
19
+ selector?: string;
15
20
  };
16
21
  screenshotEnd: {
17
22
  name: string;
@@ -20,6 +25,7 @@ export type ScreenshotPhase = {
20
25
  screenshotSaving: {
21
26
  name: string;
22
27
  path: string;
28
+ selector?: string;
23
29
  };
24
30
  screenshotError: {
25
31
  name: string;
@@ -27,6 +33,7 @@ export type ScreenshotPhase = {
27
33
  };
28
34
  getDOMStart: {
29
35
  name: string;
36
+ selector?: string;
30
37
  };
31
38
  getDOMEnd: {
32
39
  name: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/puppeteer-screenshot",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Screenshot utility for puppeteer",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -25,11 +25,14 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@d-zero/puppeteer-general-actions": "1.2.0",
28
- "@d-zero/puppeteer-page-scan": "3.0.0",
29
- "@d-zero/shared": "0.7.0"
28
+ "@d-zero/puppeteer-page-scan": "4.0.1",
29
+ "@d-zero/shared": "0.9.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@d-zero/puppeteer-page": "0.3.0"
32
+ "puppeteer": "24.10.1"
33
33
  },
34
- "gitHead": "e4fd17857e31022d121527b00fd7f009dbdb2142"
34
+ "peerDependencies": {
35
+ "puppeteer": "24.10.1"
36
+ },
37
+ "gitHead": "04c6969564182c36ee38ef41e78130936dfa4863"
35
38
  }