35 lines
888 B
Markdown
35 lines
888 B
Markdown
# JavaScript Evaluation
|
|
|
|
Use `eval` to run JavaScript in the browser context.
|
|
|
|
Simple expressions can use regular shell quoting:
|
|
|
|
```bash
|
|
agent-browser eval 'document.title'
|
|
agent-browser eval 'document.querySelectorAll("img").length'
|
|
```
|
|
|
|
For complex JavaScript, use `--stdin` to avoid shell quoting corruption:
|
|
|
|
```bash
|
|
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:
|
|
|
|
```bash
|
|
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`.
|