Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: /navigate newTab:true opens new tab + wait for document.title

bote42f37b62eb6
diff --git a/cdp.go b/cdp.go
index c32da5c3..89c3e742 100644
--- a/cdp.go
+++ b/cdp.go
@@ -35,6 +35,20 @@ func navigatePage(ctx context.Context, url string) error {
)
}
+// waitForTitle polls document.title for up to 2 seconds, returning the first
+// non-empty value or "" on timeout.
+func waitForTitle(ctx context.Context) string {
+ deadline := time.Now().Add(2 * time.Second)
+ for time.Now().Before(deadline) {
+ var title string
+ if err := chromedp.Run(ctx, chromedp.Title(&title)); err == nil && title != "" {
+ return title
+ }
+ time.Sleep(200 * time.Millisecond)
+ }
+ return ""
+}
+
// withElement resolves a backendNodeID to a JS remote object, scrolls it into
// view, and calls the given JS function on it. This is the generic helper for
// all element-targeted actions (click, hover, select, etc.).
diff --git a/handlers.go b/handlers.go
index b3aeb11e..012fd02b 100644
--- a/handlers.go
+++ b/handlers.go
@@ -7,6 +7,7 @@ import (
"log/slog"
"net/http"
"strconv"
+ "strings"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
@@ -146,8 +147,9 @@ func (b *Bridge) handleText(w http.ResponseWriter, r *http.Request) {
func (b *Bridge) handleNavigate(w http.ResponseWriter, r *http.Request) {
var req struct {
- TabID string `json:"tabId"`
- URL string `json:"url"`
+ TabID string `json:"tabId"`
+ URL string `json:"url"`
+ NewTab bool `json:"newTab"`
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil {
jsonErr(w, 400, fmt.Errorf("decode: %w", err))
@@ -158,6 +160,28 @@ func (b *Bridge) handleNavigate(w http.ResponseWriter, r *http.Request) {
return
}
+ // If newTab is requested, create a new tab and navigate there
+ if req.NewTab {
+ newTargetID, newCtx, _, err := b.CreateTab(req.URL)
+ if err != nil {
+ jsonErr(w, 500, fmt.Errorf("new tab: %w", err))
+ return
+ }
+
+ tCtx, tCancel := context.WithTimeout(newCtx, navigateTimeout)
+ defer tCancel()
+ go cancelOnClientDone(r.Context(), tCancel)
+
+ var url, title string
+ _ = chromedp.Run(tCtx,
+ chromedp.Location(&url),
+ )
+ title = waitForTitle(tCtx)
+
+ jsonResp(w, 200, map[string]any{"tabId": newTargetID, "url": url, "title": title})
+ return
+ }
+
ctx, resolvedTabID, err := b.TabContext(req.TabID)
if err != nil {
jsonErr(w, 404, err)
@@ -177,11 +201,11 @@ func (b *Bridge) handleNavigate(w http.ResponseWriter, r *http.Request) {
b.DeleteRefCache(resolvedTabID)
- var url, title string
+ var url string
_ = chromedp.Run(tCtx,
chromedp.Location(&url),
- chromedp.Title(&title),
)
+ title := waitForTitle(tCtx)
jsonResp(w, 200, map[string]any{"url": url, "title": title})
}
@@ -348,9 +372,26 @@ func (b *Bridge) handleAction(w http.ResponseWriter, r *http.Request) {
}
}
- fn, ok := b.actionRegistry()[req.Kind]
+ registry := b.actionRegistry()
+ if req.Kind == "" {
+ kinds := make([]string, 0, len(registry))
+ for k := range registry {
+ kinds = append(kinds, k)
+ }
+ jsonResp(w, 400, map[string]string{
+ "error": fmt.Sprintf("missing required field 'kind' — valid values: %s", strings.Join(kinds, ", ")),
+ })
+ return
+ }
+ fn, ok := registry[req.Kind]
if !ok {
- jsonResp(w, 400, map[string]string{"error": fmt.Sprintf("unknown action: %s", req.Kind)})
+ kinds := make([]string, 0, len(registry))
+ for k := range registry {
+ kinds = append(kinds, k)
+ }
+ jsonResp(w, 400, map[string]string{
+ "error": fmt.Sprintf("unknown action: %s — valid values: %s", req.Kind, strings.Join(kinds, ", ")),
+ })
return
}