Use V8 JavaScript Engine via ClearScript Library with UiPath

Bridge

V8 is a JavaScript engine from Google which is used in the Chrome Browser, Node.js and other applications. It implements ECMAScript (the JavaScript standard) and WebAssembly.
ClearScript from Microsoft is a dotNET assembly that makes it easy to add this kind of scripting to your UiPath workflow, because it contains the V8 engine in its native assembly packages. Compared to the approach of using Node.js, described in my tutorial, here the V8 engine is used via ClearScript and the integration can be done via the Invoke Code activity.

Preparation

First of all it is necessary to add the ClearScript packages via Manage Packages.

uipath_clearscript001

All packages and its dependencies are available at the nuget repository.

uipath_clearscript002

Now it is necessary to copy the native V8 engine into a path, where the V8 assembly of ClearScript expects it. In my case I use the win-x64 assembly.

All preparations are done now and we can use the V8 JavaScript in our UiPath workflow.

Using

In the next step we import the necessary namespaces Microsoft.ClearScript, Microsoft.ClearScript.JavaScript and Microsoft.ClearScript.V8.

uipath_clearscript004

Now we can use the example code from the ClearScript project, or in this case only a part of it.

// Begin----------------------------------------------------------------

using (var engine = new Microsoft.ClearScript.V8.V8ScriptEngine()) {

      // expose a host type
      engine.AddHostType("Console", typeof(Console));
      engine.Execute("Console.WriteLine('{0} is an interesting number.', Math.PI)");

}

// End------------------------------------------------------------------

And it delivers exact the expected result.

uipath_clearscript005

Conclusion

As we can see, the using of the V8 JavaScript engine via ClearScript in UiPath is very easy. This is another way to realize a seamless integration of JavaScript into UiPath workflows. However, before distributing the V8 NuGet package in a landscape, you should add the necessary native assembly (or assemblies) to it. Also it offers us the possibility to create a cross-platform approach, so we can use it with Windows, Linux and macOS.

Test Environment

I tested this approach with UiPath Studio 22.10 in Windows compatibility mode and with dotNET 6 in a Linux environment, it worked without any problems.
3 Likes

Hi @StefanSchnell ,
We are using inject javascript activity to fetch data from the chrome browser. Unfortunately the browser is not accepting the eval fun anymore to convert a string into window object.
EvalError: Refused to evaluate a string as JavaScript because ‘unsafe-eval’ is not an allowed source of script in the following Content Security Policy directive: “script-src ‘self’ ‘wasm-unsafe-eval’”.

Is there any alternative for the eval fun?

Regards,

1 Like