Skip to main content

Script (Legacy Syntax)

Legacy documentation

This page describes the Script configuration syntax used in Loon 3.5.1 (982) and earlier. It remains available for maintaining existing configurations. Use the new Script syntax for Loon 3.5.1 (983) and later, or migrate an existing entry with the Script Syntax Converter.

Every script type can use the Script API.

http-request

Runs before a request is sent.

http-request ^https?:\/\/example\.com script-path=request.js,tag=Request script,requires-body=true,binary-body-mode=false,timeout=10,argument="name=loon",enable=true

Common options:

OptionDescription
requires-bodyWhether to read the request body
binary-body-modeWhether to read the body as Uint8Array
argumentValue passed to the script; double quotes are recommended
timeoutTimeout in seconds; defaults to 10

Available objects:

ObjectDescription
$request.urlRequest URL
$request.methodRequest method
$request.headersRequest header object
$request.bodyString or Uint8Array; requires requires-body=true
$request.h2_trailersHTTP/2 trailers; Build 927+
$responseundefined

Finish the script with:

// Abort the request
$done();

// Keep the request unchanged
$done({});

// Modify the request
$done({
url: "https://new.example.com/",
headers: {"X-Loon": "true"},
h2_trailers: {},
node: "HK"
});

// Return a response immediately
$done({
response: {
status: 200,
headers: {"Content-Type": "application/json"},
body: "{}"
}
});

If headers or body is omitted, the original value is preserved. Use headers: {} or body: "" to clear it.

http-response

Runs after a response is received.

http-response ^https?:\/\/example\.com script-path=response.js,tag=Response script,requires-body=true,binary-body-mode=false,timeout=10,argument="name=loon",enable=true

The options are the same as http-request. Available objects:

ObjectDescription
$requestOriginal request information
$response.statusResponse status code
$response.headersResponse header object
$response.bodyString or Uint8Array; requires requires-body=true
$response.h2_trailersHTTP/2 trailers; Build 927+
// Keep the response unchanged
$done({});

// Modify the response
$done({
status: 200,
headers: {"Content-Type": "application/json"},
h2_trailers: {},
body: "{}"
});

If headers, body, or h2_trailers is omitted, the original value is preserved. Pass an empty value to clear it.

cron

Runs on a Cron schedule.

cron "0 8 * * *" script-path=cron.js,tag=Scheduled task,timeout=300,argument="1234",enable=true

Two formats are supported:

* * * * *      minute hour day month weekday
* * * * * * second minute hour day month weekday

network-changed

Runs when the network changes. If multiple entries are configured, only the first one runs.

network-changed script-path=network-changed.js,tag=Network changed,timeout=300,argument="1234",enable=true

generic

Runs manually from the app and can receive a node, policy group, or rule as context.

generic script-path=generic.js,tag=Generic script,img-url=location.fill.viewfinder.system,timeout=300,argument="1234",enable=true