PRODUCTS
jScripter
Web Pipe
jCore
 
INFORMATION
Affiliate
About Us
Contact Us
 
Products . jScripter . Functions Sign In
jScripter Functions List
Append
base64Decode
base64Encode
clearInterval
clearTimeout
Connect
Dir
Exit
getName
getTag
Http
Include
Listen
MD5
MyIP
numSockets
numTimers
parseUrl
Print
Read
Send
setGlobal
setInterval
setName
setTag
setTimeout
SetTopObjectName
SHA1
System
WiFi
Worker
Write

Append ( filename, txt )

Appends the content of txt to the end of file filename file. File is created if if does not exist.

Parameters: 
filenamedestination file path. The path must be local.
txttext or other data to be inserted to the end of the file.
Return value: 
truein case of success
falseotherwise

Example:


	var s = "Hello, world\r\n";
	if( !Append("/tmp/test.txt", s) ) {
		Print("Error");
	}


base64Decode ( txt )

Decodes the string txt from base64.

Parameters: 
txtthe data in base64 encoding.
Return value: 
stringin case of success
falseotherwise

Example:


	var s = "SGVsbG8sIHdvcmxkIQ==";
	var res = base64Decode(s);
	Print(res);
	


base64Encode ( txt )

Encodes the string txt to base64.

Parameters: 
txtthe data to be encoded in base64.
Return value: 
stringin case of success
falseotherwise

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: 
intervalIdthe 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: 
timeoutIdThe 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: 
paththe path to local directory.
Return value: 
array of objectsin case of success
empty arrayif path does not exist

Each object has the following fields

namefile name.
sizefile size, bytes.
mtimelast modification time, seconds since UNIX epoch.
typefile 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: 
stringcurrent worker name

Example:


	var s = getName( );
	Print( "Name is: " + s );
	


getTag( )

Returns the global application tag name

Parameters:none
Return value: 
stringapplication 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: 
funccallback function
urlURL to load. Must begin with one of the following: "http://", "https://", "ws://" or "wss://". Optional.
datapost data or null. Optional
proxy_addrproxy server address and port (ex: 10.11.13.13:8088) or null. Optional.
headersString. Additional HTTP headers. Optional.
Return value: 
integersocket identifier in case of success
falsein 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:

socketsocket identifier
indexcounter 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
closedtrue if connection is closed
datathe data received
headersarray of strings, HTTP headers received. Present for index=0 only.
proxythe 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: 
fpathlocal path to javascript file.
Return value: 
truein case of success
falseotherwise (ex. file not found)

Example:


	Include( "/home/myuser/test.js" );
	


Listen( )

--

Parameters:--
Return value:--

Example:



	


MD5 ( txt )

Calculates MD5 checksum of message txt.

Parameters: 
txtthe data to hash.
Return value: 
stringMD5 hash

Example:


	var hash = MD5( "Hello" );
	Print( "hash=" + hash );
	


MyIP ( )

Returns current computer's external IP address..

Parameters:none
Return value: 
stringcurrent 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: 
numbernumber 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: 
numbernumber of timers for current worker

Example:


	var num = numTimers( );
	Print( "number of timers is " + num );
	


parseUrl ( url )

Breaks the provided URL into parts.

Parameters: 
urlthe URL.
Return value: 
objectin case of success
falseotherwise

The function returns the object which consists of the following fields:
schemescheme
hosthost name
pathURL path
queryquery 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: 
fpathpath to local file.
Return value: 
stringin case of success
falseotherwise

Example:


	var s = Read( "/some/file.txt" );
	Print( "File content: ", s);
	


Send ( socketId, txt )

Sends message txt to socket. Asynchrous.

Parameters: 
socketIdsocket identifier.
txtstring to send, or null to close connection
Return value: 
truein case of success
falseotherwise (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: 
funccallback function to be fired every interval ms.
intervalinterval, in miliseconds
Return value: 
numbertimer identifier in case of success
falseotherwise

Example:


	function T ( ) {
		Print(" timer! ");
	}
	setInterval( T, 1000 ); // every 1 second
	


setName ( txt )

Sets name of current worker.

Parameters: 
txtworker name.
Return value:none

Parameters:
txt - worker name.

Return value: None.

Example:


	setName( "MyThread" );
	


setTag ( txt )

Sets application tag name.

Parameters: 
txttag name.
Return value:none

Example:


	setTag( "MyTag" );
	


setTimeout ( func, timeout )

Creates a timer to be fired only once.

Parameters: 
funccallback function to be fired in interval ms.
timeouttimeout, in miliseconds
Return value: 
numbertimer identifier in case of success
falseotherwise

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: 
txtsource string.
Return value: 
stringSHA1 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: 
cmdsystem command to be executed.
Return value: 
stringthe stdout of execution.
falsein 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: 
truefor Android: WiFi activated, for Windows and Linux: always true.
falsefor 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: 
codethe javascript code be executed.
namenew worker name (optional).
modifiedUsually 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: 
truenew thread (worker) was created.
falsein 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: 
fpathdestination file path. The path must be local
txttext or other data to be saved to file
Return value: 
truein case of success
falseotherwise

Example:


	var s = "Hello, world\r\n";
	if( !Write("/tmp/test.txt", s) ) {
		Print("Error");
	}

See also
mobile version Copyright © 1999-2024 SoftCab, Inc. All Rights Reserved ·