(port)
| 123 | close() { this.ws?.terminate?.(); this.ws?.close() } |
| 124 | } |
| 125 | async function connectPage(port) { |
| 126 | const deadline = Date.now() + 20000 |
| 127 | let lastErr = null |
| 128 | while (Date.now() < deadline) { |
| 129 | try { |
| 130 | const targets = await httpGetJson(`http://127.0.0.1:${port}/json/list`) |
| 131 | const page = targets.find((t) => t.type === 'page' && t.webSocketDebuggerUrl && (String(t.url).startsWith('file:') || String(t.url).includes('index.html'))) |
| 132 | if (page) { const c = new Cdp(page.webSocketDebuggerUrl); await c.connect(); return c } |
| 133 | } catch (e) { lastErr = e } |
| 134 | await sleep(100) |
| 135 | } |
| 136 | throw new Error(`no CDP page: ${lastErr?.message ?? 'timeout'}`) |
| 137 | } |
| 138 | async function evaluate(client, expression) { |
| 139 | const r = await client.send('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true }) |
| 140 | if (r.exceptionDetails) throw new Error(r.exceptionDetails.exception?.description ?? r.exceptionDetails.text) |
no test coverage detected