Difference between revisions of "Client JavaScript Extension API"
(Created page with "Category:INCOMPLETE var user = new User(); function StartupEvent() { user.setOnLoadedEvent(onUserLoaded); user.setOnErrorEvent(onUserError); // send request user.sen...") |
|||
Line 17: | Line 17: | ||
Methods: | Methods: | ||
− | .send(name, param) | + | ==.send(name, param)== |
sends a virtualremote (name and param will be url encoded) | sends a virtualremote (name and param will be url encoded) | ||
− | .setMaxWaitingTime(msec) | + | ==.setMaxWaitingTime(msec)== |
set max waiting time in msec. If a request exceeds this time, it will be aborted (and fire an onError event, if set). | set max waiting time in msec. If a request exceeds this time, it will be aborted (and fire an onError event, if set). | ||
− | .setMaxRetries(n) | + | ==.setMaxRetries(n)== |
set max number of retry attempts. If a a request fails, it will keep on retrying until it succeeds or the max number of retries is reached. | set max number of retry attempts. If a a request fails, it will keep on retrying until it succeeds or the max number of retries is reached. | ||
Before retrying it will fire an onRetry event, if set. | Before retrying it will fire an onRetry event, if set. | ||
If n is 0, and retryOnErrorDelay is not 0, it will keep on retrying forever (it will never fire an onError event, if set). | If n is 0, and retryOnErrorDelay is not 0, it will keep on retrying forever (it will never fire an onError event, if set). | ||
− | .setRetryOnErrorDelay(msec) | + | ==.setRetryOnErrorDelay(msec)== |
set msec of delay before retrying after a request has failed. | set msec of delay before retrying after a request has failed. | ||
If msec is 0 it won't retry (and will fire an onError event, if set). | If msec is 0 it won't retry (and will fire an onError event, if set). | ||
− | .setOnLoadedEvent(f) | + | ==.setOnLoadedEvent(f)== |
f is a function that is called when the request is successful. | f is a function that is called when the request is successful. | ||
To get the response text or xml, use .getResponseText() and .getResponseXML() | To get the response text or xml, use .getResponseText() and .getResponseXML() | ||
− | .setOnErrorEvent(f) | + | ==.setOnErrorEvent(f)== |
f is a function called when the request fails. f is called with an error code parameter: function(errCode) | f is a function called when the request fails. f is called with an error code parameter: function(errCode) | ||
error codes are enumerated as .ERROR_GENERAL, .ERROR_CONNECTION ... | error codes are enumerated as .ERROR_GENERAL, .ERROR_CONNECTION ... | ||
− | .setOnRetryEvent(f) | + | ==.setOnRetryEvent(f)== |
f is a function that is called when the last request failed, before retrying. | f is a function that is called when the last request failed, before retrying. | ||
To get the response text or xml, use .getResponseText() and .getResponseXML() | To get the response text or xml, use .getResponseText() and .getResponseXML() | ||
− | .getResponseText() | + | ==.getResponseText()== |
get the last response text | get the last response text | ||
− | .getResponseXML() | + | ==.getResponseXML()== |
get the last response XML | get the last response XML | ||
− | .free() | + | ==.free()== |
free the memory and reset the behaviour. It will be reinitialized if any method is called. | free the memory and reset the behaviour. It will be reinitialized if any method is called. | ||
− | Error codes: | + | ==Error codes:== |
.ERROR_GENERAL : general error | .ERROR_GENERAL : general error | ||
.ERROR_CONNECTION : no connection | .ERROR_CONNECTION : no connection | ||
Line 61: | Line 61: | ||
.ERROR_NOACCESS : request returned noaccess | .ERROR_NOACCESS : request returned noaccess | ||
− | Example index.js: try one time, then fire onLoaded or onError | + | ==Examples== |
+ | ===Example index.js: try one time, then fire onLoaded or onError=== | ||
var user = new User(); | var user = new User(); | ||
Line 82: | Line 83: | ||
} | } | ||
− | Example index.js: retry forever, until it succeeds | + | ===Example index.js: retry forever, until it succeeds=== |
var user = new User(); | var user = new User(); | ||
Line 100: | Line 101: | ||
− | Example index.js: retry 3 times, then fire onError | + | ===Example index.js: retry 3 times, then fire onError=== |
var user = new User(); | var user = new User(); |
Revision as of 12:31, 21 January 2014
var user = new User();
function StartupEvent() { user.setOnLoadedEvent(onUserLoaded); user.setOnErrorEvent(onUserError); // send request user.send("myname","myparam");
//document.oncontextmenu = document.body.oncontextmenu = function() {return false;} varSet("myvar1","myvalue"); varSet("myvar2",5.3); varSet("myvar3",[1,2,3]); varSet("myvar4",{"a":1,"b":2}); }
Methods:
Contents
.send(name, param)
sends a virtualremote (name and param will be url encoded)
.setMaxWaitingTime(msec)
set max waiting time in msec. If a request exceeds this time, it will be aborted (and fire an onError event, if set).
.setMaxRetries(n)
set max number of retry attempts. If a a request fails, it will keep on retrying until it succeeds or the max number of retries is reached. Before retrying it will fire an onRetry event, if set. If n is 0, and retryOnErrorDelay is not 0, it will keep on retrying forever (it will never fire an onError event, if set).
.setRetryOnErrorDelay(msec)
set msec of delay before retrying after a request has failed. If msec is 0 it won't retry (and will fire an onError event, if set).
.setOnLoadedEvent(f)
f is a function that is called when the request is successful. To get the response text or xml, use .getResponseText() and .getResponseXML()
.setOnErrorEvent(f)
f is a function called when the request fails. f is called with an error code parameter: function(errCode) error codes are enumerated as .ERROR_GENERAL, .ERROR_CONNECTION ...
.setOnRetryEvent(f)
f is a function that is called when the last request failed, before retrying. To get the response text or xml, use .getResponseText() and .getResponseXML()
.getResponseText()
get the last response text
.getResponseXML()
get the last response XML
.free()
free the memory and reset the behaviour. It will be reinitialized if any method is called.
Error codes:
.ERROR_GENERAL : general error .ERROR_CONNECTION : no connection .ERROR_MAXWAITTIME : max waiting time exceeded .ERROR_LOGOUT : client is logged out .ERROR_LOCK : client is locked out .ERROR_NOACCESS : request returned noaccess
Examples
Example index.js: try one time, then fire onLoaded or onError
var user = new User();
function StartupEvent() { user.setOnLoadedEvent(onUserLoaded); user.setOnError(onUserError); // send request user.send("myname","myparam"); }
function onUserLoaded() { webLog("loaded "+user.getResponseText()); // do something with the response user.free(); // won't be using it again } function onUserError(errCode) { webLog("error"); user.free(); // won't be using it again }
Example index.js: retry forever, until it succeeds
var user = new User();
function StartupEvent() { user.setOnLoadedEvent(onUserLoaded); user.setRetryOnErrorDelay(1000); // wait one second before retrying on error // send request user.send("myname","myparam"); }
function onUserLoaded() { webLog("loaded "+user.getResponseText()); // do something with the response user.free(); // won't be using it again }
Example index.js: retry 3 times, then fire onError
var user = new User();
function StartupEvent() { // init user object user.setRetryOnErrorDelay(100); // retry almost immediately on error user.setMaxRetries(3); // retry 3 times, then fire error user.setOnLoadedEvent(onUserLoaded); user.setOnError(onUserError); // send request user.send("myname","myparam"); }
function onUserLoaded() { webLog("loaded "+user.getResponseText()); // do something with the response user.free(); // won't be using it again } function onUserError(errCode) { webLog("error, already tried 3 times"); if (errCode == user.ERROR_MAXWAITTIME) webLog("the last request failed because it exceeded the waiting time"); user.free(); // won't be using it again }
function onUserLoaded() { webLog("loaded "+user.getResponseText()); // do something with the response user.free(); // won't be using it again } function onUserError(errCode) { webLog("error"); user.free(); // won't be using it again }
function userCommand(rem, id) { var a = varGet("counterz!"); if (!a) a = 1; else a++; varSet("counterz!",a); console.log(rem+" "+id+" "+a) if (a == 3) return ""; else if (a == 4) return "page:ciao"; else return null; }