Skip to content

Commit

Permalink
fix(plugin-recaptcha): Solve hcaptcha on newassets.hcaptcha.com (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
lagunovsky committed Feb 27, 2023
1 parent dccca38 commit dd27387
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
Expand Up @@ -16,7 +16,10 @@ export class HcaptchaContentScript {
private opts: types.ContentScriptOpts
private data: types.ContentScriptData

private baseUrl = 'assets.hcaptcha.com/captcha/v1/'
private baseUrls = [
'assets.hcaptcha.com/captcha/v1/',
'newassets.hcaptcha.com/captcha/v1/',
]

constructor(
opts = ContentScriptDefaultOpts,
Expand Down Expand Up @@ -57,15 +60,15 @@ export class HcaptchaContentScript {
/** Regular checkboxes */
private _findRegularCheckboxes() {
const nodeList = document.querySelectorAll<HTMLIFrameElement>(
`iframe[src*='${this.baseUrl}'][data-hcaptcha-widget-id]:not([src*='invisible'])`
this.baseUrls.map(url => `iframe[src*='${url}'][data-hcaptcha-widget-id]:not([src*='invisible'])`).join(',')
)
return Array.from(nodeList)
}

/** Find active challenges from invisible hcaptchas */
private _findActiveChallenges() {
const nodeList = document.querySelectorAll<HTMLIFrameElement>(
`div[style*='visible'] iframe[src*='${this.baseUrl}'][src*='hcaptcha.html']`
this.baseUrls.map(url => `div[style*='visible'] iframe[src*='${url}'][src*='hcaptcha.html']`).join(',')
)
return Array.from(nodeList)
}
Expand Down
66 changes: 39 additions & 27 deletions packages/puppeteer-extra-plugin-recaptcha/src/index.test.ts
Expand Up @@ -48,17 +48,23 @@ test('will detect hCAPTCHAs', async t => {
})
const page = await browser.newPage()

const url = 'https://democaptcha.com/demo-form-eng/hcaptcha.html'
await page.goto(url, { waitUntil: 'networkidle0' })
const urls = [
'https://accounts.hcaptcha.com/demo',
'http://democaptcha.com/demo-form-eng/hcaptcha.html',
]

const { captchas, error } = await (page as any).findRecaptchas()
t.is(error, null)
t.is(captchas.length, 1)
for (const url of urls) {
await page.goto(url, { waitUntil: 'networkidle0' })

const c = captchas[0]
t.is(c._vendor, 'hcaptcha')
t.is(c.url, url)
t.true(c.sitekey && c.sitekey.length > 5)
const { captchas, error } = await (page as any).findRecaptchas()
t.is(error, null)
t.is(captchas.length, 1)

const c = captchas[0]
t.is(c._vendor, 'hcaptcha')
t.is(c.url, url)
t.true(c.sitekey && c.sitekey.length > 5)
}

await browser.close()
})
Expand All @@ -74,24 +80,30 @@ test('will detect active hCAPTCHA challenges', async t => {
})
const page = await browser.newPage()

const url = 'https://democaptcha.com/demo-form-eng/hcaptcha.html'
await page.goto(url, { waitUntil: 'networkidle0' })
await page.evaluate(() => (window as any).hcaptcha.execute()) // trigger challenge popup
await page.waitForTimeout(2 * 1000)
await page.evaluate(() =>
document
.querySelector(`[data-hcaptcha-widget-id]:not([src*='invisible'])`)
.remove()
) // remove regular checkbox so we definitely test against the popup

const { captchas, error } = await (page as any).findRecaptchas()
t.is(error, null)
t.is(captchas.length, 1)

const c = captchas[0]
t.is(c._vendor, 'hcaptcha')
t.is(c.url, url)
t.true(c.sitekey && c.sitekey.length > 5)
const urls = [
'https://accounts.hcaptcha.com/demo',
'http://democaptcha.com/demo-form-eng/hcaptcha.html',
]

for (const url of urls) {
await page.goto(url, { waitUntil: 'networkidle0' })
await page.evaluate(() => (window as any).hcaptcha.execute()) // trigger challenge popup
await page.waitForTimeout(2 * 1000)
await page.evaluate(() =>
document
.querySelector(`[data-hcaptcha-widget-id]:not([src*='invisible'])`)
.remove()
) // remove regular checkbox so we definitely test against the popup

const { captchas, error } = await (page as any).findRecaptchas()
t.is(error, null)
t.is(captchas.length, 1)

const c = captchas[0]
t.is(c._vendor, 'hcaptcha')
t.is(c.url, url)
t.true(c.sitekey && c.sitekey.length > 5)
}

await browser.close()
})
Expand Down
30 changes: 18 additions & 12 deletions packages/puppeteer-extra-plugin-recaptcha/src/solve.test.ts
Expand Up @@ -67,18 +67,24 @@ test('will solve hCAPTCHAs', async t => {
})
const page = await browser.newPage()

const url = 'http://democaptcha.com/demo-form-eng/hcaptcha.html'
await page.goto(url, { waitUntil: 'networkidle0' })

const result = await (page as any).solveRecaptchas()
const { captchas, solutions, solved, error } = result
t.falsy(error)

t.is(captchas.length, 1)
t.is(solutions.length, 1)
t.is(solved.length, 1)
t.is(solved[0]._vendor, 'hcaptcha')
t.is(solved[0].isSolved, true)
const urls = [
'https://accounts.hcaptcha.com/demo',
'http://democaptcha.com/demo-form-eng/hcaptcha.html',
]

for (const url of urls) {
await page.goto(url, { waitUntil: 'networkidle0' })

const result = await (page as any).solveRecaptchas()
const { captchas, solutions, solved, error } = result
t.falsy(error)

t.is(captchas.length, 1)
t.is(solutions.length, 1)
t.is(solved.length, 1)
t.is(solved[0]._vendor, 'hcaptcha')
t.is(solved[0].isSolved, true)
}

await browser.close()
})
Expand Down

0 comments on commit dd27387

Please sign in to comment.