DynamicJavascriptRuntime.Blazor.Evaluator 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DynamicJavascriptRuntime.Blazor.Evaluator --version 1.2.0                
NuGet\Install-Package DynamicJavascriptRuntime.Blazor.Evaluator -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DynamicJavascriptRuntime.Blazor.Evaluator" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DynamicJavascriptRuntime.Blazor.Evaluator --version 1.2.0                
#r "nuget: DynamicJavascriptRuntime.Blazor.Evaluator, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DynamicJavascriptRuntime.Blazor.Evaluator as a Cake Addin
#addin nuget:?package=DynamicJavascriptRuntime.Blazor.Evaluator&version=1.2.0

// Install DynamicJavascriptRuntime.Blazor.Evaluator as a Cake Tool
#tool nuget:?package=DynamicJavascriptRuntime.Blazor.Evaluator&version=1.2.0                

Blazor.DynamicJavascriptRuntime.Evaluator

Wouldn't it be really useful if you could run a line or two of Javascript in your Blazor C# app? Wouldn't it be handy if you could execute arbitrary Javascript at runtime without strings of script? Wouldn't it be a big plus if the Javascript code was declared as a dynamic object expression and could be exposed to unit tests? Wouldn't it be nice if you could consume Javascript library API's without creating interop wrappers?

Calling Javascript dynamically from C# couldn't be easier:

using (dynamic context = new EvalContext(JSRuntimeInstance))
{
	(context as EvalContext).Expression = () => context.window.location = "www.be-eval.com";
	//window.location = "www.be-eval.com";
}

When it comes to executing code with arguments, the C# parser will support this:

using (dynamic context = new EvalContext(JSRuntimeInstance))
{
	dynamic arg = new EvalContext(JSRuntimeInstance);
	dynamic arg2 = new EvalContext(JSRuntimeInstance);
	(context as EvalContext).Expression = () => context.Chart.config.data.datasets[arg.i].data.push(arg2.config.data.datasets[arg.i].data);
	//Chart.config.data.datasets[i].data.push(config.data.datasets[i].data);
}

Need to call some script that returns a value? No problem:

dynamic context = new EvalContext(runtime.Object);
(context as EvalContext).Expression = () => context.document.cookie;           
var cookie = await (context as EvalContext).InvokeAsync<string>();
//document.cookie

In order to satisfy the C# parser, by default an underscore ("_") stands in for a space character in Javascript (but this is configurable):

using (dynamic context = new EvalContext(JSRuntimeInstance))
{
	dynamic arg = new EvalContext(JSRuntimeInstance);
	(context as EvalContext).Expression = () => context.var_instance = arg.new_object();
	//var instance = new object();
}

The dynamic expression is eagerly evaluated. This means decimal arithmetic will not be mangled by Javascript:

using (dynamic context = new EvalContext(JSRuntimeInstance))
{
	(context as EvalContext).Expression = () => context.sum = 0.1M + 0.2M * 0.5M / 0.5M;
	//sum = 0.3;
}

Maybe you feel like a bit of JQuery?

using (dynamic context = new EvalContext(JsRuntime))
{
    (context as EvalContext).Expression = () => context.jQuery("body").css("overflow-y", "hidden");
	//jQuery("body").css("overflow-y", "hidden")
}

How about passing complex types as arguments? We've got you covered for anonymous types:

using (dynamic context = new EvalContext(JsRuntime))
{
    var arg = new { Property = "Value", Field = 123, Child = new { Member = new DateTime(2001, 1, 1) } };
    (context as EvalContext).Expression = () => context.myScript.set(arg);
	//myScript.set({"property":"Value","field":123,"child":{"member":"2001-01-01T00:00:00"}})
}

Passing user-defined types takes more effort, but not too much:

var settings = new EvalContextSettings();
settings.SerializableTypes.Add(typeof(Specified));
using (dynamic context = new EvalContext(JsRuntime, settings))
{
    var arg = new Specified { Member = "abc" };
    (context as EvalContext).Expression = () => context.myScript.setSpecified(arg);
	//myScript.setSpecified({"member":"abc"})
}

The execution of Javascript is performed with the eval() function, so it's imperative to sanitize user input that's passed into the Javascript runtime. You have been warned.

Setup

First, install from nuget:

Install-Package DynamicJavascriptRuntime.Blazor.Evaluator -Version 1.1.0

https://www.nuget.org/packages/DynamicJavascriptRuntime.Blazor.Evaluator/

You then need to create a script include in your index.htm:

    <script src="_content/DynamicJavascriptRuntime.Blazor.Evaluator/BlazorDynamicJavascriptRuntime.js"></script>
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.