Skip to main content

JavaScript Execution Feature

The JavaScript Execution feature lets you execute arbitrary JavaScript in the browser control from C#. This is useful for calling browser APIs, querying DOM state, or running custom scripts.

Contents

Executing JavaScript

Execute a JavaScript expression and get the result:

// Execute and get the raw result (JSON-encoded)
string? result = await window.Features.JavaScript.ExecuteJavaScriptAsync(
"document.title",
CancellationToken.None
);

// Execute and deserialize to a typed result
string? title = await window.Features.JavaScript.ExecuteJavaScriptAsync<string>(
"document.title",
CancellationToken.None
);

int? count = await window.Features.JavaScript.ExecuteJavaScriptAsync<int>(
"document.querySelectorAll('button').length",
CancellationToken.None
);

The script parameter is a JavaScript expression. The result is JSON-encoded and deserialized to the requested type. If the expression evaluates to undefined, the result is null.

Returning Values

The JavaScript expression should evaluate to a JSON-serializable value:

// Primitives
string? text = await window.Features.JavaScript.ExecuteJavaScriptAsync<string>(
"document.querySelector('h1')?.textContent"
);

bool? isVisible = await window.Features.JavaScript.ExecuteJavaScriptAsync<bool>(
"document.getElementById('modal')?.style.display !== 'none'"
);

// Objects
var element = await window.Features.JavaScript.ExecuteJavaScriptAsync<JsonElement>(
"JSON.stringify({ tag: document.body.tagName, childCount: document.body.children.length })"
);

Fire-and-Forget Evaluation

For scripts where you don't need the result, use SendEvalToBrowser:

window.Features.JavaScript.SendEvalToBrowser("console.log('Hello from C#')");
window.Features.JavaScript.SendEvalToBrowser(
"document.body.style.backgroundColor = 'red'"
);

The result is sent back via a separate message if a requestId is provided.

Error Handling

JavaScript evaluation failures throw JavaScriptEvaluationException:

try {
string? result = await window.Features.JavaScript.ExecuteJavaScriptAsync<string>(
"undefinedFunction()"
);
} catch (JavaScriptEvaluationException ex) {
Console.WriteLine($"JS evaluation failed: {ex.Message}");
}
note

JavaScript execution requires the window to be ready. Use await window.WaitForReadyAsync() before executing scripts if you're calling from a startup path.

See Also