# Driver for the old Nacre (Python, Qt WebEngine / Chromium), run inside it through its NACRE_SCRIPT dev hook, # which execs this file with `ctx` (the Nacre app object) and `app` in scope. Same steps as webkit_shell.py's # driver mode: for each line of ENGINE_PLAN, mark, open in a new tab, settle, check, screenshot, pause video; # then idle ENGINE_IDLE seconds and quit. Results go to ENGINE_OUT. import json import os import subprocess from PyQt6.QtCore import QTimer OUT = os.environ["ENGINE_OUT"] PLAN = [line.split() for line in open(os.environ["ENGINE_PLAN"]) if line.strip() and not line.startswith("#")] IDLE = int(os.environ.get("ENGINE_IDLE", "60")) CHECK_JS = """JSON.stringify({title: document.title, ready: document.readyState, url: location.href, nodes: document.getElementsByTagName('*').length, videos: [...document.querySelectorAll('video')].map(v => ({t: v.currentTime, paused: v.paused, rs: v.readyState, err: v.error && v.error.code, w: v.videoWidth}))})""" PLAY_JS = "var v = document.querySelector('video'); if (v && v.paused) { v.muted = true; v.play(); } 0" PAUSE_JS = "document.querySelectorAll('video').forEach(v => v.pause()); 0" def write(name, text): with open(os.path.join(OUT, name), "w") as f: f.write(text) def step(i): if i == len(PLAN): write("mark", "idle") QTimer.singleShot(IDLE * 1000, lambda: (write("mark", "done"), ctx.quit())) return label, url, settle = PLAN[i][0], PLAN[i][1], int(PLAN[i][2]) write("mark", label) w = ctx.browser_window() w.new_tab(url) tab = w.tab() if "play" in PLAN[i][3:]: QTimer.singleShot(10000, lambda: tab.page.runJavaScript(CHECK_JS, 0, lambda val: ( write(f"check-{label}-pre.json", val if isinstance(val, str) else repr(val)), tab.page.runJavaScript(PLAY_JS, 0)))) def got(val): write(f"check-{label}.json", val if isinstance(val, str) else json.dumps({"error": repr(val)})) subprocess.run(["grim", "-o", os.environ.get("ENGINE_OUTPUT", "DP-1"), os.path.join(OUT, f"{label}.png")]) tab.page.runJavaScript(PAUSE_JS, 0) QTimer.singleShot(1000, lambda: step(i + 1)) QTimer.singleShot(settle * 1000, lambda: tab.page.runJavaScript(CHECK_JS, 0, got)) QTimer.singleShot(3000, lambda: step(0))