Skip to content

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

2.0 KB
Raw Blame
package actions

import (
	"fmt"
	"net/http"
	"strconv"

	"github.com/pinchtab/pinchtab/internal/cli/apiclient"
	"github.com/pinchtab/pinchtab/internal/cli/output"
	"github.com/spf13/cobra"
)

func Wait(client *http.Client, base, token string, args []string, cmd *cobra.Command) {
	body := map[string]any{}

	textFlag, _ := cmd.Flags().GetString("text")
	notTextFlag, _ := cmd.Flags().GetString("not-text")
	urlFlag, _ := cmd.Flags().GetString("url")
	loadFlag, _ := cmd.Flags().GetString("load")
	fnFlag, _ := cmd.Flags().GetString("fn")
	stateFlag, _ := cmd.Flags().GetString("state")
	tabID, _ := cmd.Flags().GetString("tab")

	// The server's /wait timeout is milliseconds. --timeout-ms is the canonical
	// flag; --timeout is the deprecated millisecond alias (cobra prints its
	// deprecation note on use). --timeout-ms wins when both are given.
	timeoutMs, _ := cmd.Flags().GetInt("timeout-ms")
	if timeoutMs == 0 {
		timeoutMs, _ = cmd.Flags().GetInt("timeout")
	}

	switch {
	case textFlag != "":
		body["text"] = textFlag
	case notTextFlag != "":
		body["notText"] = notTextFlag
	case urlFlag != "":
		body["url"] = urlFlag
	case loadFlag != "":
		body["load"] = loadFlag
	case fnFlag != "":
		body["fn"] = fnFlag
	case len(args) > 0:
		// Bare arg is overloaded: a number means a ms wait, anything else a selector.
		if ms, err := strconv.Atoi(args[0]); err == nil {
			body["ms"] = ms
		} else {
			body["selector"] = args[0]
			if stateFlag != "" {
				body["state"] = stateFlag
			}
		}
	default:
		fmt.Println("Usage: pinchtab wait <selector|ms> [--text|--not-text|--url|--load|--fn] [--timeout-ms ms] [--tab id]")
		return
	}

	if timeoutMs > 0 {
		body["timeout"] = timeoutMs
	}

	path := "/wait"
	if tabID != "" {
		path = "/tabs/" + tabID + "/wait"
	}

	result := apiclient.DoPostQuiet(client, base, token, path, body)

	jsonOutput, _ := cmd.Flags().GetBool("json")
	if jsonOutput {
		printIndented(result)
	}

	if waited, ok := result["waited"].(bool); ok && !waited {
		output.Error("wait", "timeout", output.ExitTimeout)
		return
	}
	if !jsonOutput {
		output.Success()
	}
}