mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-18 21:22:15 +01:00
feat(chromium): add user agent override
This commit is contained in:
@@ -227,6 +227,7 @@ func (b *chromiumBrowser) pdf(ctx context.Context, logger *zap.Logger, url, outp
|
|||||||
clearCookiesActionFunc(logger, b.arguments.clearCookies),
|
clearCookiesActionFunc(logger, b.arguments.clearCookies),
|
||||||
disableJavaScriptActionFunc(logger, b.arguments.disableJavaScript),
|
disableJavaScriptActionFunc(logger, b.arguments.disableJavaScript),
|
||||||
setCookiesActionFunc(logger, options.Cookies),
|
setCookiesActionFunc(logger, options.Cookies),
|
||||||
|
userAgentOverride(logger, options.UserAgent),
|
||||||
extraHttpHeadersActionFunc(logger, options.ExtraHttpHeaders),
|
extraHttpHeadersActionFunc(logger, options.ExtraHttpHeaders),
|
||||||
navigateActionFunc(logger, url, options.SkipNetworkIdleEvent),
|
navigateActionFunc(logger, url, options.SkipNetworkIdleEvent),
|
||||||
hideDefaultWhiteBackgroundActionFunc(logger, options.OmitBackground, options.PrintBackground),
|
hideDefaultWhiteBackgroundActionFunc(logger, options.OmitBackground, options.PrintBackground),
|
||||||
@@ -250,6 +251,7 @@ func (b *chromiumBrowser) screenshot(ctx context.Context, logger *zap.Logger, ur
|
|||||||
clearCookiesActionFunc(logger, b.arguments.clearCookies),
|
clearCookiesActionFunc(logger, b.arguments.clearCookies),
|
||||||
disableJavaScriptActionFunc(logger, b.arguments.disableJavaScript),
|
disableJavaScriptActionFunc(logger, b.arguments.disableJavaScript),
|
||||||
setCookiesActionFunc(logger, options.Cookies),
|
setCookiesActionFunc(logger, options.Cookies),
|
||||||
|
userAgentOverride(logger, options.UserAgent),
|
||||||
extraHttpHeadersActionFunc(logger, options.ExtraHttpHeaders),
|
extraHttpHeadersActionFunc(logger, options.ExtraHttpHeaders),
|
||||||
navigateActionFunc(logger, url, options.SkipNetworkIdleEvent),
|
navigateActionFunc(logger, url, options.SkipNetworkIdleEvent),
|
||||||
hideDefaultWhiteBackgroundActionFunc(logger, options.OmitBackground, true),
|
hideDefaultWhiteBackgroundActionFunc(logger, options.OmitBackground, true),
|
||||||
|
|||||||
@@ -614,6 +614,41 @@ func TestChromiumBrowser_pdf(t *testing.T) {
|
|||||||
"set cookie",
|
"set cookie",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
scenario: "user agent override",
|
||||||
|
browser: newChromiumBrowser(
|
||||||
|
browserArguments{
|
||||||
|
binPath: os.Getenv("CHROMIUM_BIN_PATH"),
|
||||||
|
wsUrlReadTimeout: 5 * time.Second,
|
||||||
|
allowList: regexp2.MustCompile("", 0),
|
||||||
|
denyList: regexp2.MustCompile("", 0),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
fs: func() *gotenberg.FileSystem {
|
||||||
|
fs := gotenberg.NewFileSystem()
|
||||||
|
|
||||||
|
err := os.MkdirAll(fs.WorkingDirPath(), 0o755)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf(fmt.Sprintf("expected no error but got: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(fmt.Sprintf("%s/index.html", fs.WorkingDirPath()), []byte("<h1>User-Agent override</h1>"), 0o755)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected no error but got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
options: PdfOptions{
|
||||||
|
Options: Options{UserAgent: "foo"},
|
||||||
|
},
|
||||||
|
noDeadline: false,
|
||||||
|
start: true,
|
||||||
|
expectError: false,
|
||||||
|
expectedLogEntries: []string{
|
||||||
|
fmt.Sprintf("user agent override: foo"),
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
scenario: "extra HTTP headers",
|
scenario: "extra HTTP headers",
|
||||||
browser: newChromiumBrowser(
|
browser: newChromiumBrowser(
|
||||||
@@ -1990,6 +2025,7 @@ func TestChromiumBrowser_screenshot(t *testing.T) {
|
|||||||
"cache not cleared",
|
"cache not cleared",
|
||||||
"cookies not cleared",
|
"cookies not cleared",
|
||||||
"JavaScript not disabled",
|
"JavaScript not disabled",
|
||||||
|
"no user agent override",
|
||||||
"no extra HTTP headers",
|
"no extra HTTP headers",
|
||||||
"navigate to",
|
"navigate to",
|
||||||
"default white background not hidden",
|
"default white background not hidden",
|
||||||
@@ -2037,6 +2073,7 @@ func TestChromiumBrowser_screenshot(t *testing.T) {
|
|||||||
"cookies not cleared",
|
"cookies not cleared",
|
||||||
"JavaScript not disabled",
|
"JavaScript not disabled",
|
||||||
"no cookies to set",
|
"no cookies to set",
|
||||||
|
"no user agent override",
|
||||||
"no extra HTTP headers",
|
"no extra HTTP headers",
|
||||||
"navigate to",
|
"navigate to",
|
||||||
"default white background not hidden",
|
"default white background not hidden",
|
||||||
@@ -2084,6 +2121,7 @@ func TestChromiumBrowser_screenshot(t *testing.T) {
|
|||||||
"cookies not cleared",
|
"cookies not cleared",
|
||||||
"JavaScript not disabled",
|
"JavaScript not disabled",
|
||||||
"no cookies to set",
|
"no cookies to set",
|
||||||
|
"no user agent override",
|
||||||
"no extra HTTP headers",
|
"no extra HTTP headers",
|
||||||
"navigate to",
|
"navigate to",
|
||||||
"default white background not hidden",
|
"default white background not hidden",
|
||||||
|
|||||||
@@ -108,8 +108,12 @@ type Options struct {
|
|||||||
// Optional
|
// Optional
|
||||||
Cookies []Cookie
|
Cookies []Cookie
|
||||||
|
|
||||||
// ExtraHttpHeaders are the HTTP headers to send by Chromium while loading
|
// UserAgent overrides the default 'User-Agent' HTTP header.
|
||||||
// the HTML document.
|
// Optional.
|
||||||
|
UserAgent string
|
||||||
|
|
||||||
|
// ExtraHttpHeaders are extra HTTP headers to send by Chromium while
|
||||||
|
// loading he HTML document.
|
||||||
// Optional.
|
// Optional.
|
||||||
ExtraHttpHeaders map[string]string
|
ExtraHttpHeaders map[string]string
|
||||||
|
|
||||||
@@ -134,6 +138,7 @@ func DefaultOptions() Options {
|
|||||||
WaitWindowStatus: "",
|
WaitWindowStatus: "",
|
||||||
WaitForExpression: "",
|
WaitForExpression: "",
|
||||||
Cookies: nil,
|
Cookies: nil,
|
||||||
|
UserAgent: "",
|
||||||
ExtraHttpHeaders: nil,
|
ExtraHttpHeaders: nil,
|
||||||
EmulatedMediaType: "",
|
EmulatedMediaType: "",
|
||||||
OmitBackground: false,
|
OmitBackground: false,
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ func FormDataChromiumOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
waitWindowStatus string
|
waitWindowStatus string
|
||||||
waitForExpression string
|
waitForExpression string
|
||||||
cookies []Cookie
|
cookies []Cookie
|
||||||
|
userAgent string
|
||||||
extraHttpHeaders map[string]string
|
extraHttpHeaders map[string]string
|
||||||
emulatedMediaType string
|
emulatedMediaType string
|
||||||
omitBackground bool
|
omitBackground bool
|
||||||
@@ -78,6 +79,7 @@ func FormDataChromiumOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}).
|
}).
|
||||||
|
String("userAgent", &userAgent, defaultOptions.UserAgent).
|
||||||
Custom("extraHttpHeaders", func(value string) error {
|
Custom("extraHttpHeaders", func(value string) error {
|
||||||
if value == "" {
|
if value == "" {
|
||||||
extraHttpHeaders = defaultOptions.ExtraHttpHeaders
|
extraHttpHeaders = defaultOptions.ExtraHttpHeaders
|
||||||
@@ -115,6 +117,7 @@ func FormDataChromiumOptions(ctx *api.Context) (*api.FormData, Options) {
|
|||||||
WaitWindowStatus: waitWindowStatus,
|
WaitWindowStatus: waitWindowStatus,
|
||||||
WaitForExpression: waitForExpression,
|
WaitForExpression: waitForExpression,
|
||||||
Cookies: cookies,
|
Cookies: cookies,
|
||||||
|
UserAgent: userAgent,
|
||||||
ExtraHttpHeaders: extraHttpHeaders,
|
ExtraHttpHeaders: extraHttpHeaders,
|
||||||
EmulatedMediaType: emulatedMediaType,
|
EmulatedMediaType: emulatedMediaType,
|
||||||
OmitBackground: omitBackground,
|
OmitBackground: omitBackground,
|
||||||
|
|||||||
@@ -281,6 +281,23 @@ func setCookiesActionFunc(logger *zap.Logger, cookies []Cookie) chromedp.ActionF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func userAgentOverride(logger *zap.Logger, userAgent string) chromedp.ActionFunc {
|
||||||
|
return func(ctx context.Context) error {
|
||||||
|
if len(userAgent) == 0 {
|
||||||
|
logger.Debug("no user agent override")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Debug(fmt.Sprintf("user agent override: %s", userAgent))
|
||||||
|
err := emulation.SetUserAgentOverride(userAgent).Do(ctx)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("set user agent override: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func extraHttpHeadersActionFunc(logger *zap.Logger, extraHttpHeaders map[string]string) chromedp.ActionFunc {
|
func extraHttpHeadersActionFunc(logger *zap.Logger, extraHttpHeaders map[string]string) chromedp.ActionFunc {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
if len(extraHttpHeaders) == 0 {
|
if len(extraHttpHeaders) == 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user