← Commit history

URL demo: retry window-capture so it grabs the real browser once it loads

John Lauer ·12fe56383d ·2mo ago ·parent 4e86de6
1 file changed +33−18
web-control-demo.html+33−18
@@ -238,24 +238,39 @@ for (const w of mine) {   // This is the parent/child window array - the real "state of the union" of the app.   function captureWindows(c, spec) {     clog(c, "Capturing the app's full window state (all its windows) ...");-    return windowsList().then(function (wins) {-      var inc = (spec.include || []).map(function (s) { return s.toLowerCase(); });-      var exc = (spec.exclude || []).map(function (s) { return s.toLowerCase(); });-      var match = wins.filter(function (w) { var t = (w.title || "").toLowerCase(); return inc.some(function (p) { return t.indexOf(p) >= 0; }) && !exc.some(function (p) { return t.indexOf(p) >= 0; }); });-      if (!match.length) { clog(c, "  no windows matched - is the app actually open?"); setRes(c.res, "err", "no windows to capture", "the app may not have opened"); return; }-      clog(c, "  found " + match.length + " window(s): " + match.map(function (w) { return '"' + (w.title || "").slice(0, 42) + '"'; }).join(", "));-      var i = 0;-      function next() {-        if (i >= match.length) { setRes(c.res, "ok", "Done - full state below", match.length + " window(s)"); clog(c, "DONE - captured the app's full window state (" + match.length + " windows)."); return; }-        var w = match[i++];-        return postSelf("desktop_screenshot_base64", { hwnd: w.hwnd, maxWidth: 900 }).then(function (sr) {-          if (sr.s === 200 && sr.j.image) { clog(c, "  captured window " + i + "/" + match.length + ': "' + (w.title || "").slice(0, 50) + '" ' + sr.j.width + "x" + sr.j.height); addShot(c.card, sr.j.image, w.title || "window"); }-          else { clog(c, "  could not capture: \"" + (w.title || "") + "\""); }-          return next();-        });-      }-      return next();-    });+    var inc = (spec.include || []).map(function (s) { return s.toLowerCase(); });+    var exc = (spec.exclude || []).map(function (s) { return s.toLowerCase(); });+    // A just-opened window (esp. a browser) may still be LOADING - its title is+    // not the page title yet. So poll a few seconds before giving up.+    var attempts = 0, maxAttempts = 8;+    function findMatches() {+      return windowsList().then(function (wins) {+        return wins.filter(function (w) { var t = (w.title || "").toLowerCase(); return inc.some(function (p) { return t.indexOf(p) >= 0; }) && !exc.some(function (p) { return t.indexOf(p) >= 0; }); });+      });+    }+    function attempt() {+      attempts++;+      return findMatches().then(function (match) {+        if (!match.length) {+          if (attempts < maxAttempts) { clog(c, "  [" + attempts + "] no matching window yet - waiting for it to finish loading ..."); return new Promise(function (r) { setTimeout(r, 1200); }).then(attempt); }+          clog(c, "  no windows matched after " + attempts + " tries - is the app actually open?"); setRes(c.res, "err", "no windows to capture", "the app may not have opened, or its title never matched");+          return;+        }+        clog(c, "  found " + match.length + " window(s): " + match.map(function (w) { return '"' + (w.title || "").slice(0, 42) + '"'; }).join(", "));+        var i = 0;+        function next() {+          if (i >= match.length) { setRes(c.res, "ok", "Done - full state below", match.length + " window(s)"); clog(c, "DONE - captured the app's full window state (" + match.length + " windows)."); return; }+          var w = match[i++];+          return postSelf("desktop_screenshot_base64", { hwnd: w.hwnd, maxWidth: 900 }).then(function (sr) {+            if (sr.s === 200 && sr.j.image) { clog(c, "  captured window " + i + "/" + match.length + ': "' + (w.title || "").slice(0, 50) + '" ' + sr.j.width + "x" + sr.j.height); addShot(c.card, sr.j.image, w.title || "window"); }+            else { clog(c, "  could not capture: \"" + (w.title || "") + "\""); }+            return next();+          });+        }+        return next();+      });+    }+    return attempt();   }    // ---- SSE streaming (open -> wait for window), then capture the window array ----