forked from locoloader/chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.js
320 lines (281 loc) · 10.1 KB
/
fetcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Obtain AsyncFunction
// @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
const LLAsyncFunction = (async function () {}).constructor;
// Actions
const LLActionsErrors = [];
const LLActionsScraped = [];
const LLActions = {
element: (selector) => {
return new Promise((resolve) => {
if (typeof selector !== 'string') {
LLActionsErrors.push('Type error: Element selector must be a string.');
resolve();
return;
}
const el = document.querySelector(selector);
if (!el) {
LLActionsErrors.push('DOM error: Element not found.');
resolve();
return;
}
LLActionsScraped.push(el.outerHTML);
resolve();
});
},
elements: (selector) => {
return new Promise((resolve) => {
if (typeof selector !== 'string') {
LLActionsErrors.push('Type error: Element selector must be a string.');
resolve();
return;
}
const els = document.querySelectorAll(selector);
if (!els.length) {
LLActionsErrors.push('DOM error: Element not found.');
resolve();
return;
}
for (const el of els) {
LLActionsScraped.push(el.outerHTML);
}
resolve();
});
},
click: (selector) => {
return new Promise((resolve) => {
if (typeof selector !== 'string') {
LLActionsErrors.push('Type error: Click selector must be a string.');
resolve();
return;
}
const el = document.querySelector(selector);
if (!el) {
LLActionsErrors.push('DOM error: Click element not found.');
resolve();
return;
}
el.click();
resolve();
});
},
fill: (array) => {
return new Promise((resolve) => {
if (array.constructor.name !== 'Array') {
LLActionsErrors.push('Type error: Fill parameter must be an array.');
resolve();
return;
}
if (!array[0] || typeof array[0] !== 'string') {
LLActionsErrors.push('Type error: Fill selector must be a string.');
resolve();
return;
}
if (!array[1] || (typeof array[1] !== 'string' && typeof array[1] !== 'number')) {
LLActionsErrors.push('Type error: Fill value must be a string or a number.');
resolve();
return;
}
const el = document.querySelector(array[0]);
if (!el) {
LLActionsErrors.push('DOM error: Fill element not found.');
resolve();
return;
}
if (el.tagName.toLowerCase() !== 'input' && el.tagName.toLowerCase() !== 'textarea') {
LLActionsErrors.push('DOM error: Fill element must be input or textarea.');
resolve();
return;
}
el.value = array[1];
resolve();
});
},
scrollX: (offset) => {
return new Promise((resolve) => {
if (typeof offset !== 'number') {
LLActionsErrors.push('Type error: ScrollX parameter must be a number.');
resolve();
return;
}
document.documentElement.scrollLeft = document.body.scrollLeft = offset;
resolve();
});
},
scrollY: (offset) => {
return new Promise((resolve) => {
if (typeof offset !== 'number') {
LLActionsErrors.push('Type error: ScrollY parameter must be a number.');
resolve();
return;
}
document.documentElement.scrollTop = document.body.scrollTop = offset;
resolve();
});
},
scrollTo: (selector) => {
return new Promise((resolve) => {
if (typeof selector !== 'string') {
LLActionsErrors.push('Type error: ScrollTo parameter must be a string.');
resolve();
return;
}
const el = document.querySelector(selector);
if (!el) {
LLActionsErrors.push('DOM error: ScrollTo element not found.');
resolve();
return;
}
el.scrollIntoView({
block: 'center',
inline: 'center',
})
resolve();
});
},
wait: (ms) => {
return new Promise((resolve) => {
if (typeof ms !== 'number') {
LLActionsErrors.push('Type error: Wait parameter must be a number.');
resolve();
return;
}
// Allow waiting for 20s max
if (ms > 20000) {
ms = 20000;
}
setTimeout(resolve, ms);
});
},
waitFor: (selector) => {
return new Promise((resolve) => {
let maxTime = 30000;
if (selector.constructor.name === 'Array') {
if (!selector[0] || typeof selector[0] !== 'string') {
LLActionsErrors.push('Type error: WaitFor selector[0] must be a string.');
resolve();
return;
}
if (!selector[1] || typeof selector[1] !== 'number') {
LLActionsErrors.push('Type error: WaitFor selector[1] must be a number.');
resolve();
return;
}
maxTime = selector[1];
selector = selector[0];
} else if (typeof selector !== 'string') {
LLActionsErrors.push('Type error: WaitFor selector must be a string.');
resolve();
return;
}
if (document.querySelector(selector)) {
resolve();
return;
}
const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
observer.disconnect();
resolve();
return;
}
});
observer.observe(document, {
childList: true,
subtree: true
});
// Don't wait more than 30s for an element, 30s is the API response limit
setTimeout(() => {
observer.disconnect();
resolve();
}, maxTime);
});
},
eval: (code) => {
return new Promise((resolve) => {
if (typeof code !== 'string') {
LLActionsErrors.push('Type error: Code to evaluate must be a string.');
resolve();
return;
}
code = 'async function SB_go() {' + code + '} resolve(await SB_go());'
new LLAsyncFunction('resolve', code)(resolve)
})
},
};
// Data fetcher
function fetcher(LLPage) {
return new Promise(async (resolve) => {
// Default response
let response = {
url: LLPage.url,
headers: {},
html: '',
dom: '',
actions: {
err: [],
result: [],
},
xhr: [],
windowURL: LLPage.windowURL,
}
// Prepare HTTP headers for fetch request
let fetchOptions = {};
if (LLPage.headers && LLPage.headers.requestHeaders && LLPage.headers.requestHeaders.action) {
for (const header of LLPage.headers.action.requestHeaders) {
if (header.operation === 'set' || header.operation === 'append') {
fetchOptions['headers'][header.header] = header.value;
}
}
}
// Fetch original page HTML...
const fetchResponse = await fetch(LLPage.url, fetchOptions);
// ...if status code is 503, 403, re-fetch page
if ((fetchResponse.status === 503 || fetchResponse.status === 403) && !LLPage.hasOwnProperty('doNotReFetch')) {
response['reFetch'] = true;
resolve(response);
return;
}
// ...get original page HTML
response.html = await fetchResponse.text();
// ...get page HTTP headers
response.headers = Object.fromEntries(fetchResponse.headers.entries());
// Evaluate JS code and get the result (if any)
if (LLPage.actions) {
const resultArr = [];
for (const index in LLPage.actions) {
for (const key in LLPage.actions[index]) {
const result = await LLActions[key](LLPage.actions[index][key]);
if (result) {
resultArr.push(result);
}
}
}
response.actions.result = resultArr;
response.actions.err = LLActionsErrors;
}
if (LLActionsScraped.length > 0) {
// Get scraped data with actions
response.dom = LLActionsScraped;
} else {
// Get original page DOM
if (document.doctype) {
response.dom = new XMLSerializer().serializeToString(document.doctype) + document.getElementsByTagName('html')[0].outerHTML;
} else {
response.dom = document.getElementsByTagName('html')[0].outerHTML;
}
}
// Perform XHR tasks (if any)
if (LLPage.xhr) {
for (const xhr of LLPage.xhr) {
const fetchResponse = await fetch(xhr.url, xhr.fetchOptions ? xhr.fetchOptions : {});
const fetchResponseText = await fetchResponse.text();
response.xhr.push({
'url': xhr.url,
'html': fetchResponseText
})
}
}
resolve(response);
});
}
// Fetch data from LLPage
fetcher(document.LLPage);