Over the last couple of months, I have been working with Nick Orr at Goya to get a couple of things added to the BaseElements plugin, the biggest one being the ability to execute JavaScript without a web viewer.  Last week, Nick released version 3.0.0. with experimental support for doing just that. So thanks to Nick and Mark Banks for getting it out there. Its a great start!

Evaluate JavaScript

The new function, BE_EvaluateJavascript, takes a single text parameter; the JavaScript to execute. It returns the value of the last executed line in the JavaScript. Here is a simple example using the “map” method of the JavaScript array prototype.  It simply adds 3 to every element in the array.

var a = [1,2,3,6,8,9];
var resultArray = a.map ( function(n){
    return n+3;
});
resultArray

=> 4,5,6,9,11,12

Not Node or A Browser

The JavaScript is executed using an embeddable JavaScript engine called duktape. It is compatible with ECMASCript 5.1 and includes some features in the upcoming ECMASCript 6.  But it does NOT include things like AJAX requests since those come from the Browser APIs, not JavaScript itself. It also doesn’t have any of the additional APIs provided by other JavaScript engines like Node.  It covers only the features specified in the JavaScript spec, which is formally known as ECMAScript.

You can learn more about JavaScript at site like Mozilla.

Just the Beginning

This function is currently marked “experimental” and is hidden. It works, but it doesn’t show up in the function list.  This means that its feature set and API may change as new features get added or bugs fixed. In the future, I hope we can get support for CommonJS modules, HTTP requests, execute SQL and file i/o.  But you can use it right now to execute standard JavaScript without a web viewer.

Downloads

Get the Plugin from the BaseElements web site.  Download an example file below.

Example File

There’s more

The 3.0.0 release also has some other cool features.  Check out the change log for more info.