Plugin samples

Calling JavaScript asynchronously

This plugin demonstrates calling JavaScript code and collecting its output asynchronously. An invisible WebKit instance is created in background so the JavaScript code has access to DOM. This also allows leveraging functionality of various JavaScript libraries for your plugin.

Script code:

var scriptexec = null;
var script_loaded = false;

function JavascriptCallback(res, err) {
  //process response from JavaScript executed in the invisible browser
  Script.Message("JavaScript output: " + res);
}

function TestJSExecuter(Sender) {
  if (scriptexec == null) {
     scriptexec = Script.CreateScriptableJsExecuter("");
  }
  var script_file = Script.Path + "javascript.js";
  if (script_loaded) {
    script_file = ""; //we do not need to load the script again (it's an optimization)
  }
  //load script (if script_file specified) and call WeBuilder_OnData in the script
  scriptexec.ExecuteJavaScriptFileRequest(script_file, "", "8", &JavascriptCallback)
  script_loaded = true;
}

Script.RegisterAction("", "Test JavaScript", "", &TestJSExecuter);


javascript.js

WeBuilder_OnData = function(channel, message) {
    var answer = Math.cbrt(message);

    WeBuilderData.SendNative(channel, answer, '');
}

Notes

WeBuilder_OnData is the magical function that ScriptableJsExecuter.ExecuteJavaScriptFileRequest executes.
Script is only loaded once because it remains loaded inside the WebKit engine as long as ScriptableJsExecuter object is not deleted.
Use WeBuilderData.SendNative to send response from JavaScript to plugin. Make sure that the first parameter matches channel name that was passed to WeBuilder_OnData at the time it was called.