-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.js
61 lines (55 loc) · 1.42 KB
/
browser.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
import { Page } from "puppeteer";
import { genUrl } from "./url.js";
import puppeteer from "puppeteer-extra";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
export class HTMLBrowser {
constructor() {
this.browserAsync = puppeteer.use(StealthPlugin()).launch();
}
/**
* Finishes constructing the object.
*
* @returns {HTMLBrowser} This object
*/
async make() {
this.browser = await this.browserAsync;
return this;
}
/**
* Creates a page.
*
* @returns {Page} The created page
*/
async createPage() {
const page = await this.browser.newPage();
await page.setRequestInterception(true);
page.on("request", req => {
if (!["document", "xhr", "fetch", "script"].includes(req.resourceType())) {
return req.abort();
}
req.continue();
});
return page;
}
/**
* Closes the browser.
*/
async close() {
this.page.close();
this.browser.close();
}
}
/**
* GETs data
*
* @param {Page} page The page to use
* @param {string} path The page path
* @returns {Buffer} Binary data
*/
export async function get(page, path) {
const url = genUrl(path);
await page.bringToFront();
await page.goto(url);
await page.waitForFunction(() => document.readyState === "complete");
return await page.content();
}