Skip to content

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

implement human-like interactions (P8 feature 1/10)

bot20e9ff899284
diff --git a/config.go b/config.go
index a089521e..7c98d421 100644
--- a/config.go
+++ b/config.go
@@ -20,14 +20,16 @@ const filterInteractive = "interactive"
// Action kinds for /action endpoint.
const (
- actionClick = "click"
- actionType = "type"
- actionFill = "fill"
- actionPress = "press"
- actionFocus = "focus"
- actionHover = "hover"
- actionSelect = "select"
- actionScroll = "scroll"
+ actionClick = "click"
+ actionType = "type"
+ actionFill = "fill"
+ actionPress = "press"
+ actionFocus = "focus"
+ actionHover = "hover"
+ actionSelect = "select"
+ actionScroll = "scroll"
+ actionHumanClick = "humanClick"
+ actionHumanType = "humanType"
)
// Tab actions for /tab endpoint.
diff --git a/handlers.go b/handlers.go
index cb3ae2d0..1c394604 100644
--- a/handlers.go
+++ b/handlers.go
@@ -14,6 +14,7 @@ import (
"time"
"github.com/chromedp/cdproto/cdp"
+ "github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
@@ -262,6 +263,7 @@ type actionRequest struct {
ScrollX int `json:"scrollX"`
ScrollY int `json:"scrollY"`
WaitNav bool `json:"waitNav"`
+ Fast bool `json:"fast"` // For humanType: fast typing mode
}
// ActionFunc handles a single action kind. Receives the full request for
@@ -373,6 +375,63 @@ func (b *Bridge) actionRegistry() map[string]ActionFunc {
return map[string]any{"scrolled": true, "y": 800},
chromedp.Run(ctx, chromedp.Evaluate("window.scrollBy(0, 800)", nil))
},
+ actionHumanClick: func(ctx context.Context, req actionRequest) (map[string]any, error) {
+ // Human-like click with natural mouse movement
+ if req.NodeID > 0 {
+ if err := humanClickElement(ctx, cdp.NodeID(req.NodeID)); err != nil {
+ return nil, err
+ }
+ return map[string]any{"clicked": true, "human": true}, nil
+ }
+ if req.Selector != "" {
+ // Get element to click
+ var nodes []*cdp.Node
+ if err := chromedp.Run(ctx,
+ chromedp.Nodes(req.Selector, &nodes, chromedp.ByQuery),
+ ); err != nil {
+ return nil, err
+ }
+ if len(nodes) == 0 {
+ return nil, fmt.Errorf("element not found: %s", req.Selector)
+ }
+ if err := humanClickElement(ctx, nodes[0].NodeID); err != nil {
+ return nil, err
+ }
+ return map[string]any{"clicked": true, "human": true}, nil
+ }
+ return nil, fmt.Errorf("need selector, ref, or nodeId")
+ },
+ actionHumanType: func(ctx context.Context, req actionRequest) (map[string]any, error) {
+ if req.Text == "" {
+ return nil, fmt.Errorf("text required for humanType")
+ }
+
+ // Focus element first
+ if req.Selector != "" {
+ if err := chromedp.Run(ctx, chromedp.Focus(req.Selector, chromedp.ByQuery)); err != nil {
+ return nil, err
+ }
+ } else if req.NodeID > 0 {
+ if err := chromedp.Run(ctx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ return dom.Focus().WithNodeID(cdp.NodeID(req.NodeID)).Do(ctx)
+ }),
+ ); err != nil {
+ return nil, err
+ }
+ } else {
+ return nil, fmt.Errorf("need selector, ref, or nodeId")
+ }
+
+ // Type with human-like timing
+ fast := req.Fast // Optional "fast" mode for humanType
+ actions := humanType(req.Text, fast)
+ if err := chromedp.Run(ctx, actions...); err != nil {
+ return nil, err
+ }
+
+ return map[string]any{"typed": req.Text, "human": true}, nil
+ },
}
}
diff --git a/human.go b/human.go
new file mode 100644
index 00000000..e8158edc
--- /dev/null
+++ b/human.go
@@ -0,0 +1,202 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "math"
+ "math/rand"
+ "time"
+
+ "github.com/chromedp/cdproto/cdp"
+ "github.com/chromedp/cdproto/dom"
+ "github.com/chromedp/cdproto/input"
+ "github.com/chromedp/chromedp"
+)
+
+// humanMouseMove performs natural mouse movement with bezier curves
+func humanMouseMove(ctx context.Context, fromX, fromY, toX, toY float64) error {
+ // Calculate distance for movement duration
+ distance := math.Sqrt((toX-fromX)*(toX-fromX) + (toY-fromY)*(toY-fromY))
+
+ // Base duration: 200-800ms depending on distance
+ baseDuration := 200 + (distance/1000)*300
+ duration := baseDuration + float64(rand.Intn(200)) // Add randomness
+
+ // Number of steps (more steps = smoother movement)
+ steps := int(duration / 16) // ~60fps
+ if steps < 10 {
+ steps = 10
+ }
+
+ // Generate control points for bezier curve (adds curvature)
+ cp1X := fromX + (toX-fromX)*0.25 + (rand.Float64()-0.5)*50
+ cp1Y := fromY + (toY-fromY)*0.25 + (rand.Float64()-0.5)*50
+ cp2X := fromX + (toX-fromX)*0.75 + (rand.Float64()-0.5)*50
+ cp2Y := fromY + (toY-fromY)*0.75 + (rand.Float64()-0.5)*50
+
+ // Move along bezier curve
+ for i := 0; i <= steps; i++ {
+ t := float64(i) / float64(steps)
+
+ // Cubic bezier formula
+ oneMinusT := 1 - t
+ x := oneMinusT*oneMinusT*oneMinusT*fromX +
+ 3*oneMinusT*oneMinusT*t*cp1X +
+ 3*oneMinusT*t*t*cp2X +
+ t*t*t*toX
+
+ y := oneMinusT*oneMinusT*oneMinusT*fromY +
+ 3*oneMinusT*oneMinusT*t*cp1Y +
+ 3*oneMinusT*t*t*cp2Y +
+ t*t*t*toY
+
+ // Add small jitter to make it more human
+ x += (rand.Float64() - 0.5) * 2
+ y += (rand.Float64() - 0.5) * 2
+
+ if err := chromedp.Run(ctx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ return input.DispatchMouseEvent(input.MouseMoved, x, y).Do(ctx)
+ }),
+ ); err != nil {
+ return err
+ }
+
+ // Variable delay between movements
+ delay := time.Duration(16+rand.Intn(8)) * time.Millisecond
+ time.Sleep(delay)
+ }
+
+ return nil
+}
+
+// humanClick performs a click with natural mouse movement and timing
+func humanClick(ctx context.Context, x, y float64) error {
+ // Get current mouse position (assume center of viewport if unknown)
+ currentX, currentY := float64(500), float64(300)
+
+ // Move to target with human-like movement
+ if err := humanMouseMove(ctx, currentX, currentY, x, y); err != nil {
+ return err
+ }
+
+ // Small pause before click (50-200ms)
+ time.Sleep(time.Duration(50+rand.Intn(150)) * time.Millisecond)
+
+ // Mouse down
+ if err := chromedp.Run(ctx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ return input.DispatchMouseEvent(input.MousePressed, x, y).
+ WithButton(input.Left).
+ WithClickCount(1).
+ Do(ctx)
+ }),
+ ); err != nil {
+ return err
+ }
+
+ // Hold for realistic duration (30-120ms)
+ time.Sleep(time.Duration(30+rand.Intn(90)) * time.Millisecond)
+
+ // Mouse up (with slight position variance)
+ releaseX := x + (rand.Float64()-0.5)*2
+ releaseY := y + (rand.Float64()-0.5)*2
+
+ return chromedp.Run(ctx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ return input.DispatchMouseEvent(input.MouseReleased, releaseX, releaseY).
+ WithButton(input.Left).
+ WithClickCount(1).
+ Do(ctx)
+ }),
+ )
+}
+
+// humanClickElement clicks an element with human-like behavior
+func humanClickElement(ctx context.Context, nodeID cdp.NodeID) error {
+ // Get element center coordinates
+ var box *dom.BoxModel
+ if err := chromedp.Run(ctx,
+ chromedp.ActionFunc(func(ctx context.Context) error {
+ var err error
+ box, err = dom.GetBoxModel().WithNodeID(nodeID).Do(ctx)
+ return err
+ }),
+ ); err != nil {
+ return err
+ }
+
+ if len(box.Content) < 4 {
+ return fmt.Errorf("invalid box model")
+ }
+
+ // Calculate center with slight randomness
+ centerX := (box.Content[0] + box.Content[2]) / 2
+ centerY := (box.Content[1] + box.Content[5]) / 2
+
+ // Add small random offset (humans don't click exact center)
+ offsetX := (rand.Float64() - 0.5) * 10
+ offsetY := (rand.Float64() - 0.5) * 10
+
+ return humanClick(ctx, centerX+offsetX, centerY+offsetY)
+}
+
+// humanType types text with natural timing and occasional corrections
+func humanType(text string, fast bool) []chromedp.Action {
+ actions := []chromedp.Action{}
+
+ // Base typing speed (ms per character)
+ baseDelay := 80
+ if fast {
+ baseDelay = 40
+ }
+
+ chars := []rune(text)
+ for i, char := range chars {
+ // Type the character
+ actions = append(actions, chromedp.KeyEvent(string(char)))
+
+ // Calculate delay
+ delay := baseDelay + rand.Intn(baseDelay/2)
+
+ // Occasionally pause longer (thinking)
+ if rand.Float64() < 0.05 {
+ delay += rand.Intn(500)
+ }
+
+ // Faster for repeated characters
+ if i > 0 && chars[i-1] == char {
+ delay = delay / 2
+ }
+
+ // Add delay
+ actions = append(actions, chromedp.Sleep(time.Duration(delay)*time.Millisecond))
+
+ // Occasional typos and corrections (5% chance)
+ if rand.Float64() < 0.05 && i < len(chars)-1 {
+ // Type wrong character
+ wrongChar := rune('a' + rand.Intn(26))
+ actions = append(actions,
+ chromedp.KeyEvent(string(wrongChar)),
+ chromedp.Sleep(time.Duration(50+rand.Intn(100))*time.Millisecond),
+ chromedp.KeyEvent(kb.Backspace),
+ chromedp.Sleep(time.Duration(30+rand.Intn(70))*time.Millisecond),
+ )
+ }
+ }
+
+ return actions
+}
+
+// Package kb provides keyboard constants
+var kb = struct {
+ Backspace string
+ Enter string
+ Tab string
+ Escape string
+}{
+ Backspace: "\u0008",
+ Enter: "\r",
+ Tab: "\t",
+ Escape: "\u001b",
+}