Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix applying custom cookies error and 'Failed to open browser' bug #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ async function interceptResponse(page: Page, callback: (payload: ChallengeResolu
});
}

async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy, download, returnOnlyCookies }: BaseRequestAPICall, page: Page): Promise<ChallengeResolutionT | void> {
async function resolveChallenge(ctx: RequestContext, {
url,
maxTimeout,
cookies,
proxy,
download,
returnOnlyCookies
}: BaseRequestAPICall, page: Page): Promise<ChallengeResolutionT | void> {
maxTimeout = maxTimeout || 60000
let status = 'ok'
let message = ''
Expand All @@ -129,6 +136,10 @@ async function resolveChallenge(ctx: RequestContext, { url, maxTimeout, proxy, d

log.debug(`Navigating to... ${url}`)
let response = await page.goto(url, { waitUntil: 'domcontentloaded' })
if (cookies) {
log.debug(`Setting custom cookies: ${JSON.stringify(cookies, null, 2)}`,)
await page.setCookie(...cookies)
}

// look for challenge
if (response.headers().server.startsWith('cloudflare')) {
Expand Down Expand Up @@ -344,10 +355,10 @@ async function setupPage(ctx: RequestContext, params: BaseRequestAPICall, browse
overrideResolvers.headers = request => Object.assign(request.headers(), headers)
}

if (cookies) {
log.debug(`Setting custom cookies: ${JSON.stringify(cookies, null, 2)}`,)
await page.setCookie(...cookies)
}
// if (cookies) {
// log.debug(`Setting custom cookies: ${JSON.stringify(cookies, null, 2)}`,)
// await page.setCookie(...cookies)
// }

// if any keys have been set on the object
if (Object.keys(overrideResolvers).length > 0) {
Expand Down
8 changes: 5 additions & 3 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ export default {
let launchTries = 3
let browser;

while (0 <= launchTries--) {
while (0 <= launchTries) {
try {
browser = await puppeteer.launch(puppeteerOptions)
break
} catch (e) {
if (e.message !== 'Failed to launch the browser process!')
throw e
log.error('puppeteer.launch error', e)
// if (e.message !== 'Failed to launch the browser process!')
// throw e
log.warn('Failed to open browser, trying again...')
}
launchTries--
}

if (!browser) { throw Error(`Failed to lanch browser 3 times in a row.`) }
Expand Down