@d-zero/puppeteer-scroll 1.0.0-alpha.2
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 +21 -0
- package/README.md +23 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/scroll-all-over.d.ts +14 -0
- package/dist/scroll-all-over.js +26 -0
- package/package.json +29 -0
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,23 @@
|
|
|
1
|
+
# `@d-zero/puppeteer-scroll`
|
|
2
|
+
|
|
3
|
+
Puppeteerでスクロールするための関数を提供します。
|
|
4
|
+
|
|
5
|
+
`IntersectionObserver`や`loading="lazy"`などの機能を使っているサイトに対して表示や読み込みを完了させるために、スクロールを行います。
|
|
6
|
+
|
|
7
|
+
## インストール
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
yarn install @d-zero/puppeteer-scroll
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 使い方
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { scrollAllOver } from '@d-zero/puppeteer-scroll';
|
|
17
|
+
|
|
18
|
+
const browser = await puppeteer.launch();
|
|
19
|
+
const page = await browser.newPage();
|
|
20
|
+
await page.goto('https://example.com');
|
|
21
|
+
|
|
22
|
+
await scrollAllOver(page);
|
|
23
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { scrollAllOver } from './scroll-all-over.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { scrollAllOver } from './scroll-all-over.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Page } from 'puppeteer';
|
|
2
|
+
export type Options = {
|
|
3
|
+
distance?: number;
|
|
4
|
+
interval?: number;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Scrolls the page vertically until the end or a maximum height is reached.
|
|
8
|
+
*
|
|
9
|
+
* @param page - The Puppeteer page object.
|
|
10
|
+
* @param options - Optional parameters for scrolling.
|
|
11
|
+
* @param options.distance - The distance to scroll on each iteration (default: 100).
|
|
12
|
+
* @param options.interval - The interval between each scroll iteration in milliseconds (default: 300).
|
|
13
|
+
*/
|
|
14
|
+
export declare function scrollAllOver(page: Page, options?: Options): Promise<void>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scrolls the page vertically until the end or a maximum height is reached.
|
|
3
|
+
*
|
|
4
|
+
* @param page - The Puppeteer page object.
|
|
5
|
+
* @param options - Optional parameters for scrolling.
|
|
6
|
+
* @param options.distance - The distance to scroll on each iteration (default: 100).
|
|
7
|
+
* @param options.interval - The interval between each scroll iteration in milliseconds (default: 300).
|
|
8
|
+
*/
|
|
9
|
+
export async function scrollAllOver(page, options) {
|
|
10
|
+
await page.evaluate(async (distance, interval) => {
|
|
11
|
+
distance = distance ?? document.documentElement.clientHeight - 5;
|
|
12
|
+
await new Promise((resolve) => {
|
|
13
|
+
let totalHeight = 0;
|
|
14
|
+
const timer = setInterval(() => {
|
|
15
|
+
const scrollHeight = document.body.scrollHeight;
|
|
16
|
+
window.scrollBy(0, distance);
|
|
17
|
+
totalHeight += distance;
|
|
18
|
+
if (totalHeight >= scrollHeight || totalHeight >= 100_000) {
|
|
19
|
+
clearInterval(timer);
|
|
20
|
+
window.scrollBy(0, 0);
|
|
21
|
+
resolve();
|
|
22
|
+
}
|
|
23
|
+
}, interval);
|
|
24
|
+
});
|
|
25
|
+
}, options?.distance, options?.interval ?? 300);
|
|
26
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@d-zero/puppeteer-scroll",
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
|
+
"description": "Scroll 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
|
+
"devDependencies": {
|
|
26
|
+
"puppeteer": "22.6.4"
|
|
27
|
+
},
|
|
28
|
+
"gitHead": "802df4fdb85892c8fab3bf1b376755e25ca2ca47"
|
|
29
|
+
}
|