|
|
|
@ -410,8 +410,42 @@ class Driver {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
open(url, headers = {}) {
|
|
|
|
|
return new Site(url.split('#')[0], headers, this)
|
|
|
|
|
async open(url, headers = {}, storage = {}) {
|
|
|
|
|
const site = new Site(url.split('#')[0], headers, this)
|
|
|
|
|
|
|
|
|
|
if (storage.local || storage.session) {
|
|
|
|
|
this.log('Setting storage...')
|
|
|
|
|
|
|
|
|
|
const page = await site.newPage(site.originalUrl)
|
|
|
|
|
|
|
|
|
|
await page.setRequestInterception(true)
|
|
|
|
|
|
|
|
|
|
page.on('request', (request) =>
|
|
|
|
|
request.respond({
|
|
|
|
|
status: 200,
|
|
|
|
|
contentType: 'text/plain',
|
|
|
|
|
body: 'ok',
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await page.goto(url)
|
|
|
|
|
|
|
|
|
|
await page.evaluate((storage) => {
|
|
|
|
|
;['local', 'session'].forEach((type) => {
|
|
|
|
|
Object.keys(storage[type] || {}).forEach((key) => {
|
|
|
|
|
window[`${type}Storage`].setItem(key, storage[type][key])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}, storage)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await page.close()
|
|
|
|
|
} catch {
|
|
|
|
|
// Continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return site
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log(message, source = 'driver') {
|
|
|
|
@ -542,50 +576,10 @@ class Site {
|
|
|
|
|
status: 0,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!this.browser) {
|
|
|
|
|
await this.initDriver()
|
|
|
|
|
|
|
|
|
|
if (!this.browser) {
|
|
|
|
|
throw new Error('Browser closed')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let page
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
page = await this.browser.newPage()
|
|
|
|
|
|
|
|
|
|
if (!page || page.isClosed()) {
|
|
|
|
|
throw new Error('Page did not open')
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
error.message += ` (${url})`
|
|
|
|
|
|
|
|
|
|
this.error(error)
|
|
|
|
|
|
|
|
|
|
await this.initDriver()
|
|
|
|
|
|
|
|
|
|
page = await this.browser.newPage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.pages.push(page)
|
|
|
|
|
|
|
|
|
|
page.setJavaScriptEnabled(!this.options.noScripts)
|
|
|
|
|
|
|
|
|
|
page.setDefaultTimeout(this.options.maxWait)
|
|
|
|
|
const page = await this.newPage(url)
|
|
|
|
|
|
|
|
|
|
await page.setRequestInterception(true)
|
|
|
|
|
|
|
|
|
|
await page.setUserAgent(this.options.userAgent)
|
|
|
|
|
|
|
|
|
|
page.on('dialog', (dialog) => dialog.dismiss())
|
|
|
|
|
|
|
|
|
|
page.on('error', (error) => {
|
|
|
|
|
error.message += ` (${url})`
|
|
|
|
|
|
|
|
|
|
this.error(error)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
let responseReceived = false
|
|
|
|
|
|
|
|
|
|
page.on('request', async (request) => {
|
|
|
|
@ -1044,6 +1038,52 @@ class Site {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async newPage(url) {
|
|
|
|
|
if (!this.browser) {
|
|
|
|
|
await this.initDriver()
|
|
|
|
|
|
|
|
|
|
if (!this.browser) {
|
|
|
|
|
throw new Error('Browser closed')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let page
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
page = await this.browser.newPage()
|
|
|
|
|
|
|
|
|
|
if (!page || page.isClosed()) {
|
|
|
|
|
throw new Error('Page did not open')
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
error.message += ` (${url})`
|
|
|
|
|
|
|
|
|
|
this.error(error)
|
|
|
|
|
|
|
|
|
|
await this.initDriver()
|
|
|
|
|
|
|
|
|
|
page = await this.browser.newPage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.pages.push(page)
|
|
|
|
|
|
|
|
|
|
page.setJavaScriptEnabled(!this.options.noScripts)
|
|
|
|
|
|
|
|
|
|
page.setDefaultTimeout(this.options.maxWait)
|
|
|
|
|
|
|
|
|
|
await page.setUserAgent(this.options.userAgent)
|
|
|
|
|
|
|
|
|
|
page.on('dialog', (dialog) => dialog.dismiss())
|
|
|
|
|
|
|
|
|
|
page.on('error', (error) => {
|
|
|
|
|
error.message += ` (${url})`
|
|
|
|
|
|
|
|
|
|
this.error(error)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return page
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async analyze(url = this.originalUrl, index = 1, depth = 1) {
|
|
|
|
|
if (this.options.recursive) {
|
|
|
|
|
await sleep(this.options.delay * index)
|
|
|
|
|