-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
66 lines (55 loc) · 2.08 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// index.js
import 'dotenv/config';
import { launch } from 'puppeteer'
import Captcha from '2captcha-ts';
import { readFileSync } from 'fs'
// Checking for APIKEY in .env
if (!process.env.APIKEY) {
console.error("APIKEY is not defined in .env file");
process.exit(1); // Terminate execution if key not found
}
const solver = new Captcha.Solver(process.env.APIKEY);
const target = 'URL where CAPTCHA occurs'
const example = async () => {
const browser = await launch({
headless: false,
devtools: true
})
const [page] = await browser.pages()
const preloadFile = readFileSync('./inject.js', 'utf8')
await page.evaluateOnNewDocument(preloadFile)
// Here we intercept the console messages to catch the message logged by inject.js script
page.on('console', async (msg) => {
const txt = msg.text()
if (txt.includes('intercepted-params:')) {
const params = JSON.parse(txt.replace('intercepted-params:', ''))
const wafParams = {
pageurl: target,
sitekey: params.key,
iv: params.iv,
context: params.context,
challenge_script: params.challenge_script,
captcha_script: params.captcha_script
}
console.log(wafParams)
try {
console.log('Solving the captcha...')
const res = await solver.amazonWaf(wafParams)
console.log(`Solved the captcha ${res.id}`)
console.log(res)
console.log('Using the token...')
await page.evaluate(async (token) => {
await ChallengeScript.submitCaptcha(token);
window.location.reload ()
}, res.data.captcha_voucher);
} catch (e) {
console.log(e)
}
} else {
return
}
})
await page.goto(target)
// Additional code to interact with the page after captcha is solved might be here...
}
example()