-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenBrowser.go
More file actions
132 lines (111 loc) · 3.32 KB
/
OpenBrowser.go
File metadata and controls
132 lines (111 loc) · 3.32 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package devbrowser
import (
"fmt"
"time"
"github.com/tinywasm/devbrowser/chromedp"
)
func (h *DevBrowser) OpenBrowser(port string, https bool) {
h.Mu.Lock()
isFirst := h.FirstCall
h.FirstCall = false
h.LastPort = port
h.LastHttps = https
//h.Logger(fmt.Sprintf("DEBUG: OpenBrowser called. port=%s, https=%v, isOpen=%v, firstCall=%v", port, https, h.IsOpenFlag, isFirst))
// Logic: on first call, only open if autoStart is true.
// On subsequent calls (e.g. user action), always open.
if isFirst && !h.AutoStart {
//h.Logger("DEBUG: OpenBrowser skipped on first call (autoStart=false)")
h.Mu.Unlock()
return
}
if h.IsOpenFlag {
//h.Logger("DEBUG: OpenBrowser returning early, already open")
h.Mu.Unlock()
return
}
if h.TestMode {
h.OpenedOnce = true
h.Mu.Unlock()
h.Logger("Skipping browser open in TestMode")
return
}
h.IsOpenFlag = true
h.OpenedOnce = true
h.Mu.Unlock()
// Add listener for exit signal (only once per open session)
go func() {
<-h.ExitChan
h.CloseBrowser()
}()
go func() {
// Detect monitor size and apply constraints ONLY if not already configured.
// If configured, we respect the user's stored preferences (which might be manual resized).
if !h.SizeConfigured {
h.DetectMonitorSize()
h.StartWithDetectedSize()
}
err := h.CreateBrowserContext()
if err != nil {
h.ErrChan <- err
return
}
// Restore device emulation if set
h.Mu.Lock()
mode := h.ViewportMode
h.Mu.Unlock()
if mode != "" && mode != "off" && mode != "desktop" {
// We need a context where the page is already loaded or at least ready
// but applyDeviceEmulation can be called on the context once it's created.
// However, emulation is better applied AFTER navigation or during it.
// Page load might reset viewport in some cases, but CDP overrides usually persist.
}
protocol := "http"
if https {
protocol = "https"
}
url := protocol + `://localhost:` + port + "/"
// Initialize console log capturing BEFORE navigating to the page
// This ensures all console.log statements from page load are captured
if err := h.initializeConsoleCapture(); err != nil {
h.Logger("Warning: failed to initialize console capture:", err)
// Continue anyway - capture is optional
}
h.initializeNetworkCapture()
h.initializeErrorCapture()
if err := chromedp.Run(h.Ctx,
chromedp.Navigate(url),
chromedp.WaitReady("body"),
); err != nil {
h.ErrChan <- fmt.Errorf("error navigating to %s: %v", url, err)
return
}
// Esperar un momento adicional para asegurar que todo esté cargado
time.Sleep(100 * time.Millisecond)
// Restore device emulation if set
h.Mu.Lock()
vMode := h.ViewportMode
h.Mu.Unlock()
if vMode != "" && vMode != "off" && vMode != "desktop" {
if err := h.applyDeviceEmulation(); err != nil {
h.Logger(fmt.Sprintf("Failed to restore emulation: %v", err))
}
}
h.ReadyChan <- true
// Monitor browser context for manual close
go h.monitorBrowserClose()
}()
// Esperar señal de inicio o error
select {
case err := <-h.ErrChan:
// use helper to ensure logging goes through configured logger
h.Logger("Error opening DevBrowser: ", err)
h.CloseBrowser()
return
case <-h.ReadyChan:
h.Logger(h.StatusMessage())
// Start monitoring browser geometry changes
go h.monitorBrowserGeometry()
h.UI.RefreshUI()
return
}
}