|
|
|
|
|
const delay = ms => new Promise(res => setTimeout(res, ms)); |
|
|
|
|
|
module.exports = async function(page, url, siteKey, action = 'submit') { |
|
|
return new Promise(async (resolve, reject) => { |
|
|
|
|
|
const timeoutTimer = setTimeout(() => reject(new Error("Timeout Global (60s)")), 60000); |
|
|
|
|
|
try { |
|
|
console.log(`[RV3] Processing: ${url} | Action: ${action}`); |
|
|
|
|
|
|
|
|
await page.setRequestInterception(false); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
await page.goto(url, { waitUntil: 'networkidle2', timeout: 30000 }); |
|
|
} catch (e) { |
|
|
console.log("[RV3] Navigation timeout, but proceeding..."); |
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
await page.mouse.move(100, 100); |
|
|
await page.mouse.move(200, 200, { steps: 5 }); |
|
|
} catch (e) {} |
|
|
|
|
|
|
|
|
const token = await page.evaluate(async (key, act) => { |
|
|
return new Promise(async (res, rej) => { |
|
|
|
|
|
const t = setTimeout(() => rej("Google Execute Timeout (30s)"), 30000); |
|
|
|
|
|
|
|
|
const waitForGrecaptcha = () => { |
|
|
return new Promise(resolve => { |
|
|
let attempts = 0; |
|
|
const check = setInterval(() => { |
|
|
attempts++; |
|
|
if (typeof window.grecaptcha !== 'undefined' && window.grecaptcha.execute) { |
|
|
clearInterval(check); |
|
|
resolve(true); |
|
|
} |
|
|
if (attempts > 20) { |
|
|
clearInterval(check); |
|
|
resolve(false); |
|
|
} |
|
|
}, 500); |
|
|
}); |
|
|
}; |
|
|
|
|
|
|
|
|
let ready = await waitForGrecaptcha(); |
|
|
|
|
|
if (!ready) { |
|
|
console.log("Injecting script manually..."); |
|
|
const script = document.createElement('script'); |
|
|
script.src = `https://www.google.com/recaptcha/api.js?render=${key}`; |
|
|
script.async = true; |
|
|
script.defer = true; |
|
|
document.head.appendChild(script); |
|
|
|
|
|
ready = await waitForGrecaptcha(); |
|
|
} |
|
|
|
|
|
if (!ready) { |
|
|
clearTimeout(t); |
|
|
rej("Failed to load grecaptcha library"); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
window.grecaptcha.ready(() => { |
|
|
window.grecaptcha.execute(key, { action: act }) |
|
|
.then(token => { |
|
|
clearTimeout(t); |
|
|
res(token); |
|
|
}) |
|
|
.catch(err => { |
|
|
clearTimeout(t); |
|
|
rej("Execution Error: " + (err.message || err)); |
|
|
}); |
|
|
}); |
|
|
} catch (e) { |
|
|
clearTimeout(t); |
|
|
rej("Critical Error: " + e.message); |
|
|
} |
|
|
}); |
|
|
}, siteKey, action); |
|
|
|
|
|
clearTimeout(timeoutTimer); |
|
|
|
|
|
if (token) { |
|
|
console.log(`[RV3] SUCCESS! Token length: ${token.length}`); |
|
|
resolve(token); |
|
|
} else { |
|
|
reject(new Error("Token returned empty")); |
|
|
} |
|
|
|
|
|
} catch (e) { |
|
|
clearTimeout(timeoutTimer); |
|
|
|
|
|
const msg = e.message.replace("Evaluation failed: ", ""); |
|
|
reject(new Error(msg)); |
|
|
} |
|
|
}); |
|
|
}; |