DynamicJavascriptRuntime.Blazor.Evaluator
1.2.0.1
See the version list below for details.
dotnet add package DynamicJavascriptRuntime.Blazor.Evaluator --version 1.2.0.1
NuGet\Install-Package DynamicJavascriptRuntime.Blazor.Evaluator -Version 1.2.0.1
<PackageReference Include="DynamicJavascriptRuntime.Blazor.Evaluator" Version="1.2.0.1" />
paket add DynamicJavascriptRuntime.Blazor.Evaluator --version 1.2.0.1
#r "nuget: DynamicJavascriptRuntime.Blazor.Evaluator, 1.2.0.1"
// Install DynamicJavascriptRuntime.Blazor.Evaluator as a Cake Addin #addin nuget:?package=DynamicJavascriptRuntime.Blazor.Evaluator&version=1.2.0.1 // Install DynamicJavascriptRuntime.Blazor.Evaluator as a Cake Tool #tool nuget:?package=DynamicJavascriptRuntime.Blazor.Evaluator&version=1.2.0.1
Blazor.DynamicJavascriptRuntime.Evaluator (A.K.A. Gasmask)
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")
}
Sorry, no $ allowed.
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.2.0.1
https://www.nuget.org/packages/DynamicJavascriptRuntime.Blazor.Evaluator/
You then need to add a script include to your index.htm:
<script src="_content/DynamicJavascriptRuntime.Blazor.Evaluator/BlazorDynamicJavascriptRuntime.js"></script>
Syntax
The are a few different syntax options for dynamic expressions:
- The
using
blocks wrapping the EvalContext are optional but prevent forgetten calls to Invoke. - Setting the
Expression
property is optional. You can chain an expression directly on the EvalContext e.g.:
dynamic context = new EvalContext();
context.alert("Gasmask");
await (context as EvalContext).InvokeVoidAsync()
Product | Versions 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. |
-
net5.0
- Microsoft.JSInterop (>= 5.0.13)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.4.0 | 426 | 11/7/2022 | |
1.3.0 | 380 | 10/5/2022 | |
1.2.0.1 | 479 | 4/3/2022 | |
1.2.0 | 508 | 2/3/2022 | |
1.1.0 | 711 | 4/3/2020 | |
1.0.10 | 531 | 1/12/2020 | |
1.0.9 | 503 | 12/13/2019 | |
1.0.7 | 346 | 11/22/2019 | |
1.0.6 | 498 | 10/8/2019 | |
1.0.5 | 489 | 10/1/2019 | |
1.0.4-preview7.19362.4 | 408 | 8/1/2019 | |
1.0.3-preview5.19227.9 | 400 | 7/29/2019 | |
1.0.2-preview5.19227.9 | 392 | 7/26/2019 | |
1.0.1-preview5.19227.9 | 428 | 7/26/2019 | |
1.0.0-preview5.19227.9 | 446 | 7/26/2019 |