Create endpoints/recaptchav3.js
Browse files- endpoints/recaptchav3.js +61 -0
endpoints/recaptchav3.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module.exports = async function(page, url, siteKey, action = 'submit') {
|
| 2 |
+
return new Promise(async (resolve, reject) => {
|
| 3 |
+
const timeout = setTimeout(() => reject(new Error("Timeout waiting for reCAPTCHA v3 token")), 30000);
|
| 4 |
+
|
| 5 |
+
try {
|
| 6 |
+
console.log(`[RV3] Processing: ${url} | Key: ${siteKey} | Action: ${action}`);
|
| 7 |
+
|
| 8 |
+
await page.setRequestInterception(true);
|
| 9 |
+
page.on('request', req => {
|
| 10 |
+
const type = req.resourceType();
|
| 11 |
+
if (['image', 'stylesheet', 'font', 'media'].includes(type)) {
|
| 12 |
+
req.abort();
|
| 13 |
+
} else {
|
| 14 |
+
req.continue();
|
| 15 |
+
}
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 30000 });
|
| 19 |
+
|
| 20 |
+
const token = await page.evaluate(async (key, act) => {
|
| 21 |
+
return new Promise((res, rej) => {
|
| 22 |
+
|
| 23 |
+
const runCaptcha = () => {
|
| 24 |
+
try {
|
| 25 |
+
grecaptcha.ready(() => {
|
| 26 |
+
grecaptcha.execute(key, { action: act })
|
| 27 |
+
.then(token => res(token))
|
| 28 |
+
.catch(err => rej("Execution Failed: " + err));
|
| 29 |
+
});
|
| 30 |
+
} catch (e) {
|
| 31 |
+
rej("Grecaptcha Error: " + e.message);
|
| 32 |
+
}
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
if (typeof grecaptcha !== 'undefined') {
|
| 36 |
+
runCaptcha();
|
| 37 |
+
} else {
|
| 38 |
+
const script = document.createElement('script');
|
| 39 |
+
script.src = `https://www.google.com/recaptcha/api.js?render=${key}`;
|
| 40 |
+
script.onload = runCaptcha;
|
| 41 |
+
script.onerror = () => rej("Failed to load Google API script");
|
| 42 |
+
document.head.appendChild(script);
|
| 43 |
+
}
|
| 44 |
+
});
|
| 45 |
+
}, siteKey, action);
|
| 46 |
+
|
| 47 |
+
clearTimeout(timeout);
|
| 48 |
+
|
| 49 |
+
if (token) {
|
| 50 |
+
console.log(`[RV3] Success! Token generated.`);
|
| 51 |
+
resolve(token);
|
| 52 |
+
} else {
|
| 53 |
+
reject(new Error("Empty token (Empty result)"));
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
} catch (e) {
|
| 57 |
+
clearTimeout(timeout);
|
| 58 |
+
reject(e);
|
| 59 |
+
}
|
| 60 |
+
});
|
| 61 |
+
};
|