Skip to content

Commit

Permalink
updated hightech use newpagewithcontext, updated browser optimized page
Browse files Browse the repository at this point in the history
  • Loading branch information
gegehprast committed Jan 18, 2020
1 parent aed07c0 commit cf91a7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
47 changes: 24 additions & 23 deletions Browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,22 @@ class Browser {
async newOptimizedPage() {
const page = await this.browser.newPage()
page.setDefaultTimeout(30000)
await page.setRequestInterception(true)

page.on('request', (req) => {
if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
req.abort()
} else {
req.continue()
}
})

return page
return await this.optimizePage(page)
}

/**
* Create new page with different browser context
* to support multiple sessions.
* to support multiple sessions. The returned page will be optimized.
* https://github.com/GoogleChrome/puppeteer/issues/85
*/
async newPageWithNewContext() {
const { browserContextId } = await this.browser._connection.send('Target.createBrowserContext')
const page = await this.browser._createPageInContext(browserContextId)
page.browserContextId = browserContextId
page.setDefaultTimeout(60000)

page.on('request', (req) => {
if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
req.abort()
} else {
req.continue()
}
})

return page
page.setDefaultTimeout(30000)

return await this.optimizePage(page)
}

/**
Expand All @@ -68,8 +51,26 @@ class Browser {
const pageTarget = page.target()
const newTarget = await this.browser.waitForTarget(target => target.opener() === pageTarget)
const newPage = await newTarget.page()
newPage.setDefaultTimeout(30000)

return await this.optimizePage(newPage)
}

/**
* Optimize a page.
*/
async optimizePage(page) {
await page.setRequestInterception(true)

return newPage
page.on('request', (req) => {
if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
req.abort()
} else {
req.continue()
}
})

return page
}

/**
Expand Down
2 changes: 1 addition & 1 deletion shortlinks/Hightech.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Hightech {
}
}

const page = await Browser.newOptimizedPage()
const page = await Browser.newPageWithNewContext()

try {
await page.goto(link)
Expand Down

0 comments on commit cf91a7c

Please sign in to comment.