|
1 | 1 | const utils = require('../helpers/utils') |
| 2 | +const exceptions = require("../helpers/exceptions"); |
2 | 3 |
|
3 | 4 | const DEFAULT_TIMEOUT = 1000; // 1 second |
4 | 5 |
|
5 | | -/* |
6 | | - * This module introduces new ability to puppeteer-service. |
7 | | - * It is capable of solving recaptchas on a given web-page. |
8 | | - * If there is no recaptcha on the page nothing bad will happen. |
9 | | - * If there is recaptcha it solves it and then inserts the special code |
| 6 | +/** |
| 7 | + * The function solves recaptchas on the page. |
| 8 | + * If there is no recaptcha on the page nothing will happen. |
| 9 | + * If there is a recaptcha the function solves it and then inserts the special code |
10 | 10 | * into the page automatically. |
11 | 11 | * |
12 | 12 | * Returns useful information about recaptcha_solving. |
13 | 13 | * For more information about return value visit |
14 | 14 | * https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin-recaptcha#result-object |
15 | 15 | */ |
16 | 16 | exports.recaptchaSolver = async function recaptchaSolver(page, request) { |
17 | | - let recaptcha_data; |
| 17 | + if (!("solve_recaptcha" in request.body)) { |
| 18 | + throw new exceptions.IncorrectArgumentError("No solve_recaptcha parameter in request"); |
| 19 | + } |
| 20 | + |
| 21 | + let recaptchaData; |
18 | 22 |
|
19 | 23 | if (request.body.solve_recaptcha) { |
20 | | - recaptcha_data = await page.solveRecaptchas(); |
| 24 | + recaptchaData = await page.solveRecaptchas(); |
21 | 25 | } else { |
22 | | - recaptcha_data = await page.findRecaptchas(); |
| 26 | + recaptchaData = await page.findRecaptchas(); |
23 | 27 | } |
24 | 28 |
|
25 | 29 | const waitOptions = request.body.waitOptions || { timeout: DEFAULT_TIMEOUT }; |
26 | 30 | const contents = await utils.getContents(page, waitOptions); |
27 | 31 |
|
28 | 32 | if (request.query.closePage || |
29 | | - (request.body.close_on_empty && recaptcha_data['captchas'].length === 0)) { |
| 33 | + (request.body.close_on_empty && recaptchaData['captchas'].length === 0)) { |
30 | 34 | await page.close(); |
31 | 35 | } |
32 | 36 |
|
33 | 37 | return { |
34 | 38 | ...contents, |
35 | | - recaptcha_data: recaptcha_data, |
| 39 | + recaptcha_data: recaptchaData, |
36 | 40 | } |
37 | 41 | } |
0 commit comments