Skip to content

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

4.2 KB
Raw Blame
// Package adapters provides runtime-specific implementations of the
// autosolver interfaces. The pinchtab adapter is the ONLY place that
// imports chromedp and connects to the Pinchtab bridge runtime.
package adapters

import (
	"context"
	"fmt"
	"time"

	"github.com/chromedp/chromedp"
	"github.com/pinchtab/pinchtab/internal/autosolver"
	"github.com/pinchtab/pinchtab/internal/bridge"
)

var (
	_ autosolver.Page           = (*PinchtabPage)(nil)
	_ autosolver.ActionExecutor = (*PinchtabExecutor)(nil)
)

// PinchtabPage implements autosolver.Page by wrapping a chromedp tab context.
type PinchtabPage struct {
	ctx   context.Context
	tabID string
	b     bridge.BridgeAPI
}

// NewPinchtabPage creates a Page backed by a Pinchtab bridge tab.
func NewPinchtabPage(ctx context.Context, tabID string, b bridge.BridgeAPI) *PinchtabPage {
	return &PinchtabPage{ctx: ctx, tabID: tabID, b: b}
}

func (p *PinchtabPage) URL() string {
	var url string
	_ = chromedp.Run(p.ctx, chromedp.Location(&url))
	return url
}

func (p *PinchtabPage) Title() string {
	var title string
	_ = chromedp.Run(p.ctx, chromedp.Title(&title))
	return title
}

func (p *PinchtabPage) HTML() (string, error) {
	return p.HTMLWithin(0)
}

// HTMLWithin fetches the outer HTML bounded by timeout (0 = no bound). The
// deadline is derived from the tab context so chromedp keeps the page target
// AND cancels the in-flight CDP command when it fires — a stalled fetch returns
// promptly without leaking a worker goroutine.
func (p *PinchtabPage) HTMLWithin(timeout time.Duration) (string, error) {
	ctx := p.ctx
	if timeout > 0 {
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(p.ctx, timeout)
		defer cancel()
	}
	var html string
	if err := chromedp.Run(ctx, chromedp.Evaluate(
		`document.documentElement.outerHTML`, &html)); err != nil {
		return "", fmt.Errorf("get HTML: %w", err)
	}
	return html, nil
}

// Screenshot goes through the bridge this adapter already holds rather than
// chromedp's helper. The helper leaves fromSurface at CDP's default of true,
// which waits for a fresh compositor frame — and an idle headed page (a captcha
// challenge sitting still is the autosolver's whole subject) stops swapping
// frames, so the capture blocks until the action deadline. The bridge engine
// owns that rule for every caller.
func (p *PinchtabPage) Screenshot() ([]byte, error) {
	if p.b == nil {
		return nil, fmt.Errorf("screenshot: no bridge")
	}
	return p.b.CaptureScreenshot(p.ctx, "png", 0, nil)
}

// PinchtabExecutor implements autosolver.ActionExecutor by delegating
// to the Pinchtab bridge's human-like input system.
type PinchtabExecutor struct {
	ctx   context.Context
	tabID string
	b     bridge.BridgeAPI
}

// NewPinchtabExecutor creates an ActionExecutor backed by a Pinchtab bridge.
func NewPinchtabExecutor(ctx context.Context, tabID string, b bridge.BridgeAPI) *PinchtabExecutor {
	return &PinchtabExecutor{ctx: ctx, tabID: tabID, b: b}
}

func (e *PinchtabExecutor) Click(ctx context.Context, x, y float64) error {
	return bridge.Click(ctx, x, y)
}

func (e *PinchtabExecutor) Type(ctx context.Context, text string) error {
	actions := bridge.Type(text, false)
	return chromedp.Run(ctx, actions...)
}

func (e *PinchtabExecutor) WaitFor(ctx context.Context, selector string, timeout time.Duration) error {
	waitCtx, cancel := context.WithTimeout(ctx, timeout)
	defer cancel()
	return chromedp.Run(waitCtx, chromedp.WaitVisible(selector))
}

func (e *PinchtabExecutor) Evaluate(ctx context.Context, expr string, result interface{}) error {
	return chromedp.Run(ctx, chromedp.Evaluate(expr, result))
}

func (e *PinchtabExecutor) Navigate(ctx context.Context, url string) error {
	return chromedp.Run(ctx, chromedp.Navigate(url))
}

// NewFromBridge creates both a Page and ActionExecutor from a Bridge
// and tab ID. This is the primary factory for integration with Pinchtab.
func NewFromBridge(b bridge.BridgeAPI, tabID string) (autosolver.Page, autosolver.ActionExecutor, error) {
	tabCtx, resolvedID, err := b.TabContext(tabID)
	if err != nil {
		return nil, nil, fmt.Errorf("resolve tab %q: %w", tabID, err)
	}

	page := NewPinchtabPage(tabCtx, resolvedID, b)
	executor := NewPinchtabExecutor(tabCtx, resolvedID, b)
	return page, executor, nil
}