Skip to content

An agent skill for creating detailed, interactive isometric illustrations of real-world machines and complex systems. Generates polished SVGs with optional WebGL shaders and 3D effects.

capture.mjs: wait for the page to load before touching it, and time out unanswered Chrome calls

R.91886f5e87b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 890127b..8c9e657 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha
### Fixed
+- `capture.mjs` waits for the page to load before it touches it, even with `--wait 0`, and gives up with an error when Chrome leaves a request unanswered for 60 s instead of hanging.
- `drive.mjs` `until` times a condition inside the page, so one that is already true passes however short the wait, even on a slow machine; a promise that never settles still times out.
- Start headless Chrome on Windows from an elevated terminal: `capture.mjs`, `drive.mjs` and `turn-check.mjs` pass `--do-not-de-elevate` there. Chrome otherwise relaunches itself without the elevation, the process the script started exits with code 0, and the check stops with `Chrome exited before startup: 0` while the relaunched browser stays running.
- Resolve the current React readout target on every write so replaced readouts and anchors do not leave updates on detached elements.
diff --git a/scripts/capture.mjs b/scripts/capture.mjs
index b42717c..287eb47 100644
--- a/scripts/capture.mjs
+++ b/scripts/capture.mjs
@@ -127,8 +127,10 @@ async function main() {
let id = 0;
const pending = new Map();
const problems = [];
+ let loaded = () => {};
socket.onmessage = (message) => {
const data = JSON.parse(message.data);
+ if (data.method === "Page.loadEventFired") loaded();
if (data.id && pending.has(data.id)) {
pending.get(data.id)(data);
pending.delete(data.id);
@@ -141,9 +143,16 @@ async function main() {
};
await new Promise((done) => (socket.onopen = done));
const send = (method, params = {}) =>
- new Promise((done) => {
+ new Promise((done, fail) => {
const call = ++id;
- pending.set(call, done);
+ const timer = setTimeout(() => {
+ pending.delete(call);
+ fail(new Error(`Chrome did not answer ${method} within 60 s`));
+ }, 60000);
+ pending.set(call, (data) => {
+ clearTimeout(timer);
+ done(data);
+ });
socket.send(JSON.stringify({ id: call, method, params }));
});
const evaluate = async (expression) => (await send("Runtime.evaluate", { expression, awaitPromise: true, returnByValue: true })).result?.result?.value;
@@ -155,9 +164,14 @@ async function main() {
if (options.theme) features.push({ name: "prefers-color-scheme", value: options.theme });
if (features.length) await send("Emulation.setEmulatedMedia", { features });
await send("Emulation.setDeviceMetricsOverride", { width: options.width, height: options.height, deviceScaleFactor: options.scale, mobile: options.width < 600 });
+ const load = new Promise((done) => {
+ loaded = done;
+ setTimeout(done, 10000);
+ });
const navigation = await send("Page.navigate", { url });
if (navigation.error) throw new Error(`Navigation failed: ${navigation.error.message}`);
if (navigation.result?.errorText) throw new Error(`Navigation failed: ${navigation.result.errorText}`);
+ await load;
await sleep(Math.min(options.wait, 1500));
await evaluate(`(async()=>{const h=document.documentElement.scrollHeight;for(let y=0;y<h;y+=400){scrollTo(0,y);await new Promise(r=>setTimeout(r,60))}scrollTo(0,0)})()`);
await sleep(Math.max(0, options.wait - 1500));