Skip to content

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

docs: add structured comments to 3 core handlers + comprehensive examples

Mario94650999973b
diff --git a/docs/examples/API_DOCUMENTATION_EXAMPLES.md b/docs/examples/API_DOCUMENTATION_EXAMPLES.md
new file mode 100644
index 00000000..a35949df
--- /dev/null
+++ b/docs/examples/API_DOCUMENTATION_EXAMPLES.md
@@ -0,0 +1,345 @@
+# API Documentation Examples
+
+This document shows what documented endpoints look like with structured comments.
+
+## Example 1: Navigate to URL (POST /navigate)
+
+### Description
+Navigate a tab to a URL or create a new tab and navigate
+
+### Request
+
+**Endpoint:** `POST /navigate`
+
+**Body Parameters:**
+- `tabId` (string, optional) — Tab ID to navigate in. If omitted, creates new tab
+- `url` (string, required) — URL to navigate to
+- `newTab` (bool, optional, default: false) — Force create new tab
+- `waitTitle` (float64, optional, default: 0) — Wait for title change (ms)
+- `timeout` (float64, optional, default: 30000) — Timeout for navigation (ms)
+
+### Response
+
+**Success (200):**
+```json
+{
+ "tabId": "tab-abc123",
+ "url": "https://example.com",
+ "title": "Example Domain"
+}
+```
+
+**Error (400):**
+```json
+{
+ "error": "invalid_url",
+ "message": "URL is required"
+}
+```
+
+### Examples
+
+**CLI:**
+```bash
+pinchtab nav https://example.com
+```
+
+**cURL — Create new tab:**
+```bash
+curl -X POST http://localhost:9867/navigate \
+ -H "Content-Type: application/json" \
+ -d '{"url":"https://example.com"}'
+```
+
+**cURL — Navigate existing tab:**
+```bash
+curl -X POST http://localhost:9867/navigate \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","url":"https://google.com"}'
+```
+
+---
+
+## Example 2: Get Page Snapshot (GET /snapshot)
+
+### Description
+Returns the accessibility tree of a tab with clickable elements, form fields, and text content
+
+### Request
+
+**Endpoint:** `GET /snapshot`
+
+**Query Parameters:**
+- `tabId` (string, required) — Tab ID
+- `filter` (string, optional, default: "all") — Filter type:
+ - `"all"` — All elements
+ - `"interactive"` — Only clickable/inputs
+- `interactive` (bool, optional) — Alias for filter=interactive
+- `compact` (bool, optional, default: false) — Compact output (shorter ref names)
+- `depth` (int, optional, default: -1) — Max nesting depth (full tree)
+- `text` (bool, optional, default: true) — Include text content
+- `format` (string, optional, default: "json") — Output format: "json" or "yaml"
+- `diff` (bool, optional, default: false) — Include diff with previous snapshot
+
+### Response
+
+**Success (200):**
+```json
+{
+ "title": "Example Domain",
+ "url": "https://example.com",
+ "elements": [
+ {
+ "ref": "e0",
+ "role": "heading",
+ "name": "Example Domain",
+ "tag": "h1"
+ },
+ {
+ "ref": "e1",
+ "role": "paragraph",
+ "name": "This domain is for use in examples...",
+ "tag": "p"
+ },
+ {
+ "ref": "e5",
+ "role": "link",
+ "name": "More information",
+ "tag": "a",
+ "href": "https://www.iana.org/domains/example"
+ }
+ ]
+}
+```
+
+### Examples
+
+**CLI — Interactive only:**
+```bash
+pinchtab snap -i -c
+```
+
+**cURL — All elements:**
+```bash
+curl "http://localhost:9867/snapshot?tabId=abc123"
+```
+
+**cURL — Interactive elements only:**
+```bash
+curl "http://localhost:9867/snapshot?tabId=abc123&filter=interactive"
+```
+
+**cURL — Compact format:**
+```bash
+curl "http://localhost:9867/snapshot?tabId=abc123&filter=interactive&compact=true"
+```
+
+**Python:**
+```python
+import requests
+
+response = requests.get(
+ "http://localhost:9867/snapshot",
+ params={"tabId": "abc123", "filter": "interactive"}
+)
+tree = response.json()
+for el in tree["elements"]:
+ print(f"{el['ref']}: {el['name']}")
+```
+
+---
+
+## Example 3: Perform Action (POST /action)
+
+### Description
+Interact with page elements: click, type text, fill inputs, press keys, hover, focus, scroll, select
+
+### Request
+
+**Endpoint:** `POST /action`
+
+**Body Parameters:**
+- `tabId` (string, required) — Tab ID
+- `kind` (string, required) — Action type:
+ - `"click"` — Click element
+ - `"type"` — Type text (appends to existing)
+ - `"fill"` — Fill input (replaces existing)
+ - `"press"` — Press key
+ - `"hover"` — Hover over element
+ - `"focus"` — Focus element
+ - `"scroll"` — Scroll
+ - `"select"` — Select option
+- `ref` (string, required) — Element reference from snapshot (e.g., "e5")
+- `text` (string, optional) — Text to type or fill (for "type"/"fill")
+- `value` (string, optional) — Value for "select" action
+- `key` (string, optional) — Key to press (for "press" action, e.g., "Enter", "Tab")
+- `x` (int, optional) — X coordinate for "scroll"
+- `y` (int, optional) — Y coordinate for "scroll"
+
+### Response
+
+**Success (200):**
+```json
+{
+ "success": true
+}
+```
+
+**Error (404):**
+```json
+{
+ "error": "element_not_found",
+ "ref": "e5"
+}
+```
+
+### Examples
+
+**CLI — Click button:**
+```bash
+pinchtab click e5
+```
+
+**CLI — Type into input:**
+```bash
+pinchtab type e3 "[email protected]"
+```
+
+**CLI — Fill form field:**
+```bash
+pinchtab fill e3 "John Doe"
+```
+
+**cURL — Click:**
+```bash
+curl -X POST http://localhost:9867/action \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","kind":"click","ref":"e5"}'
+```
+
+**cURL — Type into input:**
+```bash
+curl -X POST http://localhost:9867/action \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","kind":"type","ref":"e3","text":"[email protected]"}'
+```
+
+**cURL — Fill form (replaces existing text):**
+```bash
+curl -X POST http://localhost:9867/action \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","kind":"fill","ref":"e3","text":"John Doe"}'
+```
+
+**cURL — Press Enter key:**
+```bash
+curl -X POST http://localhost:9867/action \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","kind":"press","ref":"e7","key":"Enter"}'
+```
+
+**cURL — Click multiple actions in sequence:**
+```bash
+# 1. Click login button
+curl -X POST http://localhost:9867/action \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","kind":"click","ref":"e10"}'
+
+# 2. Fill email
+curl -X POST http://localhost:9867/action \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","kind":"fill","ref":"e3","text":"[email protected]"}'
+
+# 3. Fill password
+curl -X POST http://localhost:9867/action \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","kind":"fill","ref":"e5","text":"password123"}'
+
+# 4. Submit form
+curl -X POST http://localhost:9867/action \
+ -H "Content-Type: application/json" \
+ -d '{"tabId":"abc123","kind":"press","ref":"e7","key":"Enter"}'
+```
+
+---
+
+## Complete Workflow Example
+
+### Automated Login + Screenshot
+
+**CLI:**
+```bash
+# 1. Navigate to login page
+pinchtab nav https://example.com/login
+
+# 2. Get page structure
+pinchtab snap -i -c
+
+# 3. Fill email
+pinchtab fill e3 "[email protected]"
+
+# 4. Fill password
+pinchtab fill e5 "password123"
+
+# 5. Click submit
+pinchtab click e7
+
+# 6. Wait a moment
+sleep 2
+
+# 7. Take screenshot
+pinchtab screenshot -o login-success.jpg
+```
+
+**Python:**
+```python
+import requests
+import json
+import time
+
+BASE = "http://localhost:9867"
+
+# 1. Navigate
+r = requests.post(f"{BASE}/navigate", json={"url": "https://example.com/login"})
+tab_id = r.json()["tabId"]
+print(f"Navigated to login page: {tab_id}")
+
+# 2. Get page structure
+r = requests.get(f"{BASE}/snapshot", params={"tabId": tab_id, "filter": "interactive"})
+elements = r.json()["elements"]
+for el in elements:
+ if "email" in el.get("name", "").lower():
+ email_ref = el["ref"]
+ if "password" in el.get("name", "").lower():
+ password_ref = el["ref"]
+ if "submit" in el.get("name", "").lower() or "login" in el.get("name", "").lower():
+ submit_ref = el["ref"]
+
+# 3. Fill email
+requests.post(f"{BASE}/action", json={"tabId": tab_id, "kind": "fill", "ref": email_ref, "text": "[email protected]"})
+
+# 4. Fill password
+requests.post(f"{BASE}/action", json={"tabId": tab_id, "kind": "fill", "ref": password_ref, "text": "password123"})
+
+# 5. Submit
+requests.post(f"{BASE}/action", json={"tabId": tab_id, "kind": "click", "ref": submit_ref})
+
+# 6. Wait for page to load
+time.sleep(2)
+
+# 7. Take screenshot
+r = requests.get(f"{BASE}/screenshot", params={"tabId": tab_id})
+with open("login-success.jpg", "wb") as f:
+ f.write(r.content)
+print("Screenshot saved")
+```
+
+---
+
+## Notes
+
+- All examples use `http://localhost:9867` as the base URL
+- In production, use your actual server address
+- Tab IDs are returned from `/navigate` or other tab operations
+- Refs come from `/snapshot` responses
+- CLI commands require the pinchtab server to be running
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index be601285..7744e2ff 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -12,6 +12,53 @@ import (
"github.com/pinchtab/pinchtab/internal/web"
)
+// HandleAction performs a single action on a tab (click, type, fill, etc).
+//
+// @Endpoint POST /action
+// @Description Interact with page elements: click, type text, fill inputs, press keys, hover, focus, scroll, select
+//
+// @Param tabId string body Tab ID (required)
+// @Param kind string body Action type: "click", "type", "fill", "press", "hover", "focus", "scroll", "select" (required)
+// @Param ref string body Element reference from snapshot (e.g., "e5") (required)
+// @Param text string body Text to type or fill (for "type"/"fill" actions)
+// @Param value string body Value for "select" action (e.g., option index)
+// @Param key string body Key to press (for "press" action, e.g., "Enter", "Tab")
+// @Param x int body X coordinate for "scroll" action (optional)
+// @Param y int body Y coordinate for "scroll" action (optional)
+//
+// @Response 200 application/json Returns {success: true}
+// @Response 400 application/json Invalid action or parameters
+// @Response 404 application/json Tab or element not found
+// @Response 500 application/json Chrome error
+//
+// @Example curl click:
+// curl -X POST http://localhost:9867/action \
+// -H "Content-Type: application/json" \
+// -d '{"tabId":"abc123","kind":"click","ref":"e5"}'
+//
+// @Example curl type:
+// curl -X POST http://localhost:9867/action \
+// -H "Content-Type: application/json" \
+// -d '{"tabId":"abc123","kind":"type","ref":"e3","text":"[email protected]"}'
+//
+// @Example curl fill form:
+// curl -X POST http://localhost:9867/action \
+// -H "Content-Type: application/json" \
+// -d '{"tabId":"abc123","kind":"fill","ref":"e3","text":"John Doe"}'
+//
+// @Example curl press key:
+// curl -X POST http://localhost:9867/action \
+// -H "Content-Type: application/json" \
+// -d '{"tabId":"abc123","kind":"press","ref":"e7","key":"Enter"}'
+//
+// @Example cli click:
+// pinchtab click e5
+//
+// @Example cli type:
+// pinchtab type e3 "[email protected]"
+//
+// @Example cli fill:
+// pinchtab fill e3 "John Doe"
func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
// Ensure Chrome is initialized
if err := h.ensureChrome(); err != nil {
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 4ce20022..e438afa2 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -15,6 +15,33 @@ import (
const maxBodySize = 1 << 20
+// HandleNavigate navigates a tab to a URL or creates a new tab.
+//
+// @Endpoint POST /navigate
+// @Description Navigate to a URL in an existing tab or create a new tab and navigate
+//
+// @Param tabId string body Tab ID to navigate in (optional - creates new if omitted)
+// @Param url string body URL to navigate to (required)
+// @Param newTab bool body Force create new tab (optional, default: false)
+// @Param waitTitle float64 body Wait for title change (ms) (optional, default: 0)
+// @Param timeout float64 body Timeout for navigation (ms) (optional, default: 30000)
+//
+// @Response 200 application/json Returns {tabId, url, title}
+// @Response 400 application/json Invalid URL or parameters
+// @Response 500 application/json Chrome error
+//
+// @Example curl navigate new:
+// curl -X POST http://localhost:9867/navigate \
+// -H "Content-Type: application/json" \
+// -d '{"url":"https://example.com"}'
+//
+// @Example curl navigate existing:
+// curl -X POST http://localhost:9867/navigate \
+// -H "Content-Type: application/json" \
+// -d '{"tabId":"abc123","url":"https://google.com"}'
+//
+// @Example cli:
+// pinchtab nav https://example.com
func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
// Ensure Chrome is initialized
if err := h.ensureChrome(); err != nil {
diff --git a/internal/handlers/snapshot.go b/internal/handlers/snapshot.go
index 50f903c8..b913aab8 100644
--- a/internal/handlers/snapshot.go
+++ b/internal/handlers/snapshot.go
@@ -16,6 +16,41 @@ import (
"gopkg.in/yaml.v3"
)
+// HandleSnapshot returns the accessibility tree of a tab.
+//
+// @Endpoint GET /snapshot
+// @Description Returns the page structure with clickable elements, form fields, and text content
+//
+// @Param tabId string query Tab ID (required)
+// @Param filter string query Filter type: "interactive" for clickable/inputs only, "all" for everything (optional, default: "all")
+// @Param interactive bool query Alias for filter=interactive (optional)
+// @Param compact bool query Compact output (shorter ref names) (optional, default: false)
+// @Param depth int query Max nesting depth (optional, default: -1 for full tree)
+// @Param text bool query Include text content (optional, default: true)
+// @Param format string query Output format: "json" or "yaml" (optional, default: "json")
+// @Param diff bool query Include diff with previous snapshot (optional, default: false)
+// @Param output string query Write to file instead of response (optional)
+//
+// @Response 200 application/json Returns accessibility tree with refs
+// @Response 400 application/json Invalid tabId or parameters
+// @Response 404 application/json Tab not found
+//
+// @Example curl all elements:
+// curl "http://localhost:9867/snapshot?tabId=abc123"
+//
+// @Example curl interactive only:
+// curl "http://localhost:9867/snapshot?tabId=abc123&filter=interactive"
+//
+// @Example curl compact:
+// curl "http://localhost:9867/snapshot?tabId=abc123&filter=interactive&compact=true"
+//
+// @Example cli:
+// pinchtab snap -i -c
+//
+// @Example python:
+// import requests
+// r = requests.get("http://localhost:9867/snapshot", params={"tabId": "abc123", "filter": "interactive"})
+// tree = r.json()
func (h *Handlers) HandleSnapshot(w http.ResponseWriter, r *http.Request) {
// Ensure Chrome is initialized
if err := h.ensureChrome(); err != nil {