| 1 | console.log("[Flow2API] Captcha Worker injected."); |
| 2 | |
| 3 | function getRecaptchaToken(action) { |
| 4 | return new Promise((resolve, reject) => { |
| 5 | const reqId = Date.now() + Math.random().toString(); |
| 6 | const script = document.createElement("script"); |
| 7 | script.textContent = ` |
| 8 | try { |
| 9 | function runCaptcha() { |
| 10 | grecaptcha.enterprise.ready(function() { |
| 11 | grecaptcha.enterprise.execute('6LdsFiUsAAAAAIjVDZcuLhaHiDn5nnHVXVRQGeMV', {action: '${action}'}) |
| 12 | .then(token => window.postMessage({type: 'reCAPTCHA_result', reqId: '${reqId}', token: token}, '*')) |
| 13 | .catch(err => window.postMessage({type: 'reCAPTCHA_error', reqId: '${reqId}', error: err.message}, '*')); |
| 14 | }); |
| 15 | } |
| 16 | |
| 17 | if (typeof grecaptcha !== "undefined" && grecaptcha.enterprise) { |
| 18 | runCaptcha(); |
| 19 | } else { |
| 20 | const rScript = document.createElement('script'); |
| 21 | rScript.src = "https://www.google.com/recaptcha/enterprise.js?render=6LdsFiUsAAAAAIjVDZcuLhaHiDn5nnHVXVRQGeMV"; |
| 22 | rScript.onload = () => { runCaptcha(); }; |
| 23 | rScript.onerror = () => { window.postMessage({type: 'reCAPTCHA_error', reqId: '${reqId}', error: 'Failed to load enterprise.js'}, '*'); }; |
| 24 | document.head.appendChild(rScript); |
| 25 | } |
| 26 | } catch (e) { |
| 27 | window.postMessage({type: 'reCAPTCHA_error', reqId: '${reqId}', error: e.message}, '*'); |
| 28 | } |
| 29 | `; |
| 30 | |
| 31 | const listener = (event) => { |
| 32 | if (event.source !== window || !event.data) return; |
| 33 | if (event.data.reqId === reqId) { |
| 34 | window.removeEventListener("message", listener); |
| 35 | script.remove(); |
| 36 | if (event.data.type === 'reCAPTCHA_result') { |
| 37 | resolve(event.data.token); |
| 38 | } else { |
| 39 | reject(new Error(event.data.error || "Unknown reCAPTCHA Error")); |
| 40 | } |
| 41 | } |
| 42 | }; |
| 43 | window.addEventListener("message", listener); |
| 44 | document.documentElement.appendChild(script); |
| 45 | |
| 46 | setTimeout(() => { |
| 47 | window.removeEventListener("message", listener); |
| 48 | script.remove(); |
| 49 | reject(new Error("Timeout generating reCAPTCHA")); |
| 50 | }, 15000); |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { |
| 55 | if (message.type === "get_token") { |