文件

15 行
714 B
Python

import asyncio,json,urllib.request,websockets,itertools
_id=itertools.count(1)
async def open_ws():
tabs=json.load(urllib.request.urlopen('http://127.0.0.1:9333/json'))
page=[t for t in tabs if t['type']=='page'][0]
return await websockets.connect(page['webSocketDebuggerUrl'],max_size=50_000_000)
async def send(ws,method,params=None):
i=next(_id); await ws.send(json.dumps({'id':i,'method':method,'params':params or {}}))
while True:
m=json.loads(await ws.recv())
if m.get('id')==i: return m
async def ev(ws,expr):
r=await send(ws,'Runtime.evaluate',{'expression':expr,'awaitPromise':True,'returnByValue':True})
return r.get('result',{}).get('result',{}).get('value')