Skip to main content

Overview

Evaluate JavaScript code in the browser page context. Returns the result of the expression or script execution.

Syntax

bdg dom eval <script> [options]

Arguments

script
string
required
JavaScript expression or statement to execute (e.g., "document.title", "window.location.href")

Options

--json
flag
Output as JSON with full Runtime.evaluate response

Output

Returns the evaluated result. Objects are serialized to strings.

Human-readable format

$ bdg dom eval "document.title"
Browser Debugger CLI

$ bdg dom eval "document.querySelectorAll('button').length"
3

JSON format

{
  "version": "0.5.1",
  "success": true,
  "data": {
    "result": "Browser Debugger CLI"
  }
}

Examples

Get page properties

bdg dom eval "document.title"
bdg dom eval "window.location.href"
bdg dom eval "document.readyState"

Query DOM

bdg dom eval "document.querySelector('h1').textContent"
bdg dom eval "document.querySelectorAll('.error').length"

Execute complex scripts

bdg dom eval '(() => {
  const buttons = document.querySelectorAll("button");
  return Array.from(buttons).map(b => b.textContent);
})()'

Check element state

bdg dom eval "document.querySelector('#submit-btn').disabled"
bdg dom eval "document.querySelector('input').value"

Shell quote handling

JavaScript expressions with quotes require careful escaping:
bdg dom eval 'document.querySelector("h1").textContent'
If you see a SyntaxError, the shell may have stripped quotes. The error message shows the script as received to help diagnose escaping issues.

Error messages

When JavaScript execution fails, bdg provides context:
$ bdg dom eval "document.querySelector(input).value"
Error: ReferenceError: input is not defined
Script received: document.querySelector(input).value

Shell quote damage detected:
  querySelector(input) - quotes stripped by shell

Try: bdg dom eval 'document.querySelector("input")'

Common use cases

Extract data

# Get all link URLs
bdg dom eval 'Array.from(document.querySelectorAll("a")).map(a => a.href)'

# Get form data
bdg dom eval 'Object.fromEntries(new FormData(document.querySelector("form")))'

Check page state

# Scroll position
bdg dom eval "window.scrollY"

# Viewport size
bdg dom eval '[window.innerWidth, window.innerHeight]'

Modify page

# Scroll to element
bdg dom eval 'document.querySelector("#footer").scrollIntoView()'

# Set value
bdg dom eval 'document.querySelector("#search").value = "test"'

Return value limitations

  • Objects are serialized (not live references)
  • DOM nodes return [object HTMLElement] (use selectors instead)
  • Functions cannot be returned
  • Circular references may fail
For element inspection, use bdg dom get or bdg dom query instead.

Exit codes

0
number
Success - script executed
81
number
INVALID_ARGUMENTS - JavaScript syntax error
102
number
CDP_TIMEOUT - CDP operation timed out
103
number
SESSION_FILE_ERROR - session metadata missing

Build docs developers (and LLMs) love