Utility functions#
General-purpose functions for scripts: logging, waiting, random numbers, hashing and encoding strings, HTTP calls. These functions do not act on the web page.
Log #
Writes one or more values to the log so you can follow how far the script has run. The log shows in
Studio (the Logs panel) and in 9Login; in 9Hits campaign macros the function prints nothing, so do
not rely on the log as your only way to know the result.
Parameters
| Name | Description |
|---|---|
msg1 |
The value to print, of any type. Objects are printed as JSON; an ArrayBuffer prints only its byte count. |
msg2, ... |
More values to print (optional), separated by tabs on the same line. |
Examples
Delay#
Pauses the script. Pass one number to pause for exactly that many milliseconds; pass two numbers to
pause for a random time between ms1 and ms2, which looks more natural.
Parameters
| Name | Description |
|---|---|
ms1 |
How long to pause, in milliseconds. When ms2 is given, this is the minimum. |
ms2 |
The maximum, in milliseconds (optional). Ignored if it is not greater than ms1. |
Example
Exit#
Ends the session and closes the browser. Use it at the end of a script, or when you want to stop the session early.
Example
GetAppVersion#
Returns the version of the app that opened the browser.
Returns
The version string, for example "6.0.0".
Example
Random#
The old name of RandomInt; it works exactly the same. New scripts should use
RandomInt.
Parameters
| Name | Description |
|---|---|
value1 |
Called with one number: the upper bound, and the result is from 0 up to but not including value1. Called with two numbers: the lower bound, which can be returned. |
value2 |
The upper bound (optional), never returned. Must be greater than value1. |
Returns
An integer.
Example
RandomInt#
Returns a random integer. Pass one number: the result is from 0 to value1. Pass two numbers: the
result is from value1 to value2. The upper bound is never returned.
Parameters
| Name | Description |
|---|---|
value1 |
Called with one number: the upper bound, and the result is from 0 up to but not including value1. Called with two numbers: the lower bound, which can be returned. |
value2 |
The upper bound (optional), never returned. Must be greater than value1. |
Returns
An integer.
Examples
RandomFloat#
Returns a random floating-point number from min to max (not including max). If either parameter
is not a number, the function returns a random number from 0 to 1.
Parameters
| Name | Description |
|---|---|
min |
The lower bound, which can be returned. |
max |
The upper bound, never returned. |
Returns
A floating-point number.
Example
RandomArray#
Picks a random element from an array, for example one keyword from a ready-made list.
Parameters
| Name | Description |
|---|---|
array |
The array to pick from; its elements can be of any type. |
Returns
The picked element, or null when the array is empty or is not an array.
Example
Md5#
Hashes a string with MD5. Good for keys or checksums; do not use it for passwords or signatures.
Parameters
| Name | Description |
|---|---|
input |
The string to hash. |
Returns
A lowercase hex hash string, 32 characters long.
Examples
Sha1#
Hashes a string with SHA-1. For anything that needs security, use Sha256.
Parameters
| Name | Description |
|---|---|
input |
The string to hash. |
Returns
A lowercase hex hash string, 40 characters long.
Example
Sha224#
Hashes a string with SHA-224.
Parameters
| Name | Description |
|---|---|
input |
The string to hash. |
Returns
A lowercase hex hash string, 56 characters long.
Example
Sha256#
Hashes a string with SHA-256, the most common choice when you need a hash.
Parameters
| Name | Description |
|---|---|
input |
The string to hash. |
Returns
A lowercase hex hash string, 64 characters long.
Sha384#
Hashes a string with SHA-384.
Parameters
| Name | Description |
|---|---|
input |
The string to hash. |
Returns
A lowercase hex hash string, 96 characters long.
Example
Sha512#
Hashes a string with SHA-512.
Parameters
| Name | Description |
|---|---|
input |
The string to hash. |
Returns
A lowercase hex hash string, 128 characters long.
Example
AesEncryptEx#
Encrypts data with AES (AES-128, AES-192 or AES-256 depending on the key length). Decrypt it with
AesDecryptEx using the same key and options.
Parameters
| Name | Description |
|---|---|
data |
The data to encrypt: a string (read as UTF-8) or an ArrayBuffer. |
key |
A key 16, 24 or 32 bytes long. A string is read as UTF-8; change that with options.keyEncoding. |
options.mode |
Encryption mode: "cbc" (default), "ctr", "ecb", "ofb" or "gcm". |
options.iv |
Initialization vector, required for every mode except "ecb". |
options.padding |
On by default (PKCS7). Pass false or "none" to turn it off. |
options.aad |
Additional authenticated data, only used with "gcm" (optional). |
options.tagLength |
Length of the "gcm" authentication tag, in bytes. Default 16. |
options.keyEncoding, options.ivEncoding, options.aadEncoding |
How key, iv and aad are read when they are strings: "utf8" (default), "hex" or "base64". |
options.inputEncoding |
How data is read when it is a string: "utf8" (default), "hex" or "base64". |
options.outputEncoding |
Output format: "base64" (default), "hex", "utf8" or "buffer" (Uint8Array). |
Returns
A base64 string of the encrypted data (or the format chosen in outputEncoding); false when the
input is invalid.
Examples
AesDecryptEx#
Decrypts data that was encrypted with AesEncryptEx, using the same key and
options.
Parameters
| Name | Description |
|---|---|
data |
The encrypted data: a base64 string (default) or an ArrayBuffer. For a string in another format, set options.inputEncoding. |
key |
The key used for encryption. |
options |
The same fields as AesEncryptEx. The defaults differ: inputEncoding is "base64" and outputEncoding is "utf8". |
Returns
The original string (UTF-8, or the format chosen in outputEncoding); false when the input is
invalid or decryption fails, including when "gcm" data fails authentication.
Example
Base64Encode#
Encodes a string or binary data as Base64. Run objects through JSON.stringify first; otherwise the
result is the encoding of the string "[object Object]".
Parameters
| Name | Description |
|---|---|
input |
A string or an ArrayBuffer. Other types are converted to a string before encoding. |
Returns
A Base64 string, or "" when input is empty, null or undefined.
Examples
Base64Decode#
Decodes a Base64 string back to text or binary data.
Parameters
| Name | Description |
|---|---|
encodedText |
The Base64 string to decode. |
returnAsText |
true (default) returns a UTF-8 string; use it only for text. For images or binary files, pass false to get an ArrayBuffer, otherwise the data gets corrupted. |
Returns
A string when returnAsText is true, otherwise an ArrayBuffer.
Examples
HttpRequest#
Sends an HTTP request from the script, for example to call an external API or to fetch data for a
macro. The request does not carry the page's cookies. Note: by default the request goes directly from
the computer, not through the session's proxy (see bypassProxy).
Syntax
Parameters
| Name | Description |
|---|---|
url |
The full address, for example "https://httpbin.org/get". |
data |
Empty or null to send a GET. With a value, it sends a POST: a string is sent as JSON (application/json), an object as a form, an ArrayBuffer as binary. |
timeout |
Maximum wait time, in milliseconds. 0 (default) waits at most 30 seconds. |
headers |
An object of headers to send, for example {"Authorization": "Bearer abc"}. Set Content-Type here to replace the type that the function picks from data. Default null. |
returnAsBuffer |
true to get body as an ArrayBuffer (images, files). Default false: body is a string. |
bypassProxy |
true (default): the request does not go through the session's proxy, so the server sees the computer's real IP. Pass false to go through the session's proxy. |
Returns
Object {status, headers, body}: status is the HTTP code, headers is a {name: value} object
and body is the response content. On a network error or timeout: {status: 0, error}, with the
reason in error.
Examples
GenerateXpath#
Builds an xpath string from a tag name, an attribute and a value, for use with functions that take an
xpath, such as ClickByXpath.
Parameters
| Name | Description |
|---|---|
tag |
Tag name, for example "a", "div", "button". |
attr |
Attribute name, for example "href", "class". Use "text" to match the text inside the element. |
value |
The value to match. Wrap it in % to choose how it matches: "%x%" means contains, "x%" means starts with, "%x" means ends with, and no % means exactly equal. |
index |
A position number appended to the end of the xpath as [index], counting from 1 as xpath does (optional). Empty or 0 appends nothing. |
Returns
An xpath string, or "" when tag, attr or value is missing or one of the three is not a
string.
Examples
GetProfileInfo #
Tells you which 9Login profile the script is running in: the profile's ID, its name and its position in the profile table when it was opened.
Returns
Object {id, title, index}, or null in 9Hits campaign macros. id is the profile ID, title is
the profile name, and index is the position counted from 0 in the table order when the profile
was opened (row 1 has index 0). To recognize a profile across several runs, use id.
Example
A function for 9Login
In 9Hits campaign macros the function returns null. When you test-run in Studio, it returns
placeholder values (id is "macro-test", title is "Studio", index is 0). Use this
function in 9Login scripts.