hubcap eval
Evaluate a JavaScript expression in the page context and return the result.
When to use
Evaluate a JavaScript expression in the page context. Returns the result value and type. If the expression returns a Promise, it is automatically awaited. Use run to execute a JS file instead of an inline expression. Use evalframe to evaluate inside a specific frame.
Usage
hubcap eval <expression> Arguments
| Argument | Type | Required | Description |
|---|---|---|---|
expression | string | Yes | JavaScript expression to evaluate in the page context |
Flags
None.
Output
| Field | Type | Description |
|---|---|---|
value | any | The returned value from the expression |
type | string | JavaScript type of the result (string, number, boolean, object, undefined) |
{"value":42,"type":"number"} Errors
| Condition | Exit code | Stderr |
|---|---|---|
| JavaScript evaluation error | 1 | error: JS exception: <message> |
| Chrome not connected | 2 | error: connecting to Chrome: ... |
| Timeout | 3 | error: timeout |
Examples
Get the page title:
hubcap eval 'document.title' Count elements matching a selector:
hubcap eval 'document.querySelectorAll("a").length' Return a JSON object:
hubcap eval '({width: window.innerWidth, height: window.innerHeight})' Await an async operation:
hubcap eval 'fetch("/api/data").then(r => r.json())' Chain with jq to extract the value and use in a shell pipeline:
hubcap eval 'document.querySelectorAll("li").length' | jq -r '.value'