import asyncio import json import websockets import requests async def hook_amplitude_events(): pages = requests.get("http://localhost:9222/json/list").json() ws_url = pages[0]["webSocketDebuggerUrl"] async with websockets.connect(ws_url, max_size=50*1024*1024) as ws: js = """ (() => { window.__liveAmplitudeEvents = window.__liveAmplitudeEvents || []; const inst = window.analyticsConnectorInstances && window.analyticsConnectorInstances['$default_instance']; if (inst && inst.eventBridge) { const origReceiver = inst.eventBridge.eventReceiver; inst.eventBridge.setEventReceiver((event) => { window.__liveAmplitudeEvents.push({ time: new Date().toISOString(), event: event }); console.log('>>> [AMPLITUDE EVENT CAPTURED]:', JSON.stringify(event)); if (origReceiver) origReceiver(event); }); } // Also monitor Yandex Metrika / ym calls if any const origYm = window.ym; if (typeof origYm === 'function') { window.ym = function(...args) { window.__liveAmplitudeEvents.push({ time: new Date().toISOString(), type: 'ym', args: args }); return origYm.apply(this, args); }; } return { hooked: true, existingEventsCount: window.__liveAmplitudeEvents.length }; })() """ await ws.send(json.dumps({ "id": 1, "method": "Runtime.evaluate", "params": { "expression": js, "returnByValue": True } })) res = json.loads(await ws.recv()) print("Hook setup result:", json.dumps(res.get("result", {}).get("result", {}).get("value", {}), indent=2, ensure_ascii=False)) if __name__ == "__main__": asyncio.run(hook_amplitude_events())