testdriverai 7.2.79 → 7.2.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/captcha/solver.js +64 -2
- package/package.json +1 -1
package/lib/captcha/solver.js
CHANGED
|
@@ -28,6 +28,68 @@ function httpsGet(url) {
|
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Safely parse JSON response that may contain extra characters or multiple objects.
|
|
33
|
+
* The 2captcha API sometimes returns concatenated JSON or has trailing characters.
|
|
34
|
+
* @param {string} text - The response text to parse
|
|
35
|
+
* @returns {object} The parsed JSON object
|
|
36
|
+
*/
|
|
37
|
+
function safeParseJson(text) {
|
|
38
|
+
// Trim whitespace first
|
|
39
|
+
const trimmed = text.trim();
|
|
40
|
+
|
|
41
|
+
// Try standard parsing first
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(trimmed);
|
|
44
|
+
} catch {
|
|
45
|
+
// If standard parsing fails, try to extract the first valid JSON object
|
|
46
|
+
const jsonStart = trimmed.indexOf("{");
|
|
47
|
+
if (jsonStart === -1) {
|
|
48
|
+
throw new Error("No JSON object found in response: " + trimmed);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Find the matching closing brace by counting braces
|
|
52
|
+
let braceCount = 0;
|
|
53
|
+
let inString = false;
|
|
54
|
+
let escape = false;
|
|
55
|
+
|
|
56
|
+
for (let i = jsonStart; i < trimmed.length; i++) {
|
|
57
|
+
const char = trimmed[i];
|
|
58
|
+
|
|
59
|
+
if (escape) {
|
|
60
|
+
escape = false;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (char === "\\") {
|
|
65
|
+
escape = true;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (char === '"') {
|
|
70
|
+
inString = !inString;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!inString) {
|
|
75
|
+
if (char === "{") {
|
|
76
|
+
braceCount++;
|
|
77
|
+
} else if (char === "}") {
|
|
78
|
+
braceCount--;
|
|
79
|
+
if (braceCount === 0) {
|
|
80
|
+
// Found the end of the first complete JSON object
|
|
81
|
+
const jsonStr = trimmed.substring(jsonStart, i + 1);
|
|
82
|
+
return JSON.parse(jsonStr);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// If we get here, we couldn't find a complete JSON object
|
|
89
|
+
throw new Error("Invalid JSON in response: " + trimmed);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
31
93
|
// Auto-detection script that runs in the browser context
|
|
32
94
|
const detectCaptchaScript = `
|
|
33
95
|
(function() {
|
|
@@ -232,7 +294,7 @@ const checkSuccessScript = `(function() {
|
|
|
232
294
|
|
|
233
295
|
// Submit to 2captcha
|
|
234
296
|
console.log("SUBMITTING...");
|
|
235
|
-
const submitResp =
|
|
297
|
+
const submitResp = safeParseJson(await httpsGet(submitUrl));
|
|
236
298
|
if (submitResp.status !== 1) {
|
|
237
299
|
throw new Error("Submit failed: " + JSON.stringify(submitResp));
|
|
238
300
|
}
|
|
@@ -244,7 +306,7 @@ const checkSuccessScript = `(function() {
|
|
|
244
306
|
const maxAttempts = Math.ceil(config.timeout / config.pollInterval);
|
|
245
307
|
for (let i = 0; i < maxAttempts; i++) {
|
|
246
308
|
await sleep(config.pollInterval);
|
|
247
|
-
const resultResp =
|
|
309
|
+
const resultResp = safeParseJson(
|
|
248
310
|
await httpsGet(
|
|
249
311
|
"https://2captcha.com/res.php?key=" +
|
|
250
312
|
config.apiKey +
|