Skip to content

Commit b2038a0

Browse files
ono-maxko1
authored andcommitted
Support opening Chrome automatically on linux
1 parent daf8cae commit b2038a0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/debug/server_cdp.rb

+30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require 'open3'
99
require 'tmpdir'
1010
require 'tempfile'
11+
require 'timeout'
1112

1213
module DEBUGGER__
1314
module UI_CDP
@@ -67,6 +68,8 @@ def setup_chrome addr, uuid
6768
nil
6869
end
6970

71+
TIMEOUT_SEC = 5
72+
7073
def run_new_chrome
7174
path = CONFIG[:chrome_path]
7275

@@ -120,6 +123,33 @@ def run_new_chrome
120123
path = $2
121124
end
122125

126+
at_exit{
127+
DEBUGGER__.skip_all
128+
FileUtils.rm_rf dir
129+
}
130+
when /linux/
131+
path = path || 'google-chrome'
132+
dir = Dir.mktmpdir
133+
# The command line flags are based on: https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Chrome_Desktop#connecting.
134+
stdin, stdout, stderr, wait_thr = *Open3.popen3("#{path} --remote-debugging-port=0 --no-first-run --no-default-browser-check --user-data-dir=#{dir}")
135+
stdin.close
136+
stdout.close
137+
data = ''
138+
begin
139+
Timeout.timeout(TIMEOUT_SEC) do
140+
until data.match?(/DevTools listening on ws:\/\/127.0.0.1:\d+.*/)
141+
data = stderr.readpartial 4096
142+
end
143+
end
144+
rescue Exception
145+
raise NotFoundChromeEndpointError
146+
end
147+
stderr.close
148+
if data.match /DevTools listening on ws:\/\/127.0.0.1:(\d+)(.*)/
149+
port = $1
150+
path = $2
151+
end
152+
123153
at_exit{
124154
DEBUGGER__.skip_all
125155
FileUtils.rm_rf dir

0 commit comments

Comments
 (0)