Poll an evaluated expression until it returns truthy (or time out → null).
(client, expression, timeoutMs = 4000, intervalMs = 80)
| 142 | } |
| 143 | /** Poll an evaluated expression until it returns truthy (or time out → null). */ |
| 144 | async function until(client, expression, timeoutMs = 4000, intervalMs = 80) { |
| 145 | const deadline = Date.now() + timeoutMs |
| 146 | while (Date.now() < deadline) { |
| 147 | const v = await evaluate(client, expression) |
| 148 | if (v) return v |
| 149 | await sleep(intervalMs) |
| 150 | } |
| 151 | return null |
| 152 | } |
| 153 | function pressKey(client, key, shift = false) { |
| 154 | const code = key.length === 1 ? `Key${key.toUpperCase()}` : key |
| 155 | return evaluate(client, `(() => { window.dispatchEvent(new KeyboardEvent('keydown', { key: ${JSON.stringify(key)}, code: ${JSON.stringify(code)}, shiftKey: ${shift}, bubbles: true, cancelable: true })); return true; })()`) |