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

ArgumentTypeRequiredDescription
expressionstringYesJavaScript expression to evaluate in the page context

Flags

None.

Output

FieldTypeDescription
valueanyThe returned value from the expression
typestringJavaScript type of the result (string, number, boolean, object, undefined)
{"value":42,"type":"number"}

Errors

ConditionExit codeStderr
JavaScript evaluation error1error: JS exception: <message>
Chrome not connected2error: connecting to Chrome: ...
Timeout3error: 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'

See also

  • run - Execute JavaScript from a file
  • evalframe - Evaluate JavaScript in a specific frame
  • waitfn - Wait for a JavaScript function to return truthy