|
|
|
@ -68,100 +68,100 @@ class PuppeteerBrowser extends Browser {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async visit(url) {
|
|
|
|
|
let done = false;
|
|
|
|
|
let browser;
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
|
let done = false;
|
|
|
|
|
let browser;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
browser = await this.browser();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
reject(new Error(error.message || error.toString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
browser = await this.browser();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(error.message || error.toString());
|
|
|
|
|
}
|
|
|
|
|
browser.on('disconnected', () => {
|
|
|
|
|
if (!done) {
|
|
|
|
|
reject(new Error('Disconnected'));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
browser.on('disconnected', () => {
|
|
|
|
|
if (!done) {
|
|
|
|
|
throw new Error('Disconnected');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
page.setDefaultTimeout(this.options.maxWait);
|
|
|
|
|
|
|
|
|
|
page.setDefaultTimeout(this.options.maxWait);
|
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
|
|
|
|
|
|
await page.setRequestInterception(true);
|
|
|
|
|
page.on('error', reject);
|
|
|
|
|
|
|
|
|
|
page.on('error', (error) => {
|
|
|
|
|
throw new Error(error.message || error.toString());
|
|
|
|
|
});
|
|
|
|
|
page.on('request', request => request.continue());
|
|
|
|
|
|
|
|
|
|
page.on('request', request => request.continue());
|
|
|
|
|
page.on('response', (response) => {
|
|
|
|
|
if (response.status() === 301 || response.status() === 302) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page.on('response', (response) => {
|
|
|
|
|
if (response.status() === 301 || response.status() === 302) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!this.statusCode) {
|
|
|
|
|
this.statusCode = response.status();
|
|
|
|
|
|
|
|
|
|
if (!this.statusCode) {
|
|
|
|
|
this.statusCode = response.status();
|
|
|
|
|
this.headers = {};
|
|
|
|
|
|
|
|
|
|
this.headers = {};
|
|
|
|
|
const headers = response.headers();
|
|
|
|
|
|
|
|
|
|
const headers = response.headers();
|
|
|
|
|
Object.keys(headers).forEach((key) => {
|
|
|
|
|
this.headers[key] = Array.isArray(headers[key]) ? headers[key] : [headers[key]];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Object.keys(headers).forEach((key) => {
|
|
|
|
|
this.headers[key] = Array.isArray(headers[key]) ? headers[key] : [headers[key]];
|
|
|
|
|
});
|
|
|
|
|
this.contentType = headers['content-type'] || null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.contentType = headers['content-type'] || null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
page.on('console', ({ _type, _text, _location }) => this.log(`${_text} (${_location.url}: ${_location.lineNumber})`, _type));
|
|
|
|
|
|
|
|
|
|
page.on('console', ({ _type, _text, _location }) => this.log(`${_text} (${_location.url}: ${_location.lineNumber})`, _type));
|
|
|
|
|
await page.setUserAgent(this.options.userAgent);
|
|
|
|
|
|
|
|
|
|
await page.setUserAgent(this.options.userAgent);
|
|
|
|
|
await Promise.race([
|
|
|
|
|
page.goto(url, { waitUntil: 'networkidle2' }),
|
|
|
|
|
new Promise(_resolve => setTimeout(_resolve, this.options.maxWait)),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await Promise.race([
|
|
|
|
|
page.goto(url, { waitUntil: 'networkidle2' }),
|
|
|
|
|
new Promise(resolve => setTimeout(resolve, this.options.maxWait)),
|
|
|
|
|
]);
|
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
|
const links = await page.evaluateHandle(() => Array.from(document.getElementsByTagName('a')).map(({
|
|
|
|
|
hash, hostname, href, pathname, protocol, rel,
|
|
|
|
|
}) => ({
|
|
|
|
|
hash,
|
|
|
|
|
hostname,
|
|
|
|
|
href,
|
|
|
|
|
pathname,
|
|
|
|
|
protocol,
|
|
|
|
|
rel,
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
|
const links = await page.evaluateHandle(() => Array.from(document.getElementsByTagName('a')).map(({
|
|
|
|
|
hash, hostname, href, pathname, protocol, rel,
|
|
|
|
|
}) => ({
|
|
|
|
|
hash,
|
|
|
|
|
hostname,
|
|
|
|
|
href,
|
|
|
|
|
pathname,
|
|
|
|
|
protocol,
|
|
|
|
|
rel,
|
|
|
|
|
})));
|
|
|
|
|
|
|
|
|
|
this.links = await links.jsonValue();
|
|
|
|
|
this.links = await links.jsonValue();
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
|
const scripts = await page.evaluateHandle(() => Array.from(document.getElementsByTagName('script')).map(({
|
|
|
|
|
src,
|
|
|
|
|
}) => src));
|
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
|
const scripts = await page.evaluateHandle(() => Array.from(document.getElementsByTagName('script')).map(({
|
|
|
|
|
src,
|
|
|
|
|
}) => src));
|
|
|
|
|
|
|
|
|
|
this.scripts = (await scripts.jsonValue()).filter(script => script);
|
|
|
|
|
this.scripts = (await scripts.jsonValue()).filter(script => script);
|
|
|
|
|
|
|
|
|
|
this.js = await page.evaluate(getJs);
|
|
|
|
|
this.js = await page.evaluate(getJs);
|
|
|
|
|
|
|
|
|
|
this.cookies = (await page.cookies()).map(({
|
|
|
|
|
name, value, domain, path,
|
|
|
|
|
}) => ({
|
|
|
|
|
name, value, domain, path,
|
|
|
|
|
}));
|
|
|
|
|
this.cookies = (await page.cookies()).map(({
|
|
|
|
|
name, value, domain, path,
|
|
|
|
|
}) => ({
|
|
|
|
|
name, value, domain, path,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
this.html = await page.content();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(error.message || error.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
done = true;
|
|
|
|
|
this.html = await page.content();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
reject(error.message || error.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
done = true;
|
|
|
|
|
|
|
|
|
|
await browser.close();
|
|
|
|
|
}
|
|
|
|
|
await browser.close();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|