Workflow-App/Workflow/scripts/__test_capture.py

25 lines
1.3 KiB
Python
Raw Normal View History

import base64, subprocess, tempfile, json, os
AB = "C:\\Users\\SJ\\AppData\\Roaming\\npm\\agent-browser.cmd"
def run_ab(*args, timeout=30):
tmp = tempfile.NamedTemporaryFile(mode="w", delete=False, encoding="utf-8", errors="replace", prefix="ab_", suffix=".out")
tmp_path = tmp.name
tmp.close()
try:
result = subprocess.run([AB, *args], stdout=open(tmp_path, "w", encoding="utf-8", errors="replace"), stderr=subprocess.DEVNULL, timeout=timeout)
with open(tmp_path, "r", encoding="utf-8", errors="replace") as f:
out = f.read().strip()
finally:
try: os.unlink(tmp_path)
except OSError: pass
return out
# Single-line IIFE that returns JSON.stringify of { title, body, links }
js = """(() => { const ifr = document.querySelectorAll('iframe')[0]; const doc = ifr.contentDocument; const body = doc.body ? doc.body.innerText : ''; const links = Array.from(doc.querySelectorAll('a')).map(a => ({ text: a.textContent.trim().slice(0, 40), href: (a.href || '').slice(0, 100) })); return JSON.stringify({ title: doc.title, body: body.slice(0, 300), links: links.slice(0, 15) }); })()"""
b64 = base64.b64encode(js.encode("utf-8")).decode("ascii")
out = run_ab("eval", "-b", b64)
if out.startswith('"') and out.endswith('"'):
out = json.loads(out)
print(json.dumps(json.loads(out), indent=2))