Files
FamReynaBrain/exports/emi_astra_export.py
2026-09-22 23:00:26 -04:00

28 lines
2.0 KiB
Python

"""Export the exact deck tab already opened by agent-browser hermes-visible.
Connects only to the existing canonical Chrome, never launches a browser.
"""
from pathlib import Path
import asyncio, urllib.request, json, base64
import websockets
O=Path('/Users/adolforeyna/brain/exports')
async def main():
tabs=json.load(urllib.request.urlopen('http://127.0.0.1:9222/json'))
tab=next(t for t in tabs if t.get('url')=='file:///Users/adolforeyna/brain/exports/emi_astra_family_that_remains.html')
async with websockets.connect(tab['webSocketDebuggerUrl'],max_size=40_000_000) as ws:
seq=0
async def call(method,params={}):
nonlocal seq
seq+=1; await ws.send(json.dumps({'id':seq,'method':method,'params':params}))
while True:
data=json.loads(await ws.recv())
if data.get('id')==seq:
if 'error' in data:raise RuntimeError(data['error'])
return data.get('result',{})
await call('Runtime.evaluate',{'expression':'document.fonts.ready.then(()=>true)','awaitPromise':True})
bounds=await call('Runtime.evaluate',{'expression':'''JSON.stringify([...document.querySelectorAll('.slide')].map(s=>{const r=s.getBoundingClientRect();return {slide:s.id,overflow:[...s.querySelectorAll('h1,h2,h3,p,li,.smalltag,.source')].filter(e=>{const b=e.getBoundingClientRect();return b.left<r.left||b.right>r.right||b.top<r.top||b.bottom>r.bottom-65}).map(e=>e.textContent)}}))''','returnByValue':True})
(O/'emi_astra_dom_bounds.json').write_text(bounds['result']['value'])
pdf=await call('Page.printToPDF',{'printBackground':True,'preferCSSPageSize':True,'paperWidth':20,'paperHeight':11.25,'marginTop':0,'marginBottom':0,'marginLeft':0,'marginRight':0,'displayHeaderFooter':False})
(O/'emi_astra_family_that_remains.pdf').write_bytes(base64.b64decode(pdf['data']))
print('Exported widescreen PDF with CSS page size; DOM bounds:',bounds['result']['value'])
asyncio.run(main())