har_capture_cdp.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python3
  2. """Capture a HAR from a browser you connect to over CDP (not one you launch).
  3. Use this when the browser is owned by someone else and only reachable over the
  4. Chrome DevTools Protocol: Hermes cloud backends (Browserbase, Browser-Use,
  5. Firecrawl), or anything wired via
  6. `/browser connect <url>` / BROWSER_CDP_URL / browser.cdp_url in config.
  7. Why this exists: Playwright's record_har_path only works on a context you
  8. launched locally. connect_over_cdp() attaches to an existing browser, so
  9. record_har is unavailable — we assemble the HAR from CDP Network.* events
  10. ourselves via page.on("request"/"response").
  11. Usage:
  12. python3 har_capture_cdp.py <cdp_url> <output.har> [--wait S] \
  13. [--goto URL] [--action "fill:SEL:TEXT"] [--action "click:SEL"] ...
  14. <cdp_url> is the ws:// or http:// CDP endpoint. For Hermes: run
  15. `/browser connect` to see the active endpoint, or read BROWSER_CDP_URL.
  16. """
  17. import argparse
  18. import base64
  19. import json
  20. import sys
  21. import time
  22. from playwright.sync_api import sync_playwright
  23. def run_action(page, spec: str) -> None:
  24. parts = spec.split(":", 2)
  25. kind = parts[0]
  26. if kind == "fill":
  27. page.fill(parts[1], parts[2])
  28. elif kind == "press":
  29. page.press(parts[1], parts[2])
  30. elif kind == "click":
  31. page.click(parts[1])
  32. elif kind == "goto":
  33. page.goto(parts[1] + (":" + parts[2] if len(parts) > 2 else ""))
  34. elif kind == "sleep":
  35. time.sleep(float(parts[1]))
  36. else:
  37. raise ValueError(f"unknown action: {spec}")
  38. def _har_entry(req, resp):
  39. """Build a minimal HAR entry from a Playwright request/response pair."""
  40. body_text, encoding = "", ""
  41. if resp is not None:
  42. try:
  43. raw = resp.body()
  44. try:
  45. body_text = raw.decode("utf-8")
  46. except UnicodeDecodeError:
  47. body_text = base64.b64encode(raw).decode("ascii")
  48. encoding = "base64"
  49. except Exception:
  50. pass
  51. post = req.post_data
  52. return {
  53. "_resourceType": req.resource_type,
  54. "request": {
  55. "method": req.method,
  56. "url": req.url,
  57. "headers": [{"name": k, "value": v} for k, v in req.headers.items()],
  58. "queryString": [], # har_to_client.py re-parses the URL, so leave empty
  59. "postData": {"mimeType": req.headers.get("content-type", ""),
  60. "text": post} if post else {},
  61. },
  62. "response": {
  63. "status": resp.status if resp else 0,
  64. "headers": [{"name": k, "value": v} for k, v in (resp.headers.items() if resp else [])],
  65. "content": {
  66. "mimeType": (resp.headers.get("content-type", "") if resp else ""),
  67. "text": body_text,
  68. **({"encoding": encoding} if encoding else {}),
  69. },
  70. },
  71. }
  72. def main() -> int:
  73. ap = argparse.ArgumentParser()
  74. ap.add_argument("cdp_url")
  75. ap.add_argument("har_path")
  76. ap.add_argument("--goto", default=None, help="URL to navigate to after attaching")
  77. ap.add_argument("--wait", type=float, default=3.0)
  78. ap.add_argument("--action", action="append", default=[])
  79. args = ap.parse_args()
  80. entries = []
  81. pending = {} # id(request) -> request
  82. with sync_playwright() as p:
  83. browser = p.chromium.connect_over_cdp(args.cdp_url)
  84. context = browser.contexts[0] if browser.contexts else browser.new_context()
  85. page = context.pages[0] if context.pages else context.new_page()
  86. def on_request(req):
  87. pending[id(req)] = req
  88. def on_response(resp):
  89. req = resp.request
  90. pending.pop(id(req), None)
  91. entries.append(_har_entry(req, resp))
  92. page.on("request", on_request)
  93. page.on("response", on_response)
  94. if args.goto:
  95. page.goto(args.goto, wait_until="domcontentloaded")
  96. for spec in args.action:
  97. run_action(page, spec)
  98. try:
  99. page.wait_for_load_state("networkidle", timeout=15000)
  100. except Exception:
  101. pass
  102. time.sleep(args.wait)
  103. page.remove_listener("request", on_request)
  104. page.remove_listener("response", on_response)
  105. # Do NOT close: we connected to someone else's browser.
  106. har = {"log": {"version": "1.2",
  107. "creator": {"name": "har_capture_cdp", "version": "0.1"},
  108. "entries": entries}}
  109. with open(args.har_path, "w", encoding="utf-8") as f:
  110. json.dump(har, f)
  111. print(f"HAR written: {args.har_path} ({len(entries)} entries)")
  112. return 0
  113. if __name__ == "__main__":
  114. sys.exit(main())