Scripting
When a rule can't express what you need, a script can. elcapture runs JavaScript, Python or Rhai on a matching request, response or WebSocket message. The runtimes are built in; there is nothing to install.
Languages
- Rhai — a small, fast scripting language with no dependencies; the default.
- JavaScript — a built-in engine; accepts
snake_caseorcamelCasehook names. - Python — runs
python3in an isolated subprocess, with a 10-second limit.
Hooks
Define the hook for the stage you want; return the (modified) object to continue.
| Hook | Receives & returns |
|---|---|
on_request(req) | {method, url, headers, body}. Return it changed, or return {response: {status, headers, body}} to answer locally without hitting the server. |
on_response(res) | {status, headers, body}. Return it changed. |
on_message(msg) | A WebSocket frame: {url, outgoing, text or binary}. Return it changed, or {drop: true} to discard it. |
Helpers
Available in every language:
| Helper | Does |
|---|---|
env(name) | Read a script environment variable (set in Network → Scripts). |
state_get(key) / state_set(key, value) | Persist a value between calls. |
log(msg) | Write to the elcapture log. |
http_get(url) | Fetch a URL directly (no proxy, 10s). |
base64_encode / base64_decode | Base64. |
sha256(s), uuid(), now_ms() | Hash, a v4 UUID, the current time. |
jwt_decode(token) | Decode a JWT payload (no signature check). |
When it runs
Attach a script to a Script rule and it runs on every request, response or WebSocket frame the rule matches, in order with your other rules — so you can combine a script with a header change or a map in one flow.