1

This is the site where I learned the procedure to output the userAgent using bash on Ubuntu 22.04:

Get the "user agent" string from a new Google Chrome browser session from bash

and I had the command:

echo navigator.userAgent | /opt/google/chrome/chrome --headless --repl | perl -pe 's/^>>> //; s/HeadlessChrome/Chrome/g' | jq -r .result.value

Which worked perfectly until the version before Chrome 113, after which it gave only errors:

[0531/154816.789187:WARNING:bluez_dbus_manager.cc(247)] Floss manager not present, cannot set Floss enable/disable.
[0531/154816.807959:WARNING:sandbox_linux.cc(393)] InitializeSandbox() called with multiple threads in process gpu-process.
[0531/154816.812699:ERROR:command_buffer_proxy_impl.cc(128)] ContextResult::kTransientFailure: Failed to send GpuControl.CreateCommandBuffer.

Solved thanks to the link:

How to Supress "Floss manager not present, cannot set Floss enable/disable."

But with this code, even if no more errors appear:

echo navigator.userAgent | /opt/google/chrome/chrome --headless=new --repl --remote-allow-origins=* | perl -pe 's/^>>> //; s/HeadlessChrome/Chrome/g' | jq -r .result.value

The command shell expects input but I've already given it through the pipe, which apparently didn't work.
How do I solve the problem?

Mario Palumbo
  • 693
  • 8
  • 32

1 Answers1

1

So I finally figured it out.

The new --headless mode does not have a --repl option and the old one is missing the "Floss manager", so the only option you have to access the User Agent is by setting up a --remote-debugging-port, like so:

$ /opt/google/chrome/chrome --headless=new --remote-debugging-port=9222

but now you can't get any data because all the commands need to be executed through websockets, you could use websocat or socat to do so but the debugging port also allows you to get some information just by accessing some HTTP endpoints.

One of the endpoints is /json/version which shows the the User-Agent property that you are looking for. Here is a short example:

$ /opt/google/chrome/chrome --headless=new --remote-debugging-port=9222 &>/dev/null &
$ PID=$! # save the PID from the previous command
$ curl http://localhost:9222/json/version --silent # | use jq to get the User-Agent
{
   "Browser": "Chrome/110.0.5481.77",
   "Protocol-Version": "1.3",
   "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
   "V8-Version": "11.0.226.13",
   "WebKit-Version": "537.36 (@65ed616c6e8ee3fe0ad64fe83796c020644d42af)",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/c2815764-ed1b-4d09-9d7e-db245159c235"
}
$ kill -SIGINT $PID # kill the process or otherwise you'll leave lock files behind.

Value from the older way: {"id":2,"result":{"result":{"type":"string","value":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/110.0.5481.77 Safari/537.36"}}}

This you would not need to replace "HeadlessChrome" to "Chrome".

Tested with Chrome 112 as well:

$ /opt/google/chrome/chrome --headless=new --remote-debugging-port=9222 &>/dev/null &
[1] 192
$ PID=$!
$ curl http://localhost:9222/json/version --silent
{
   "Browser": "Chrome/112.0.5615.49",
   "Protocol-Version": "1.3",
   "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
   "V8-Version": "11.2.214.9",
   "WebKit-Version": "537.36 (@bd2a7bcb881c11e8cfe3078709382934e3916914)",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/3318bf4f-344a-4130-8c0e-1e9ccc85a124"
}
$ kill -SIGINT $PID

:)