Meziantou.Framework.WebPlatform.JavaScript 1.0.1

Prefix Reserved
dotnet add package Meziantou.Framework.WebPlatform.JavaScript --version 1.0.1
                    
NuGet\Install-Package Meziantou.Framework.WebPlatform.JavaScript -Version 1.0.1
                    
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="Meziantou.Framework.WebPlatform.JavaScript" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Meziantou.Framework.WebPlatform.JavaScript" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Meziantou.Framework.WebPlatform.JavaScript" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Meziantou.Framework.WebPlatform.JavaScript --version 1.0.1
                    
#r "nuget: Meziantou.Framework.WebPlatform.JavaScript, 1.0.1"
                    
#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.
#:package Meziantou.Framework.WebPlatform.JavaScript@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Meziantou.Framework.WebPlatform.JavaScript&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Meziantou.Framework.WebPlatform.JavaScript&version=1.0.1
                    
Install as a Cake Tool

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 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. 
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.

Version Downloads Last Updated
1.0.1 17 10/21/2026
1.0.0 31 10/21/2026