Meziantou.Framework.WebPlatform.JavaScript
1.0.0
Prefix Reserved
See the version list below for details.
dotnet add package Meziantou.Framework.WebPlatform.JavaScript --version 1.0.0
NuGet\Install-Package Meziantou.Framework.WebPlatform.JavaScript -Version 1.0.0
<PackageReference Include="Meziantou.Framework.WebPlatform.JavaScript" Version="1.0.0" />
<PackageVersion Include="Meziantou.Framework.WebPlatform.JavaScript" Version="1.0.0" />
<PackageReference Include="Meziantou.Framework.WebPlatform.JavaScript" />
paket add Meziantou.Framework.WebPlatform.JavaScript --version 1.0.0
#r "nuget: Meziantou.Framework.WebPlatform.JavaScript, 1.0.0"
#:package Meziantou.Framework.WebPlatform.JavaScript@1.0.0
#addin nuget:?package=Meziantou.Framework.WebPlatform.JavaScript&version=1.0.0
#tool nuget:?package=Meziantou.Framework.WebPlatform.JavaScript&version=1.0.0
Meziantou.Framework.WebPlatform.JavaScript
Runs the scripts of the documents of Meziantou.Framework.WebPlatform with
Meziantou.Framework.JavaScriptEngine: a ScriptEngine with the
WebIDL bindings of the DOM.
using var engine = new JavaScriptScriptEngine(new JavaScriptScriptEngineOptions
{
Timeout = TimeSpan.FromSeconds(5), // per script or callback
});
using var context = new BrowsingContext(new BrowsingContextOptions
{
ScriptEngine = engine,
DocumentLoader = DocumentLoader.Create(), // optional: external scripts, modules, fetch, XMLHttpRequest, WebSocket, EventSource...
UnhandledScriptError = (window, error) => Console.Error.WriteLine(error),
ConsoleMessage = (window, message) => Console.WriteLine(message.Text), // console.log(), console.count()...
});
var document = await context.NavigateAsync("https://example.com/", _ => """
<title>Example</title>
<ul id=list></ul>
<script>
setTimeout(() => document.getElementById('list').innerHTML = '<li>a<li>b', 100);
</script>
""");
// Run the tasks and the timers until there is nothing left
do { await context.EventLoop.RunUntilIdleAsync(); } while (await context.EventLoop.WaitForNextTimerAsync());
Assert.Equal("<li>a</li><li>b</li>", document.GetElementById("list")!.InnerHtml);
// Evaluate code in the realm of a window
var title = engine.Evaluate(context.Window, "document.title");
Assert.Equal("Example", title.AsString());
The windows of the browsing contexts that use an engine share its JsEngine (the similar-origin window agent); each
window is the global object of its own realm. A worker gets its own engine, with the same options, on its own thread.
JavaScriptScriptEngineOptions limits the resources of each entry into JavaScript (a script, an event listener, a timer
callback...): Timeout (10 seconds by default), MemoryLimit (256 MB allocated by default), MaxStatements,
RecursionLimit (512 calls by default; null is the limit of the engine, 10,000 calls) and RegexTimeout.
TimeProvider is the clock of Date, and ConfigureEngine sets the other options of the engine (e.g. the time zone and
the default locale of Intl). The scripts only see the DOM and the JavaScript built-ins: the engine has no access to
.NET types.
A subclass can add its own functions to the global objects in OnRealmCreated:
/// <summary>A script engine whose windows have a <c>hostAdd</c> function.</summary>
private sealed class HostFunctionsScriptEngine : JavaScriptScriptEngine
{
protected override void OnRealmCreated(GlobalScope global, JsObject globalObject)
{
var realm = Engine.CurrentRealm!;
globalObject.SetProperty("hostAdd", realm.CreateFunction("hostAdd", 2, (_, arguments) => arguments[0].AsNumber() + arguments[1].AsNumber()));
}
}
See scripting.md for the threat model and the integration with the processing model of the HTML Standard.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Meziantou.Framework.JavaScriptEngine (= 1.0.0)
- Meziantou.Framework.WebPlatform (= 1.0.0)
-
net11.0
- Meziantou.Framework.JavaScriptEngine (= 1.0.0)
- Meziantou.Framework.WebPlatform (= 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.