888 B
888 B
JavaScript Evaluation
Use eval to run JavaScript in the browser context.
Simple expressions can use regular shell quoting:
agent-browser eval 'document.title'
agent-browser eval 'document.querySelectorAll("img").length'
For complex JavaScript, use --stdin to avoid shell quoting corruption:
agent-browser eval --stdin <<'EVALEOF'
JSON.stringify(
Array.from(document.querySelectorAll("img"))
.filter(i => !i.alt)
.map(i => ({ src: i.src.split("/").pop(), width: i.width }))
)
EVALEOF
Use base64 for generated scripts:
agent-browser eval -b "$(printf '%s' 'Array.from(document.querySelectorAll("a")).map(a => a.href)' | base64)"
Rules of thumb:
- Single-line with no nested quotes: regular
eval 'expression'. - Nested quotes, arrow functions, template literals, or multiline:
eval --stdin. - Programmatic scripts:
eval -b.