SOTI MobiControl Agent JavaScript Scripting

As of version 14.0.0, the agent supports JavaScript scripts, along with legacy scripts. JavaScript scripts can be used anywhere you can use legacy scripts, including within rules and profile configurations.

JavaScript Engine

Generally, most people use JavaScript to add scripts to their HTML web pages. However, the agent has an implementation of the core language only and doesn't contain objects or methods for manipulating HTML documents.

The agent supports the JavaScript language as standardized by Standard ECMA-262 ECMAScript. The agent's JavaScript engine conforms to Edition 3 of the standard, and partially supports features of Editions 4 and higher, see Advanced ECMA Feature Support.

Working with JavaScript Modules

The JavaScript engine supports loading third-party or in-house modules which conform to the CommonJS standard.

Before you can use a module, you need to upload it to the %scripts% folder of the agent using a file sync rule. It is recommended that you create a sub-folder for each module.

For example, let's say you want to use fraction.js:

  1. On the server, create a folder for syncing scripts, something like C:\scripts. If you already have a folder for scripts, you can use that one.
  2. Create a 'fraction' subfolder and copy fraction.js (obtained from https://www.npmjs.com/package/fraction.js) into it.
  3. Create a file sync rule to download files from the server's C:\scripts folder to the device's %scripts% folder (including sub-folders) and assign the rule.
  4. Now you can send the following JavaScript script which is expected to log Result of operation: 1:
var fraction = require('fraction.js');
var result = fraction(1).div(98).mul(98) // = 1
mobicontrol.log.info('Result of operation: ' + result)

MobiControlError

The Android Agent JavaScript engine throws the Error when a programming error occurs, for example, if a function is called with fewer arguments than required, or when a constructor is passed an undefined argument. MobiControlError has the same properties as a Mozilla flavour of a standard JavaScript Error and its name property is equal to "MobiControlError".

try {
    // Call a function with missing argument
    var dialog = mobicontrol.message.createInfoDialog()
}
catch(error) {
    mobicontrol.log.warn(error.name + ' in ' + error.fileName + ' at line ' + error.lineNumber);
}
mobicontrol.log.info('Resuming the execution');

XML Support

The agent's JavaScript engine implements ECMA-357 ECMAScript for XML (E4X). That is, JavaScript has built-in APIs to manipulate XML data. For example, the following code constructs an XML object from a string, modifies the object and converts the modified object back to a string:

var xmlString = "<employees>" +
    "<employee><name>John</name><age>25</age></employee>" +
    "<employee><name>Sue</name><age>32</age></employee>" +
    "</emplyees>";
var employees = new XML(xmlString);
employees.employee[0].age = 26;
mobicontrol.log.info(employees.toString());