自版本113起,userAgent不再适用于/opt/google/chrome/chrome

xxls0lw8  于 2023-03-06  发布在  Go
关注(0)|答案(1)|浏览(145)

这是我学习了在Ubuntu 22.04上使用bash输出userAgent的过程的站点:
Get the "user agent" string from a new Google Chrome browser session from bash
我得到了命令

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

直到Chrome 113之前的版本,它工作得很完美,之后它只给出错误:

[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.

感谢链接:
How to Supress "Floss manager not present, cannot set Floss enable/disable."
但是使用这段代码,即使没有更多的错误出现:

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

命令shell需要输入,但我已经通过管道给出了输入,这显然不起作用。
如何解决这个问题?

eyh26e7m

eyh26e7m1#

所以我终于想明白了。
新的--headless模式没有--repl选项,旧的模式缺少“Floss manager”,因此您访问User Agent的唯一选项是设置--remote-debugging-port,如下所示:

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

但是现在你不能获得任何数据,因为所有的命令都需要通过websockets执行,你可以使用websocatsocat来执行,但是调试端口也允许你通过访问一些HTTP端点来获得一些信息。
其中一个端点是/json/version,它显示了您正在查找的User-Agent属性。下面是一个简短的例子:

$ /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.

旧方法的值:{"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"}}}
您不需要将“HeadlessChrome”替换为“Chrome”。
在Chrome 112上也可以看到:

$ /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

:)

相关问题