refactor: 重命名Message.RawMsg
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// ScreenshotURL 截图函数:传入网址、输出路径、宽高、四个边距和等待的元素 ID
|
||||
func ScreenshotURL(url, output string, width, height int, marginTop, marginRight, marginBottom, marginLeft int, waitClass string) error {
|
||||
func ScreenshotURL(url, output string, width, height int, marginTop, marginBottom, marginRight, marginLeft int, waitClass string) error {
|
||||
// 创建一个上下文,连接到 Docker 中运行的 headless-shell 实例
|
||||
remoteAllocatorCtx, cancel := chromedp.NewRemoteAllocator(
|
||||
context.Background(), "ws://127.0.0.1:9222/json/ws",
|
||||
@@ -29,7 +29,7 @@ func ScreenshotURL(url, output string, width, height int, marginTop, marginRight
|
||||
defer cancel()
|
||||
|
||||
// 设置页面的宽高和缩放
|
||||
if err := chromedp.Run(ctx, setViewportAndUserAgent(width, height, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0")); err != nil {
|
||||
if err := chromedp.Run(ctx, setViewport(width, height)); err != nil {
|
||||
return fmt.Errorf("设置页面大小失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -45,6 +45,30 @@ func ScreenshotURL(url, output string, width, height int, marginTop, marginRight
|
||||
if waitClass == "" {
|
||||
queryAction = chromedp.WaitVisible(`body`, chromedp.ByQuery)
|
||||
}
|
||||
// 设置自定义请求头
|
||||
headers := map[string]interface{}{
|
||||
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
||||
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
|
||||
"cache-control": "max-age=0",
|
||||
"priority": "u=0, i",
|
||||
"sec-ch-ua": `"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"`,
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": `"Windows"`,
|
||||
"sec-fetch-dest": "document",
|
||||
"sec-fetch-mode": "navigate",
|
||||
"sec-fetch-site": "same-origin",
|
||||
"sec-fetch-user": "?1",
|
||||
"upgrade-insecure-requests": "1",
|
||||
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
|
||||
}
|
||||
|
||||
// 启用网络功能并设置请求头
|
||||
if err := chromedp.Run(ctx, network.Enable()); err != nil {
|
||||
return fmt.Errorf("启用网络失败: %w", err)
|
||||
}
|
||||
if err := chromedp.Run(ctx, network.SetExtraHTTPHeaders(network.Headers(headers))); err != nil {
|
||||
return fmt.Errorf("设置请求头失败: %w", err)
|
||||
}
|
||||
|
||||
// 执行任务:打开网页并截图
|
||||
err := chromedp.Run(ctx,
|
||||
@@ -77,14 +101,13 @@ func ScreenshotURL(url, output string, width, height int, marginTop, marginRight
|
||||
return nil
|
||||
}
|
||||
|
||||
func setViewportAndUserAgent(width, height int, userAgent string) chromedp.Tasks {
|
||||
func setViewport(width, height int) chromedp.Tasks {
|
||||
return chromedp.Tasks{
|
||||
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false).
|
||||
WithScreenOrientation(&emulation.ScreenOrientation{
|
||||
Type: emulation.OrientationTypePortraitPrimary,
|
||||
Angle: 0,
|
||||
}),
|
||||
emulation.SetUserAgentOverride(userAgent),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user