@koalarx/scrapping 2.0.3 → 2.0.4
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/Dom.d.ts +2 -0
- package/Dom.js +48 -9
- package/package.json +1 -1
package/Dom.d.ts
CHANGED
|
@@ -15,7 +15,9 @@ export declare class DOM {
|
|
|
15
15
|
click(selector: string): Promise<void>;
|
|
16
16
|
focus(selector: string): Promise<void>;
|
|
17
17
|
content(selector: string): Promise<string[]>;
|
|
18
|
+
getText(selector: string): Promise<string>;
|
|
18
19
|
getDatatable<T = any>(selector: string, options?: GetDatatableOptions): Promise<T[]>;
|
|
19
20
|
waitNavigation(): Promise<void>;
|
|
21
|
+
waitIsVisible(selector: string, limitTime?: number): Promise<void>;
|
|
20
22
|
getDownloadedFiles(): Promise<Buffer<ArrayBufferLike>[]>;
|
|
21
23
|
}
|
package/Dom.js
CHANGED
|
@@ -55,19 +55,27 @@ class DOM {
|
|
|
55
55
|
return elements.map((el) => el.innerText.trim());
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
|
+
async getText(selector) {
|
|
59
|
+
await this._page.locator(selector).scroll();
|
|
60
|
+
return this._page.$eval(selector, (el) => el.innerText.trim());
|
|
61
|
+
}
|
|
58
62
|
async getDatatable(selector, options) {
|
|
59
63
|
const table = await this._page.$eval(selector, (table) => table.outerHTML);
|
|
60
64
|
const tableData = html_table_to_json_1.default.parse(table).results[0];
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const numberValue = Number(value);
|
|
66
|
-
rowData[(0, utils_1.toCamelCase)(key)] = isNaN(numberValue) ? value : numberValue;
|
|
67
|
-
});
|
|
68
|
-
return rowData;
|
|
69
|
-
});
|
|
65
|
+
let result = [];
|
|
66
|
+
if (tableData.length === 0) {
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
70
69
|
if (options?.withPagination) {
|
|
70
|
+
result.push(...tableData.map((row) => {
|
|
71
|
+
const rowData = {};
|
|
72
|
+
Object.keys(row).forEach((key) => {
|
|
73
|
+
const value = row[key];
|
|
74
|
+
const numberValue = Number(value);
|
|
75
|
+
rowData[(0, utils_1.toCamelCase)(key)] = isNaN(numberValue) ? value : numberValue;
|
|
76
|
+
});
|
|
77
|
+
return rowData;
|
|
78
|
+
}));
|
|
71
79
|
const nextButtonSelector = options.withPagination.nextButtonSelector;
|
|
72
80
|
const nextButton = await this._page.$(nextButtonSelector);
|
|
73
81
|
if (nextButton &&
|
|
@@ -77,6 +85,20 @@ class DOM {
|
|
|
77
85
|
result.push(...nextPageData);
|
|
78
86
|
}
|
|
79
87
|
}
|
|
88
|
+
else if (options?.infiniteScroll) {
|
|
89
|
+
let endOfPage = false;
|
|
90
|
+
do {
|
|
91
|
+
await this._page
|
|
92
|
+
.locator(`body > my-app > table > tbody > tr:nth-child(${tableData.length})`)
|
|
93
|
+
.scroll();
|
|
94
|
+
await (0, utils_1.delay)(1000);
|
|
95
|
+
const nextPageData = await this.getDatatable(selector, options);
|
|
96
|
+
result.push(...nextPageData);
|
|
97
|
+
if (nextPageData.length === tableData.length) {
|
|
98
|
+
endOfPage = true;
|
|
99
|
+
}
|
|
100
|
+
} while (!endOfPage);
|
|
101
|
+
}
|
|
80
102
|
return result;
|
|
81
103
|
}
|
|
82
104
|
async waitNavigation() {
|
|
@@ -84,6 +106,23 @@ class DOM {
|
|
|
84
106
|
.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 5000 })
|
|
85
107
|
.catch(() => null);
|
|
86
108
|
}
|
|
109
|
+
async waitIsVisible(selector, limitTime = 10000) {
|
|
110
|
+
let currentTime = 0;
|
|
111
|
+
let intervalTime = 500;
|
|
112
|
+
let element = null;
|
|
113
|
+
do {
|
|
114
|
+
element = await this._page
|
|
115
|
+
.locator(selector)
|
|
116
|
+
.wait()
|
|
117
|
+
.catch(() => null);
|
|
118
|
+
if (element) {
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
await (0, utils_1.delay)(intervalTime);
|
|
122
|
+
currentTime += intervalTime;
|
|
123
|
+
} while (!element && currentTime < limitTime);
|
|
124
|
+
await this._page.locator(selector).scroll();
|
|
125
|
+
}
|
|
87
126
|
async getDownloadedFiles() {
|
|
88
127
|
const downloadPath = this._options?.downloadFolderPath ?? './downloads';
|
|
89
128
|
if ((0, node_fs_1.existsSync)(downloadPath)) {
|
package/package.json
CHANGED