ChromeController icon indicating copy to clipboard operation
ChromeController copied to clipboard

Connect to an existing chrome instance

Open nielslaukens opened this issue 4 years ago • 4 comments

Hi,

I stumbled upon this project, and it looks pretty much what I need. Thank you for sharing your work!

I'm trying to figure out if/how I can connect to an already-running Chrome instance. From what I see in the code, it will always start a new chrome-instance itself.

(The Chrome-instance will be running on a different machine, so it's easier to start it first and connect later)

Thank you, Niels

nielslaukens avatar Jun 09 '21 15:06 nielslaukens

Hmmm, that's an interesting use-case I hadn't considered. Currently the transport manager is very heavily bound to running the chrome binary internally.

Let me see if I can separate the section responsible for running the chrome binary, and the websocket communications. That's a logical division anyways, and the fact that the current implementation kind of smears the two together isn't great.

fake-name avatar Jun 09 '21 19:06 fake-name

I currently worked around the problem by having ChromeController run a script that connects the given --remote-debugging-port=#### to the actual remote port. And thinking about it, that isn't a too bad solution after all.

nielslaukens avatar Jun 09 '21 19:06 nielslaukens

@nielslaukens - Any chance you could share your implementation either in a fork or as code pasted here in the issue? I started to follow your recommendation but figured it might be less error prone to merely ask for your code.

Dulani avatar Jul 16 '21 20:07 Dulani

I use the script below as follows:

chrome_args = [f"--remote={ip}:{args.port}"]
with ChromeController.ChromeContext(binary="./remote-chrome.sh", additional_options=chrome_args) as cr:
    ...

The script itself is pretty simple: It parses out the --remote and --remote-debugging-port arguments (and ignores the rest) and uses socat (netcat would also work) to patch the connection through.

#!/bin/bash

while [ $# -gt 0 ]; do
  case "$1" in
    --remote-debugging-port=*)    PORT=${1##--remote-debugging-port=};;
    --remote=*) REMOTE=${1##--remote=};;
  esac;
  shift;
done

exec socat TCP-LISTEN:${PORT},fork,reuseaddr TCP:${REMOTE}

nielslaukens avatar Jul 19 '21 07:07 nielslaukens