-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
53 lines (46 loc) · 1.53 KB
/
context.go
File metadata and controls
53 lines (46 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package devbrowser
import (
"context"
"strings"
"github.com/tinywasm/devbrowser/chromedp"
)
func (h *DevBrowser) CreateBrowserContext() error {
// Create allocator with custom options
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", h.Headless),
chromedp.Flag("disable-blink-features", "WebFontsInterventionV2"),
chromedp.Flag("use-fake-ui-for-media-stream", true),
chromedp.Flag("window-position", h.Position),
chromedp.WindowSize(h.Width, h.Height),
)
// Conditionally add devtools flag
if h.Width > 1200 {
opts = append(opts, chromedp.Flag("auto-open-devtools-for-tabs", true))
}
// Disable cache by default unless explicitly enabled
// Note: disk-cache-size and media-cache-size flags cause "invalid exec pool flag" errors
// Use --disable-cache instead
if !h.CacheEnabled {
opts = append(opts,
chromedp.Flag("disable-cache", true),
chromedp.Flag("disable-gpu-shader-disk-cache", true),
)
}
allocCtx, _ := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancel := chromedp.NewContext(allocCtx,
chromedp.WithErrorf(func(format string, args ...any) {
// Chrome sends new CDP enum values before cdproto is updated.
// These unmarshal errors are harmless — suppress them to avoid
// corrupting the TUI via stderr.
if strings.HasPrefix(format, "could not unmarshal event") {
return
}
errorArgs := append([]any{"ERROR: "}, args...)
// Forward error to devbrowser log
h.Log(errorArgs...)
}),
)
h.Ctx = ctx
h.Cancel = cancel
return nil
}