The page will introduce how to write customizable script.
Introduction
Snaker supports customizable script written by javascript.All the script should be placed at the engines folder. The snaker will automatically load the engine scripts on loading.
The META INFO
The meta information is for the snaker reading some configuration about the script. It should be placed at the head of the script file.
A typical header:
// ==UserScript==
// @name SingleFile
// @title Single File
// @description a simple downloader for single url
// @parameter *url text URL
// ==/UserScript==
Fields
| field | usage |
| name | The script's name |
| title | it will be shown at the web console |
| description | a detailed explanation about the engine |
| parameter | parameter name ,input type and title which is shown on the console, add a star to represent the parameter is not optional |
Script API
All APIs which are provided by the snaker platform is in the object "$". The object "$" is live in the scope of the whole script.
Send a HTTP GET request
$.get(url)
Example:
var url = "http://photo.auto.sina.com.cn"+thread;
var response = $.get(url);
if (response.statusCode / 100 == 2) {
$.print("download model successfully:"+url);
}Send a HTTP POST request
$.post(url,params,charset)
Example:
var param = {};
param.username = $.username;
param.password = $.password;
var url = "http://sis001.com/forum/logging.php?action=login&loginsubmit=true";
var response = $.post(url,param);Download the url and save to disk
$.save(url,subFolder,fileName)
The subFolder and fileName is optional.
Example:
var url = $.url;
$.save(url);
Recognize a picture
$.recognize(url,manual)
Require snaker to help recognize a image.If specify manual as true,it will be recognized by user at the web UI,otherwise it will be recognized by internal OCR tools.
Example:
var code = $.recongize(url,true);
$.print(code);
Response object
The response object represents the HTTP response after send GET/POST request.
response.statusCode The status code of HTTP response
response.body The body of HTTP response
response.headers The response HTTP headers, it is a map object.
Example:
var url = "https://englishpod.com/accounts/signin";
var response = $.post(url,param);
var statusCode = response.statusCode;
var location;
var result = false;
if(statusCode == 302){
location = response.headers["Location"];
if(location.length>0 && location!=url){
$.get(location);
$.print("Login successfully!");
}
}Get parameter from user input
$.{paramName}
Example:
var email = $.email;
var password = $.password;
Set auto follow redirects
$.followRedirects(true/false)
The default behavior is auto follow redirects.(HTTP 3XX)
Print a debug message
$.print(message)
Sleep
$.sleep(milleseconds)
test