Script API
Basics
- $loon
$loon: device name, system version, app version, build version
- $script
$script.name: name of the script being executed
$script.startTime: the time the script started
- $config
$config.getConfig(): returns the current configuration as a JSON string
{
"running_model": 1,//running mode: 0 = global direct, 1 = rule based, 2 = global proxy
"all_buildin_nodes": [
"DIRECT",
"REJECT"
],
"global_proxy": "Node Select",
"all_policy_groups": [
"Payments",
"Netflix",
"Carrier Hijack",
"Load Balance",
"Global Direct",
"Domestic Media",
"HK",
"Ad Block",
"Final",
"WiFi",
"Node Select",
"JP",
"Apple Services",
"Speed Test",
"Health Check",
"BiliBili Area",
"TW",
"Google",
"YouTube",
"Foreign Sites",
"NetEase Unlock"
],
"ssid": "loon-wifi-5g",
"final": "Node Select",
"policy_select": {
"Apple Services": "Global Direct",
"Ad Block": "REJECT",
"BiliBili Area": "HK",
"YouTube": "Node Select",
"Payments": "🇺🇲 v1|US|A|6 nodes",
"Netflix": "DIRECT",
"Speed Test": "DIRECT",
"Google": "Health Check",
"Domestic Media": "Global Direct",
"Carrier Hijack": "REJECT",
"Node Select": "JP",
"Final": "Node Select",
"JP": "🇯🇵 v1|Japan|A|7 nodes",
"NetEase Unlock": "DIRECT",
"Global Direct": "DIRECT",
"TW": "🇭🇰 v1|Hong Kong|D|7 nodes",
"Foreign Sites": "Node Select"
}
}$config.getConfig(policyName,selectName): selects selectName as the active policy of the policyName group. Both arguments are String; returns false on failure and true on success.
$config.getSubPolicies(policyName, function(subPolicies){}): fetches every sub-policy of the policyName group; the callback receives subPolicies as an array of strings.
$config.getSelectedPolicy(policyName): returns the name of the sub-policy currently selected in the policyName group.
$config.setRunningModel(model): sets the running mode. model is an int: 0 = global direct, 1 = rule based, 2 = global proxy.
Local storage
- $persistentStore
$persistentStore.write(value,[key]): stores value locally under key. Both are strings; when key is omitted, a hash of the current script name is used. Returns true on success, false on failure.
$persistentStore.read([key]): reads the value stored under key. When key is omitted, a hash of the current script name is used. Returns the value as a string.
$persistentStore.remove(): clears every value stored locally through the script API.
- $notification
$notification.post(title,subtitle,content,[attach]): posts a local iOS notification. The first three arguments are the title, subtitle and body, all String. attach is optional and describes the attachment — an image or video URL, or the URL opened when the notification is tapped.
// when attach is a string, it is the URL opened on tap
$notification.post("title","subtitle","content","loon://switch")
// to support both an attachment and a tap target, pass a JS object
var attach = {
"openUrl":"loon://switch",
"mediaUrl":"https://example.com/img"
}
$notification.post("title","subtitle","content",attach)Network requests
- $httpClient
$httpClient.get(params, function(errormsg,response,data){}): performs an HTTP GET. params holds the request parameters and the callback runs when the request finishes.
// params
{
url:"https://example.com/",
timeout: 2000, // request timeout in ms, 5000 ms by default
headers:{
Content-Type:"application/json"
},
body:"{}",// only used by POST; may be a JSON object, a string or binary data
body-base64:true,// when set, body is decoded from base64 into binary. Do not set it unless body really is base64 (build 612+)
node:"HK - v1.0",// which node or policy group performs this request (a node name, a policy group name, or a Loon node description such as node:"shadowsocksr,example.com,1070,chacha20-ietf,"password",protocol=auth_aes128_sha1,protocol-param=test,obfs=plain,obfs-param=edge.microsoft.com")
binary-mode:true,// return the response as binary, false by default
auto-redirect:false,// follow redirects automatically, true by default (build 660+)
auto-cookie:false,// store and send cookies automatically, true by default (build 662+)
}
// callback arguments
errormsg: failure reason as a String, null when the request succeeded
response: JS object
{
status:200,
headers:{
content-length:200
}
}
data: the response body as a String$httpClient.post(params, function(errormsg,response,data){}): performs a POST. Parameters and callback are identical to get.
$httpClient.head(params, function(errormsg,response,data){}): performs a HEAD. Parameters and callback are identical to get.
$httpClient.delete(params, function(errormsg,response,data){}): performs a DELETE. Parameters and callback are identical to get.
$httpClient.put(params, function(errormsg,response,data){}): performs a PUT. Parameters and callback are identical to get.
$httpClient.options(params, function(errormsg,response,data){}): performs an OPTIONS. Parameters and callback are identical to get.
$httpClient.patch(params, function(errormsg,response,data){}): performs a PATCH. Parameters and callback are identical to get.
Miscellaneous
- $done()
In an ordinary script, calling $done() ends execution and releases the script resources Loon allocated, so call it at the end of every script. In http-request and http-response scripts the semantics differ — see script types.
- $envirnoment
Only available in generic scripts. When such a script runs against a node, $envirnoment exposes:
$environment.params.node: the node name (use nodeInfo since build 410)
$environment.params.nodeInfo: condensed node information (not every field is returned, for security reasons)
