@d-zero/puppeteer-scroll 1.0.4 → 2.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/dist/scroll-all-over.d.ts +3 -1
- package/dist/scroll-all-over.js +25 -15
- package/package.json +7 -3
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { Page } from 'puppeteer';
|
|
1
|
+
import type { Page } from '@d-zero/puppeteer-page';
|
|
2
2
|
export type Options = {
|
|
3
3
|
distance?: number;
|
|
4
4
|
interval?: number;
|
|
5
|
+
logger?: (scrollY: number, scrollHeight: number, message: string) => void;
|
|
5
6
|
};
|
|
6
7
|
/**
|
|
7
8
|
* Scrolls the page vertically until the end or a maximum height is reached.
|
|
@@ -10,5 +11,6 @@ export type Options = {
|
|
|
10
11
|
* @param options - Optional parameters for scrolling.
|
|
11
12
|
* @param options.distance - The distance to scroll on each iteration (default: 100).
|
|
12
13
|
* @param options.interval - The interval between each scroll iteration in milliseconds (default: 300).
|
|
14
|
+
* @param options.logger - A function that logs messages.
|
|
13
15
|
*/
|
|
14
16
|
export declare function scrollAllOver(page: Page, options?: Options): Promise<void>;
|
package/dist/scroll-all-over.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { delay } from '@d-zero/shared/delay';
|
|
1
2
|
/**
|
|
2
3
|
* Scrolls the page vertically until the end or a maximum height is reached.
|
|
3
4
|
*
|
|
@@ -5,22 +6,31 @@
|
|
|
5
6
|
* @param options - Optional parameters for scrolling.
|
|
6
7
|
* @param options.distance - The distance to scroll on each iteration (default: 100).
|
|
7
8
|
* @param options.interval - The interval between each scroll iteration in milliseconds (default: 300).
|
|
9
|
+
* @param options.logger - A function that logs messages.
|
|
8
10
|
*/
|
|
9
11
|
export async function scrollAllOver(page, options) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
}, interval);
|
|
12
|
+
const interval = options?.interval ?? 300;
|
|
13
|
+
let currentScrollY = 0;
|
|
14
|
+
let scrollHeight = await page.evaluate(() => document.body.scrollHeight);
|
|
15
|
+
while (currentScrollY < scrollHeight) {
|
|
16
|
+
[currentScrollY, scrollHeight] = await page.evaluate(() => {
|
|
17
|
+
// Move the scroll position to the bottom of the page.
|
|
18
|
+
globalThis.scrollBy(0, document.documentElement.clientHeight);
|
|
19
|
+
// Return the current scroll position.
|
|
20
|
+
return [
|
|
21
|
+
globalThis.scrollY + globalThis.innerHeight,
|
|
22
|
+
document.body.scrollHeight,
|
|
23
|
+
];
|
|
24
24
|
});
|
|
25
|
-
|
|
25
|
+
options?.logger?.(currentScrollY, scrollHeight, 'Scrolling');
|
|
26
|
+
await delay(interval);
|
|
27
|
+
}
|
|
28
|
+
options?.logger?.(currentScrollY, scrollHeight, 'End of page');
|
|
29
|
+
await page.evaluate(() => {
|
|
30
|
+
// Move the scroll position to the top of the page.
|
|
31
|
+
globalThis.scrollTo(0, 0);
|
|
32
|
+
});
|
|
33
|
+
await delay(400);
|
|
34
|
+
options?.logger?.(currentScrollY, scrollHeight, 'End of page');
|
|
35
|
+
await delay(400);
|
|
26
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-zero/puppeteer-scroll",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Scroll function for puppeteer",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,10 +20,14 @@
|
|
|
20
20
|
],
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "tsc",
|
|
23
|
+
"watch": "tsc --watch",
|
|
23
24
|
"clean": "tsc --build --clean"
|
|
24
25
|
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@d-zero/shared": "0.6.0"
|
|
28
|
+
},
|
|
25
29
|
"devDependencies": {
|
|
26
|
-
"puppeteer": "
|
|
30
|
+
"@d-zero/puppeteer-page": "0.2.0"
|
|
27
31
|
},
|
|
28
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "1eb1c03400580040119121e11ffb33acd876fe1b"
|
|
29
33
|
}
|