-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
87 lines (72 loc) · 2.81 KB
/
app.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require('fs');
const fsPromises = fs.promises;
const puppeteer = require('puppeteer');
const captchaUri = process.env.CAPTCHA_DOMAIN;
if (!captchaUri) {
throw('please provide URI for the CAPTCHA_DOMAIN env variable!');
}
(async () => {
// reduce chrome security by altering flags
const args = [
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process',
'--window-size=1920,1080',
'--start-fullscreen',
];
const browser = await puppeteer.launch({ headless: true, args });
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:75.0) Gecko/20100101 Firefox/75.0');
await page.goto(captchaUri);
// increase this delay time if your connection is not good
await page.waitFor(1000);
// get all iframes
const frames = page.frames();
const challengeFrame = frames[1];
const checkBoxFrame = frames[2];
// find and click checkbox to start
await checkBoxFrame.hover('#checkbox');
await checkBoxFrame.click('#checkbox');
// increase this delay time if your connection is not good
await page.waitFor(1000);
// get hcaptcha spec
const element = await challengeFrame.$('.prompt-text');
const searchText = await challengeFrame.evaluate(element => element.textContent, element);
const wordsArray = searchText.split(' ');
const [keyword] = wordsArray.slice(-1);
console.log('Keyword is: ', keyword);
// get the challenge images (only first page)
const images = await challengeFrame.$$('.task-grid .task-image .image');
console.log(`${images.length} images found!`);
// get the images count in folders
const imageFiles = await fsPromises.readdir('images');
const ssFiles = await fsPromises.readdir('ss');
let imageCounter = imageFiles.length - 1;
let ssCounter = ssFiles.length;
// url extraction regex
const urlRegex = /^url\("(.*)"\)$/;
for (let image of images) {
const backgroundImage = await challengeFrame.evaluate(image => image.style['background-image'], image);
const matches = backgroundImage.match(urlRegex);
if (matches.length > 0) {
const url = matches[matches.length - 1];
if (url) {
// create a new tab
const newTab = await browser.newPage();
const imageSource = await newTab.goto(url);
const buffer = await imageSource.buffer();
// save image to the file
++imageCounter;
await fsPromises.writeFile(`images/${imageCounter}.jpg`, buffer);
console.log(`${imageCounter} saved`);
// close tab
await newTab.close();
}
}
}
// save screenshot to the
++ssCounter;
await page.screenshot({ path: `ss/${ssCounter}.png`, fullPage: true });
console.log(`Screenshot ${ssCounter} saved`);
await browser.close();
})();