AngleSharp.Js 1.1.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package AngleSharp.Js --version 1.1.0
                    
NuGet\Install-Package AngleSharp.Js -Version 1.1.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.

                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.

                    
Directory.Packages.props

                    
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 AngleSharp.Js --version 1.1.0
                    
#r "nuget: AngleSharp.Js, 1.1.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.
#:package AngleSharp.Js@1.1.0
                    
#: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=AngleSharp.Js&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=AngleSharp.Js&version=1.1.0
                    
Install as a Cake Tool

logo

AngleSharp.Js

CI GitHub Tag NuGet Count Issues Open CLA Assistant

AngleSharp.Js extends the core AngleSharp library with a .NET-based JavaScript engine.

Basic Configuration

If you just want a configuration that works you should use the following code:

var config = Configuration.Default
    .WithJs(); // from AngleSharp.Js

For many real-world scripts this is only the starting point. AngleSharp.Js exposes DOM APIs from the AngleSharp services you register, so you usually also want to bring in packages such as AngleSharp.Io and AngleSharp.Css to get a broader browser-like surface:

var config = Configuration.Default
    .WithDefaultLoader(new LoaderOptions { IsResourceLoadingEnabled = true }) // AngleSharp.Io
    .WithCss()                                                                 // AngleSharp.Css
    .WithJs()
    .WithEventLoop();

Depending on the script, additional AngleSharp libraries may be required as well. For very special scenarios, packages such as AngleSharp.Wasm can also be relevant.

This will register a scripting engine for JS files. The engine can be tuned by passing a JsScriptingOptions instance to WithJs:

var config = Configuration.Default
    .WithJs(new JsScriptingOptions
    {
        // how deep a script may recurse before the engine reports
        // "Maximum call stack size exceeded" (10000 by default)
        MaxCallStackDepth = 5000,
    });

You can also use this part with a console for logging. The call for this is WithConsoleLogger, e.g.,

var config = Configuration.Default
    .WithJs()
    .WithConsoleLogger(ctx => new MyConsoleLogger(ctx));

in the previous example MyConsoleLogger refers to a class implementing the IConsoleLogger interface. Examples of classes implementing this interface are available in our samples repository.

Extension Methods

This plugin also delivers some extension methods to be used together with elements from AngleSharp, e.g., IDocument or IElement.

For instance, the following waits until a stable point has been reached (and the document is fully available):

var context = BrowsingContext.New(config);
var document = await context.OpenAsync(address)
    .WaitUntilAvailable();

Scripts can also be run in the context of the document, where the result of the last expression is returned:

var numEntries = document.ExecuteScript("document.querySelectorAll('div').length");

Vision and Status

The repository contains DOM bindings for the Jint JavaScript engine. Jint implements ECMAScript 2015 (ES6) through ECMAScript 2025 — classes and private fields, modules, generators, async/await, Promise, Proxy/Reflect, Symbol, typed arrays, BigInt, optional chaining, and iterator helpers among them — and provides the basis for evaluating JavaScripts in the context of the AngleSharp DOM representation. See the Jint feature list for the authoritative matrix.

The library comes with a service that exposes WithJs to IConfiguration. This enables automatic evaluation of script elements that have a valid JavaScript type (or without any explicit type, since JavaScript is the default one), including type="module" and type="importmap". The DOM bindings are generated on the fly via reflection. Since Jint is interpreting JavaScript, the library can be published in compatibility with .NET Standard (2.0). The downside is that the performance is definitely worse than any compiled JavaScript engine would deliver. For most scripts that should not be a big issue.

Features

  • Support of modern JavaScript (ES2015 through ES2025) through Jint
  • Connection to the DOM
  • ES modules and import maps (