Skip to content

Commit 19ccde1

Browse files
add custom header support for screenshot (#1785)
1 parent 842e59e commit 19ccde1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

runner/headless.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package runner
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"time"
78

89
"github.com/go-rod/rod"
@@ -98,11 +99,21 @@ func NewBrowser(proxy string, useLocal bool, optionalArgs map[string]string) (*B
9899
return engine, nil
99100
}
100101

101-
func (b *Browser) ScreenshotWithBody(url string, timeout time.Duration) ([]byte, string, error) {
102+
func (b *Browser) ScreenshotWithBody(url string, timeout time.Duration, headers []string) ([]byte, string, error) {
102103
page, err := b.engine.Page(proto.TargetCreateTarget{})
103104
if err != nil {
104105
return nil, "", err
105106
}
107+
for _, header := range headers {
108+
headerParts := strings.SplitN(header, ":", 2)
109+
if len(headerParts) != 2 {
110+
continue
111+
}
112+
key := strings.TrimSpace(headerParts[0])
113+
value := strings.TrimSpace(headerParts[1])
114+
_, _ = page.SetExtraHeaders([]string{key, value})
115+
}
116+
106117
page = page.Timeout(timeout)
107118
defer page.Close()
108119

runner/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,7 @@ retry:
21172117
var pHash uint64
21182118
if scanopts.Screenshot {
21192119
var err error
2120-
screenshotBytes, headlessBody, err = r.browser.ScreenshotWithBody(fullURL, time.Duration(scanopts.ScreenshotTimeout)*time.Second)
2120+
screenshotBytes, headlessBody, err = r.browser.ScreenshotWithBody(fullURL, time.Duration(scanopts.ScreenshotTimeout)*time.Second, r.options.CustomHeaders)
21212121
if err != nil {
21222122
gologger.Warning().Msgf("Could not take screenshot '%s': %s", fullURL, err)
21232123
} else {

0 commit comments

Comments
 (0)