mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 00:22:14 +01:00
feat(chromium): add --chromium-disable-javascript property (fixes #175)
This commit is contained in:
@@ -65,6 +65,7 @@ type Chromium struct {
|
||||
proxyServer string
|
||||
allowList *regexp.Regexp
|
||||
denyList *regexp.Regexp
|
||||
disableJavaScript bool
|
||||
disableRoutes bool
|
||||
}
|
||||
|
||||
@@ -218,6 +219,7 @@ func (mod Chromium) Descriptor() gotenberg.ModuleDescriptor {
|
||||
fs.String("chromium-proxy-server", "", "Set the outbound proxy server; this switch only affects HTTP and HTTPS requests")
|
||||
fs.String("chromium-allow-list", "", "Set the allowed URLs for Chromium using a regular expression")
|
||||
fs.String("chromium-deny-list", "^file:///[^tmp].*", "Set the denied URLs for Chromium using a regular expression")
|
||||
fs.Bool("chromium-disable-javascript", false, "Disable JavaScript")
|
||||
fs.Bool("chromium-disable-routes", false, "Disable the routes")
|
||||
|
||||
err := fs.MarkDeprecated("chromium-user-agent", "use the userAgent form field instead")
|
||||
@@ -241,6 +243,7 @@ func (mod *Chromium) Provision(ctx *gotenberg.Context) error {
|
||||
mod.proxyServer = flags.MustString("chromium-proxy-server")
|
||||
mod.allowList = flags.MustRegexp("chromium-allow-list")
|
||||
mod.denyList = flags.MustRegexp("chromium-deny-list")
|
||||
mod.disableJavaScript = flags.MustBool("chromium-disable-javascript")
|
||||
mod.disableRoutes = flags.MustBool("chromium-disable-routes")
|
||||
|
||||
binPath, ok := os.LookupEnv("CHROMIUM_BIN_PATH")
|
||||
@@ -389,6 +392,23 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath
|
||||
return chromedp.Tasks{
|
||||
network.Enable(),
|
||||
fetch.Enable(),
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
// See https://github.com/gotenberg/gotenberg/issues/175.
|
||||
if !mod.disableJavaScript {
|
||||
logger.Debug("JavaScript not disabled")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
logger.Debug("disable JavaScript")
|
||||
|
||||
err := emulation.SetScriptExecutionDisabled(true).Do(ctx)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("disable JavaScript: %w", err)
|
||||
}),
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
if len(options.ExtraHTTPHeaders) == 0 {
|
||||
logger.Debug("no extra HTTP headers")
|
||||
@@ -408,6 +428,8 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath
|
||||
return nil
|
||||
}
|
||||
|
||||
emulation.SetScriptExecutionDisabled(true)
|
||||
|
||||
return fmt.Errorf("set extra HTTP headers: %w", err)
|
||||
}),
|
||||
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||
|
||||
@@ -238,6 +238,7 @@ func TestChromium_PDF(t *testing.T) {
|
||||
proxyServer string
|
||||
allowList *regexp.Regexp
|
||||
denyList *regexp.Regexp
|
||||
disableJavaScript bool
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
@@ -256,6 +257,10 @@ func TestChromium_PDF(t *testing.T) {
|
||||
UserAgent: "foo",
|
||||
},
|
||||
},
|
||||
{
|
||||
URL: "file:///tests/test/testdata/chromium/html/sample9/index.html",
|
||||
disableJavaScript: true,
|
||||
},
|
||||
{
|
||||
URL: "file:///tests/test/testdata/chromium/html/sample4/index.html",
|
||||
options: Options{
|
||||
@@ -412,6 +417,7 @@ func TestChromium_PDF(t *testing.T) {
|
||||
|
||||
mod.allowList = tc.allowList
|
||||
mod.denyList = tc.denyList
|
||||
mod.disableJavaScript = tc.disableJavaScript
|
||||
|
||||
outputDir, err := gotenberg.MkdirAll()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user