Repository navigation
Expand file tree
/
Copy pathOpenBrowser.go
More file actions
160 lines (136 loc) · 3.99 KB
/
Copy pathOpenBrowser.go
File metadata and controls
160 lines (136 loc) · 3.99 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package devbrowser
import (
"fmt"
"time"
"webtyp.com/devbrowser/chromedp"
)
// Open idempotently opens the browser at rawURL.
// If the browser is already open and responsive, it navigates to rawURL without spawning a new window.
func (h *DevBrowser) Open(rawURL string) error {
h.Mu.Lock()
isFirst := h.FirstCall
h.FirstCall = false
h.LastURL = rawURL
// If already open and alive, just navigate to the requested URL (idempotent)
if h.IsOpenFlag && h.Ctx != nil && h.Ctx.Err() == nil {
h.Mu.Unlock()
return h.NavigateToURL(rawURL)
}
// Logic: on first call, only open if autoStart is true.
// On subsequent calls (e.g. user action), always open.
if isFirst && !h.AutoStart {
h.Mu.Unlock()
return nil
}
if h.TestMode {
h.OpenedOnce = true
h.Mu.Unlock()
h.Logger("Skipping browser open in TestMode")
return nil
}
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
}
h.Mu.Lock()
if h.mainTab != nil {
h.mainTab.url = rawURL
}
h.Mu.Unlock()
// Start Chrome now: an empty Run allocates the browser. If it fails, the
// context is spent — a second Run on it would allocate again and close
// chromedp's allocated channel twice (panic) — so stop here.
if err := chromedp.Run(h.Ctx); err != nil {
h.ErrChan <- fmt.Errorf("chrome failed to start: %v", err)
return
}
// 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.installLoadGuard()
h.guard.SetOrigin(rawURL)
h.initializeErrorCapture()
h.initializeInterceptCapture()
h.installSelectionCapture()
h.watchTab(h.mainTab)
if err := chromedp.Run(h.Ctx,
chromedp.Navigate(rawURL),
chromedp.WaitReady("body"),
); err != nil {
h.ErrChan <- fmt.Errorf("error navigating to %s: %v", rawURL, err)
return
}
// Wait an extra moment to ensure rendering completes
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))
}
}
// Mark the browser as fully ready: the context now has an allocated
// browser (the Navigate above forced allocation). Only now is it safe
// for other goroutines (e.g. the file watcher's Reload) to issue
// chromedp actions without triggering a second allocation.
h.Mu.Lock()
h.ready = true
h.Mu.Unlock()
h.ProcessPendingReload()
h.ReadyChan <- true
// Monitor browser context for manual close
go h.monitorBrowserClose()
}()
// Wait for start signal or error
select {
case err := <-h.ErrChan:
h.Logger("Error opening DevBrowser: ", err)
h.CloseBrowser()
return err
case <-h.ReadyChan:
h.Logger(h.StatusMessage())
// Start monitoring browser geometry changes
go h.monitorBrowserGeometry()
h.UI.RefreshUI()
return nil
}
}
// OpenBrowser opens the browser on the given port and scheme.
// It formats the URL and delegates to Open, preserving backward compatibility.
func (h *DevBrowser) OpenBrowser(port string, https bool) {
if port == "" {
return
}
h.Mu.Lock()
h.LastPort = port
h.LastHttps = https
h.Mu.Unlock()
protocol := "http"
if https {
protocol = "https"
}
url := protocol + "://localhost:" + port + "/"
_ = h.Open(url)
}