Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: chromedb sandbox switch #23

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ RendererContext values:
- `BrowserExecPath`: Manually set chrome / chromium browser's executable path
- Type: String
- Default: Empty string (Auto detect)
- `NoSandbox`: Set to disable using sandbox for isolating browser process (usually use in docker container or AWS lambda)
- Type: bool
- Default: false

### Example
See usage example at [examples](examples/render/main.go)
Expand All @@ -54,6 +57,8 @@ Usage of ./render:
how to determine loading idle and return, valid input: networkIdle, InteractiveTime (default "networkIdle")
-imageLoad
indicate if load image when rendering
-sandbox
indicate if using sandbox for isolating browser process (default true)
-skipFrameCount int
skip first n frames with same id as init frame, only valid with idleType=networkIdle
-timeout int
Expand Down Expand Up @@ -92,6 +97,9 @@ PdfContext values:
- `BrowserExecPath`: Manually set chrome / chromium browser's executable path
- Type: String
- Default: Empty string (Auto detect)
- `NoSandbox`: Set to disable using sandbox for isolating browser process (usually use in docker container or AWS lambda)
- Type: bool
- Default: false

### Example
See usage example at [examples](examples/pdf/main.go)
Expand Down Expand Up @@ -124,6 +132,8 @@ Usage of ./pdf.out:
paper height in centimeter
-paperWidth float
paper width in centimeter
-sandbox
indicate if using sandbox for isolating browser process (default true)
```


2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type RendererContext struct {
IdleType string
SkipFrameCount int
BrowserExecPath string
NoSandbox bool
}

// WithRendererContext add RendererContext with rendererKey to context and return
Expand Down Expand Up @@ -60,6 +61,7 @@ type PdfContext struct {
MarginRightCm float64
IdleType string
BrowserExecPath string
NoSandbox bool
}

// WithPdfContext add PdfContext with pdfKey to context and return
Expand Down
2 changes: 2 additions & 0 deletions examples/pdf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func main() {
idleType := flag.String("idleType", "networkIdle",
"how to determine loading idle and return, valid input: networkIdle, InteractiveTime")
browserExecPath := flag.String("browserPath", "", "manually set browser executable path")
sandbox := flag.Bool("sandbox", true, "indicate if using sandbox for isolating browser process")
flag.Parse()

if *paperWidth < 0 || *paperHeight < 0 {
Expand Down Expand Up @@ -53,6 +54,7 @@ func main() {
MarginRightCm: *marginRight,
IdleType: *idleType,
BrowserExecPath: *browserExecPath,
NoSandbox: !*sandbox,
}

ctx := context.Background()
Expand Down
2 changes: 2 additions & 0 deletions examples/render/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
skipFrameCount := flag.Int("skipFrameCount", 0,
"skip first n frames with same id as init frame, only valid with idleType=networkIdle")
browserExecPath := flag.String("browserPath", "", "manually set browser executable path")
sandbox := flag.Bool("sandbox", true, "indicate if using sandbox for isolating browser process")
flag.Parse()

if *browserWidth <= 0 || *browserHeight <= 0 {
Expand Down Expand Up @@ -50,6 +51,7 @@ func main() {
IdleType: *idleType,
SkipFrameCount: *skipFrameCount,
BrowserExecPath: *browserExecPath,
NoSandbox: !*sandbox,
}

ctx := context.Background()
Expand Down
10 changes: 10 additions & 0 deletions renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func RenderPdf(ctx context.Context, urlStr string) ([]byte, error) {
opts = append(opts, chromedp.ExecPath(pdfContext.BrowserExecPath))
}

if pdfContext.NoSandbox {
fmt.Println("Set NoSandbox config")
opts = append(opts, chromedp.NoSandbox)
}

start := time.Now()
ctx, cancel := chromedp.NewExecAllocator(ctx, opts...)
defer cancel()
Expand Down Expand Up @@ -110,6 +115,11 @@ func RenderPage(ctx context.Context, urlStr string) ([]byte, error) {
if rendererContext.BrowserExecPath != "" {
opts = append(opts, chromedp.ExecPath(rendererContext.BrowserExecPath))
}

if rendererContext.NoSandbox {
fmt.Println("Set NoSandbox config")
opts = append(opts, chromedp.NoSandbox)
}
// fmt.Printf("Rendering: %s\n", urlStr)

start := time.Now()
Expand Down