Script types
http-request
Triggered when an HTTP request is received. Configurable parameters:
requires-body = true— whether to read the body of the requestbinary-body-mode = true— exposes the request body as aUint8Arrayargument = "name=loon&version=2.1.0"— readable inside the script as$argument. Wrap the value in double quotes so it is parsed correctly.
Syntax
http-request ^https?:\/\/(www.)?(example)\.com script-path=localscript.js,tag = requestScript,requires-body = true,timeout = 10,binary-body-mode = false,argument = "1234",enable=trueThe default execution timeout is 10 s. The following variables are available:
- Every Script API
$request— the HTTP request$request.url:String, the request URL$request.method:String, the request method$request.headers: JS object, the request headers$request.body:StringorUint8Array, only set whenrequires-body = true
$response—undefined$done()arguments:$done()— no argument: abandon the request and close the connection$done({})— an empty JS object: continue, leaving the request untouched$done({url:"https://new.example.com/",headers:{},node:"HK"})—urlreplaces the URL,headersreplaces the request headers, andnoderoutes the rest of the request through the given policy group or node name (build 534 and later)$done({response:{ status:200, headers:{}, body:"" }})— answer the request with a synthetic response.bodymay be aStringor aUint8Array; when the body is non-empty,content-lengthin the headers is recalculated automatically andcontent-encodingis generated from the body typeNote if the object passed to$done()has noheadersorbody, the original ones are kept. To clear the body passbody=""; to clear the headers passheaders={}.
http-response
Triggered when an HTTP response is received. The parameter requires-body = true controls whether the response body is read.
requires-body = true— whether to read the bodybinary-body-mode = true— exposes the body as aUint8Arrayargument = "name=loon&version=2.1.0"— readable inside the script as$argument. Wrap the value in double quotes so it is parsed correctly.
Syntax
http-response ^https?:\/\/(www.)?(example)\.com script-path=https://example.com/loon.js,timeout=10,requires-body = true,tag = responseScript,enable=true,timeout = 10,binary-body-mode = false,argument = "1234",enable=trueThe default execution timeout is 10 s. The following variables are available:
- Every Script API
$request— the HTTP request$request.url:String, the request URL$request.method:String, the request method$request.headers: JS object, the request headers$request.body:StringorUint8Array, only set when the request carries a body
$response— the HTTP response$response.status: the status code$response.headers: the response headers$response.body:StringorUint8Array, only set when the response carries a body andrequires-body = true
$done()arguments:$done()— no argument: abandon the request and close the connection$done({})— an empty JS object: continue, leaving everything untouched$done({ status:200, headers:{}, body:"" })— return a synthetic response.bodymay be aStringor aUint8Array; when the body is non-empty,content-lengthis recalculated automatically andcontent-encodingis generated from the body typeNote if the object passed to$done()has noheadersorbody, the original ones are kept. To clear the body passbody=""; to clear the headers passheaders={}.
cron
- Runs a script on a schedule defined by a cron expression
"** ** * * *"— minute hour day month weekday"** ** ** * * *"— second minute hour day month weekday
Syntax
cron "0 8 * * *" script-path=cron.js,tag = cronScript,enable=true,timeout = 300,argument = "1234",enable=trueThe default execution timeout is 200 s. The following variables are available:
- Every Script API
network-changed
Called whenever the network environment changes. If several scripts of this type are declared, only the first one in the configuration file is called.
Syntax
network-changed script-path=https://raw.githubusercontent.com/Loon0x00/LoonExampleConfig/master/Script/netChanged.js,tag=changeModel,timeout = 300,argument = "1234",enable=trueThe default execution timeout is 200 s. The following variables are available:
- Every Script API
generic
A script that receives the nodes, policy groups and rules as parameters. It only runs when you trigger it manually from the in-app screen — never on its own.
Syntax
generic script-path=https://raw.githubusercontent.com/Loon0x00/LoonExampleConfig/master/Script/generic_example.js,tag=GeoLocation,timeout=10,img-url=location.fill.viewfinder.system,timeout = 300,argument = "1234",enable=trueThe following variables are available:
- Every Script API
