首页 > 解决方案 > 使用 Chrome / Chromium 无头模式时如何获取地址栏?

问题描述

当我使用谷歌浏览器或 Chromium 的无头模式截屏时,只需要 HTML Body。

$ chromium --headless --disable-gpu --screenshot --window-size=1280,1080 https://stackoverflow.com/

为了方便,我想用地址栏截图...然后当我看到图片时,我从不混淆“我采取了哪个URL?”。

使用 Chrome/Chromium 无头模式时如何使用地址栏截屏?

标签: google-chromescreenshotchromiumheadless

解决方案


Chrome headless 没有 UI,它只完成了内存渲染,然后在页面加载到缓冲区后用于提供屏幕截图。所以没有办法让它脱离无头镀铬功能。

但这是其他方式。例如在 linux 上你可以使用类似的命令(ImageMagick 也应该在其他操作系统下工作):

convert screenshot.png -background black -fill white -splice 0x20 -annotate +15+15 'http://test.com' -gravity North -pointsize 30 screenshot2.png 

它使用提供的文本为图像添加标题。因此很容易将其包装到单个 shell 脚本中。就像是:

#/bin/bash

url=$1

google-chrome --headless --disable-gpu --screenshot "$url"

current_time=$(date "+%Y.%m.%d-%H.%M.%S")
new_fileName=screenshot_$current_time.png

convert screenshot.png -background black -fill white -splice 0x20 -annotate +15+15 $url -gravity North -pointsize 30 $new_fileName

并将其用作./take_screenshot.sh http://google.com


推荐阅读