| 139 | } |
| 140 | |
| 141 | async function handleGetToken(data) { |
| 142 | let newTabId = null; |
| 143 | try { |
| 144 | console.log("[Flow2API] Auto-opening fresh Google Labs tab to avoid token expiry..."); |
| 145 | const newTab = await chrome.tabs.create({ url: "https://labs.google/fx/tools/flow", active: false }); |
| 146 | newTabId = newTab.id; |
| 147 | |
| 148 | await waitForTabReady(newTabId); |
| 149 | await sleep(1200); |
| 150 | |
| 151 | let successResponse = null; |
| 152 | let lastErrorMsg = "No response from tab."; |
| 153 | const scriptTimeoutMs = data.action === "VIDEO_GENERATION" ? 30000 : 20000; |
| 154 | |
| 155 | try { |
| 156 | const results = await chrome.scripting.executeScript({ |
| 157 | target: { tabId: newTabId }, |
| 158 | world: "MAIN", |
| 159 | func: async (action, timeoutMs) => { |
| 160 | return new Promise((resolve, reject) => { |
| 161 | let settled = false; |
| 162 | const finish = (fn, value) => { |
| 163 | if (settled) return; |
| 164 | settled = true; |
| 165 | fn(value); |
| 166 | }; |
| 167 | try { |
| 168 | function run() { |
| 169 | grecaptcha.enterprise.ready(function() { |
| 170 | grecaptcha.enterprise.execute("6LdsFiUsAAAAAIjVDZcuLhaHiDn5nnHVXVRQGeMV", { action: action }) |
| 171 | .then(token => finish(resolve, token)) |
| 172 | .catch(err => finish(reject, err.message || "reCAPTCHA evaluation failed internally")); |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | if (typeof grecaptcha !== "undefined" && grecaptcha.enterprise) { |
| 177 | run(); |
| 178 | } else { |
| 179 | const s = document.createElement("script"); |
| 180 | s.src = "https://www.google.com/recaptcha/enterprise.js?render=6LdsFiUsAAAAAIjVDZcuLhaHiDn5nnHVXVRQGeMV"; |
| 181 | s.onload = run; |
| 182 | s.onerror = () => finish(reject, "Failed to load enterprise.js via network"); |
| 183 | document.head.appendChild(s); |
| 184 | } |
| 185 | |
| 186 | setTimeout(() => finish(reject, "Timeout generating reCAPTCHA locally"), timeoutMs); |
| 187 | } catch (e) { |
| 188 | finish(reject, e.message); |
| 189 | } |
| 190 | }); |
| 191 | }, |
| 192 | args: [data.action || "IMAGE_GENERATION", scriptTimeoutMs] |
| 193 | }); |
| 194 | |
| 195 | if (results && results[0] && results[0].result) { |
| 196 | successResponse = { status: "success", token: results[0].result }; |
| 197 | } |
| 198 | } catch (e) { |