@d-zero/puppeteer-scroll 3.1.15 → 3.1.16

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.
@@ -7,6 +7,11 @@ export type Options = {
7
7
  };
8
8
  /**
9
9
  * Scrolls the page vertically until the end or a maximum height is reached.
10
+ *
11
+ * Detects stuck scrolling (e.g. fullpage.js or other scroll-jacking libraries
12
+ * that block `scrollBy` while `body.scrollHeight` exceeds the viewport) and
13
+ * bails out after {@link MAX_STUCK_ITERATIONS} consecutive iterations without
14
+ * progress.
10
15
  * @param page - The Puppeteer page object.
11
16
  * @param options - Optional parameters for scrolling.
12
17
  * @param options.distance - The distance to scroll on each iteration (default: 100).
@@ -1,6 +1,20 @@
1
1
  import { delay } from '@d-zero/shared/delay';
2
+ /**
3
+ * Number of consecutive iterations without scroll progress before bailing out.
4
+ *
5
+ * Scroll-jacking libraries (e.g. fullpage.js) can block `scrollBy` while
6
+ * `body.scrollHeight` remains larger than the viewport, causing an infinite
7
+ * loop. Three stuck iterations (≈ 900 ms at the default interval) is enough
8
+ * to confirm that scrolling is genuinely blocked.
9
+ */
10
+ const MAX_STUCK_ITERATIONS = 3;
2
11
  /**
3
12
  * Scrolls the page vertically until the end or a maximum height is reached.
13
+ *
14
+ * Detects stuck scrolling (e.g. fullpage.js or other scroll-jacking libraries
15
+ * that block `scrollBy` while `body.scrollHeight` exceeds the viewport) and
16
+ * bails out after {@link MAX_STUCK_ITERATIONS} consecutive iterations without
17
+ * progress.
4
18
  * @param page - The Puppeteer page object.
5
19
  * @param options - Optional parameters for scrolling.
6
20
  * @param options.distance - The distance to scroll on each iteration (default: 100).
@@ -11,6 +25,8 @@ export async function scrollAllOver(page, options) {
11
25
  const interval = options?.interval ?? 300;
12
26
  let currentScrollY = 0;
13
27
  let scrollHeight = await page.evaluate(() => document.body.scrollHeight);
28
+ let prevScrollY = -1;
29
+ let stuckCount = 0;
14
30
  while (Math.ceil(currentScrollY) < Math.ceil(scrollHeight)) {
15
31
  [currentScrollY, scrollHeight] = await page.evaluate(() => {
16
32
  // Move the scroll position to the bottom of the page.
@@ -22,6 +38,17 @@ export async function scrollAllOver(page, options) {
22
38
  ];
23
39
  });
24
40
  options?.logger?.(currentScrollY, scrollHeight, 'Scrolling');
41
+ if (currentScrollY === prevScrollY) {
42
+ stuckCount++;
43
+ if (stuckCount >= MAX_STUCK_ITERATIONS) {
44
+ options?.logger?.(currentScrollY, scrollHeight, 'Scroll stuck, bailing out');
45
+ break;
46
+ }
47
+ }
48
+ else {
49
+ stuckCount = 0;
50
+ }
51
+ prevScrollY = currentScrollY;
25
52
  await delay(interval);
26
53
  }
27
54
  options?.logger?.(currentScrollY, scrollHeight, 'End of page');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/puppeteer-scroll",
3
- "version": "3.1.15",
3
+ "version": "3.1.16",
4
4
  "description": "Scroll function for puppeteer",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -31,5 +31,5 @@
31
31
  "peerDependencies": {
32
32
  "puppeteer": "24.37.5"
33
33
  },
34
- "gitHead": "6f13c72151f3d6fe5ddf72d0cde61b31a7fc96ac"
34
+ "gitHead": "a347e4e88968fca7f2ace7639090d32014479d9f"
35
35
  }