       (log/error "Exception in devtools WebSocket receive, msg: " msg
                  ", throwable: " t))))
 
-(defn- fetch-ws-debugger-url [host port]
-  (let [response @(http/get (str "http://" host ":" port "/json/list"))
-        url (some-> response :body parse-json first :web-socket-debugger-url)]
-    (when-not url
-      (throw (ex-info "No chrome remote debugging URL found"
-                      {:host host :port port
-                       :response response})))
-    url))
-
 (defn- wait-for-remote-debugging-port [host port max-wait-time-ms]
   (let [wait-until (+ (System/currentTimeMillis) max-wait-time-ms)
-        url (str "http://" host ":" port "/json/version")]
+        url        (str "http://" host ":" port "/json/version")]
     (loop [response @(http/head url)]
       (cond
         (= (:status response) 200)


        (> (System/currentTimeMillis) wait-until)
        (throw (ex-info "Chrome remote debugging port not up"
                        {:host host :port port
                         :max-wait-time-ms max-wait-time-ms}))

        :default
        (do (Thread/sleep 100)
            (recur @(http/head url)))))))

(defn connect
  "Connect to a running headless Chrome "
  ([] (connect "localhost" 9222))
  ([host port] (connect host port nil))
  ([host port max-wait-time-ms]
   (when max-wait-time-ms
     (wait-for-remote-debugging-port host port max-wait-time-ms))
   (let [client (ws/client)
         url (fetch-ws-debugger-url host port)

         ;; Request id to callback
         requests (atom {})

         ;; Event pub/sub channel
         event-chan (async/chan)
         event-pub (async/pub event-chan (juxt :domain :event))]

     ;; Configure max message size to 1mb (default 64kb is way too small)
     (doto (.getPolicy client)
       (.setMaxTextMessageSize (* 1024 1024)))

     (.start client)
     (->Connection (ws/connect url
                     :on-receive (partial on-receive requests event-chan)
                     :client client)
                   requests
                   event-chan
                   event-pub))))

(defn send-command [{requests :requests con :ws-connection} payload id callback]
  (swap! requests assoc id callback)

  (let [[domain event] (map (comp keyword camel->clojure)
                            (str/split (:method msg) #"\."))
        event {:domain domain
               :event  event
               :params (:params msg)}]
    (async/go
      (async/>! event-chan event))))
