jScripter Functions List
Append ( filename, txt )
Appends the content of txt to the end of file filename file. File is created if if does not exist.
Parameters: | |
filename | destination file path. The path must be local. |
txt | text or other data to be inserted to the end of the file. |
Return value: | |
true | in case of success |
false | otherwise |
Example:
var s = "Hello, world\r\n";
if( !Append("/tmp/test.txt", s) ) {
Print("Error");
}
base64Decode ( txt )
Decodes the string txt from base64.
Parameters: | |
txt | the data in base64 encoding. |
Return value: | |
string | in case of success |
false | otherwise |
Example:
var s = "SGVsbG8sIHdvcmxkIQ==";
var res = base64Decode(s);
Print(res);
base64Encode ( txt )
Encodes the string txt to base64.
Parameters: | |
txt | the data to be encoded in base64. |
Return value: | |
string | in case of success |
false | otherwise |
Example:
var s = "Hello, world!";
var res = base64Encode(s);
Print(res);
clearInterval ( intervalId )
Method clears a timer set with the setInterval() method. If the parameter provided does not identify a previously established action, this method does nothing.
Parameters: | |
intervalId | the identifier of the repeated action to cancel. This ID was returned by the corresponding call to setInterval(). |
Return value: | none |
Example:
var theIntervalId = setInterval( function() {}, 1000);
clearInterval ( theIntervalId );
clearTimeout ( timeoutId )
Cancels a timeout previously established by calling setTimeout(). If the parameter provided does not identify a previously established action, this method does nothing.
Parameters: | |
timeoutId | The identifier of the timeout to cancel. This ID was returned by the corresponding call to setTimeout() |
Return value: | none |
Example:
var theTimeoutId = setTimeout( function() {}, 1000);
clearTimeout ( theTimeoutId );
Dir( path )
Reads file names in specified directory. Not recursive.
Parameters: | |
path | the path to local directory. |
Return value: | |
array of objects | in case of success |
empty array | if path does not exist |
Each object has the following fields
name | file name. |
size | file size, bytes. |
mtime | last modification time, seconds since UNIX epoch. |
type | file type (F - file, D - directory, L - simlink). |
Example:
var files = Dir( "." );
Print ( JSON.stringify ( files ) );
/*** Example of possible output: ***/
[{
"name": ".storage",
"size": 145,
"mtime": 1691077935.209,
"type": "F"
}, {
"name": "autotest",
"size": 0,
"mtime": 1684428159.011,
"type": "D"
}, {
"name": "release",
"size": 0,
"mtime": 1690964371.008,
"type": "D"
}, {
"name": "test.js",
"size": 1500,
"mtime": 1690880002.712,
"type": "F"
}]
Connect( )
--
Parameters: | -- |
Return value: | -- |
Example:
Exit( )
Exits current worker. Other workers will continue running.
Parameters: | none |
Return value: | none |
Example:
Exit( );
getName( )
Returns the name of current worker
Parameters: | none |
Return value: | |
string | current worker name |
Example:
var s = getName( );
Print( "Name is: " + s );
getTag( )
Returns the global application tag name
Parameters: | none |
Return value: | |
string | application tag name. Read more about tags here. |
Example:
var s = getTag( );
Print( "Tag is: " + s );
Http ( func, url, data, proxy_addr, headers )
Makes HTTP request and returns immediately. Asynchronous.
Parameters: | |
func | callback function |
url | URL to load. Must begin with one of the following: "http://", "https://", "ws://" or "wss://". Optional. |
data | post data or null. Optional |
proxy_addr | proxy server address and port (ex: 10.11.13.13:8088) or null. Optional. |
headers | String. Additional HTTP headers. Optional. |
Return value: | |
integer | socket identifier in case of success |
false | in case of error |
After request is complete, the callback function func is called with an object as parameter. Return value of callback function is ignored. The object has the following fields:
socket | socket identifier |
index | counter of received data portions. First call to function func has index 0, second 1 and so on. For HTTP protocols only one call is made, with index 0. For websocket protocol there can be many calls, one per websocket frame received |
closed | true if connection is closed |
data | the data received |
headers | array of strings, HTTP headers received. Present for index=0 only. |
proxy | the object, present only when proxy_addr parameter was given. Example: {"host":"10.11.12.13", "port":8088, "protocol":"socks5"} |
Example:
Http(callback, "https://www.tiktok.com/robots.txt");
function callback( res ) {
Print( JSON.stringify ( res ) );
}
// Example of possible output:
{
"socket": 2,
"index": 0,
"closed": true,
"data": "User-agent: Baiduspider\nDisallow: /\n",
"headers": [
"HTTP/1.0 200 OK",
"Server: nginx",
"Content-Type: text/plain; charset=utf-8",
"Content-Encoding: gzip",
"Content-Length: 233",
"Connection: close"
]
}
Include ( fpath )
Loads and evaluates local javascript file. Synchrous.
Parameters: | |
fpath | local path to javascript file. |
Return value: | |
true | in case of success |
false | otherwise (ex. file not found) |
Example:
Include( "/home/myuser/test.js" );
Listen( )
--
Parameters: | -- |
Return value: | -- |
Example:
MD5 ( txt )
Calculates MD5 checksum of message txt.
Parameters: | |
txt | the data to hash. |
Return value: | |
string | MD5 hash |
Example:
var hash = MD5( "Hello" );
Print( "hash=" + hash );
MyIP ( )
Returns current computer's external IP address..
Parameters: | none |
Return value: | |
string | current IP address |
Example:
var ip = MyIP( );
Print( "my IP is " + ip );
numSockets ( )
Returns number of active network connections for current worker.
Parameters: | none |
Return value: | |
number | number of opened sockets for current worker |
Example:
var num = numSockets( );
Print( "number of sockets is " + num );
numTimers ( )
Returns number of active timers in current worker (timers are created by setTimeout and setInterval functions).
Parameters: | none |
Return value: | |
number | number of timers for current worker |
Example:
var num = numTimers( );
Print( "number of timers is " + num );
parseUrl ( url )
Breaks the provided URL into parts.
Parameters: | |
url | the URL. |
Return value: | |
object | in case of success |
false | otherwise |
The function returns the object which consists of the following fields:
scheme | scheme |
host | host name |
path | URL path |
query | query parameters |
Example:
var url = "https://www.google.com/search?client=opera&q=sb-core&sourceid=opera&ie=UTF-8&oe=UTF-8";
var parts = parseUrl ( url );
Print ( JSON.stringify ( parts ) );
/*** Result: ***/
{
"scheme": "https",
"host": "www.google.com",
"port": 443,
"path": "/search",
"query": {
"client": "opera",
"q": "sb-core",
"sourceid": "opera",
"ie": "UTF-8",
"oe": "UTF-8"
}
}
Print (...)
Outputs specified string(s) to both stdout and log file.
Parameters: | none |
Return value: | none |
Example:
Print( "A", 5+6, "B" + 22 );
// The output:
A11B22
Read ( fpath )
Reads content of local file. Synchrous.
Parameters: | |
fpath | path to local file. |
Return value: | |
string | in case of success |
false | otherwise |
Example:
var s = Read( "/some/file.txt" );
Print( "File content: ", s);
Send ( socketId, txt )
Sends message txt to socket. Asynchrous.
Parameters: | |
socketId | socket identifier. |
txt | string to send, or null to close connection |
Return value: | |
true | in case of success |
false | otherwise (ex. socket does not exists, or already closed) |
Example:
Http(callback, "wss://websocketstest.com/service");
function callback( res ) {
// dump the data
Print("WebsocketTest: ", JSON.stringify(res));
// when connected, request the timer
if( res.index == 0 )
Send( res.socket, "timer," );
// close connection after 5th received frame
if( res.index == 5 )
Send( res.socket, null );
}
// the output:
WebsocketTest: {"socket":2,"index":0,"headers":[
"HTTP/1.1 101 Switching Protocols",
"Date: Sat, 05 Aug 2023 17:38:14 GMT",
"Connection: upgrade",
"Upgrade: WebSocket",
"Sec-WebSocket-Accept: s6ZgSKRlTXz3xV2tQzO0GM58iKc="]}
WebsocketTest: {"socket":2,"index":1,"data":"connected,"}
WebsocketTest: {"socket":2,"index":2,"data":"time,2023/8/5 17:38:21"}
WebsocketTest: {"socket":2,"index":3,"data":"time,2023/8/5 17:38:22"}
WebsocketTest: {"socket":2,"index":4,"data":"time,2023/8/5 17:38:23"}
WebsocketTest: {"socket":2,"index":5,"data":"time,2023/8/5 17:38:24"}
WebsocketTest: {"socket":2,"index":6,"closed":true}
setInterval ( func, interval )
Creates a timer.
Parameters: | |
func | callback function to be fired every interval ms. |
interval | interval, in miliseconds |
Return value: | |
number | timer identifier in case of success |
false | otherwise |
Example:
function T ( ) {
Print(" timer! ");
}
setInterval( T, 1000 ); // every 1 second
setName ( txt )
Sets name of current worker.
Parameters: | |
txt | worker name. |
Return value: | none |
Parameters:
txt - worker name.
Return value: None.
Example:
setName( "MyThread" );
setTag ( txt )
Sets application tag name.
Parameters: | |
txt | tag name. |
Return value: | none |
Example:
setTag( "MyTag" );
setTimeout ( func, timeout )
Creates a timer to be fired only once.
Parameters: | |
func | callback function to be fired in interval ms. |
timeout | timeout, in miliseconds |
Return value: | |
number | timer identifier in case of success |
false | otherwise |
Example:
function T ( ) {
Print(" timer! ");
}
setTimeout( T, 1000 ); // will be fired in 1 second
SetTopObjectName( )
--
Parameters: | -- |
Return value: | -- |
Example:
SHA1 ( txt )
Returns SHA1 checksum of txt.
Parameters: | |
txt | source string. |
Return value: | |
string | SHA1 checksum. |
Example:
var s = SHA1( "Hello, world!" );
Print ( s );
// The output: 943A702D06F34599AEE1F8DA8EF9F7296031D699
System ( cmd )
Executes the command cmd, waits for cmd to exit, and returns output of cmd (stdout) as a string.
Parameters: | |
cmd | system command to be executed. |
Return value: | |
string | the stdout of execution. |
false | in case of error. |
Example:
var s = System( "cat /some/file.txt" );
Print ( s );
WiFi ()
Returns true if WiFi is enabled and active when running under Android OS, or false otherwise. For Windows and Linux always returns true.
Parameters: | none |
Return value: | |
true | for Android: WiFi activated, for Windows and Linux: always true. |
false | for Android: WiFi disabled. |
Example:
var is_connected = WiFi( );
Worker ( code, name, modified )
Creates new thread and executes the javascript code there. Current thread will not have access to new one.
Parameters: | |
code | the javascript code be executed. |
name | new worker name (optional). |
modified | Usually last modification time, converted to string. The system will not create a worker with the same name and modified, and will return false. If modified is different, older worker will be stopped, and new one created instead. Optional. |
Return value: | |
true | new thread (worker) was created. |
false | in case of error. |
Example:
var code = " Print('Hello, world!') ";
Worker( code );
Write ( fpath, txt )
Writes the content of txt to the fpath file. File is created if if does not exist. Content of the file will be overwritten if file exists.
Parameters: | |
fpath | destination file path. The path must be local |
txt | text or other data to be saved to file |
Return value: | |
true | in case of success |
false | otherwise |
Example:
var s = "Hello, world\r\n";
if( !Write("/tmp/test.txt", s) ) {
Print("Error");
}
See also
|