FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

WebContents ยท ElectronNET/Electron.NET Wiki ยท GitHub

WebContents

github-actions[bot] edited this page Oct 31, 2025 · 1 revision

Render and control web pages.

Overview

The Electron.WebContents API provides control over web page content within Electron windows. It handles page loading, navigation, JavaScript execution, and web page lifecycle events.

Properties

๐Ÿ“‹ int Id

Gets the unique identifier for this web contents.

๐Ÿ“‹ Session Session

Manage browser sessions, cookies, cache, proxy settings, etc.

Methods

๐ŸงŠ void ExecuteJavaScriptAsync(string code, bool userGesture = false)

Evaluates script code in page.

In the browser window some HTML APIs like requestFullScreen can only be invoked by a gesture from the user. Setting userGesture to true will remove this limitation.

Code execution will be suspended until web page stop loading.

Parameters:

  • code - The code to execute
  • userGesture - if set to true simulate a user gesture

Returns:

The result of the executed code.

๐ŸงŠ Task<PrinterInfo[]> GetPrintersAsync()

Get system printers.

Returns:

Array of available printers.

๐ŸงŠ Task<string> GetUrl()

Get the URL of the loaded page.

It's useful if a web-server redirects you and you need to know where it redirects. For instance, It's useful in case of Implicit Authorization.

Returns:

URL of the loaded page.

๐ŸงŠ void InsertCSS(bool isBrowserWindow, string path)

Inserts CSS into the web page.

See: https://www.electronjs.org/docs/api/web-contents#contentsinsertcsscss-options

Works for both BrowserWindows and BrowserViews.

Parameters:

  • isBrowserWindow - Whether the webContents belong to a BrowserWindow or not (the other option is a BrowserView)
  • path - Absolute path to the CSS file location

๐ŸงŠ Task LoadURLAsync(string url)

Loads the url in the window. The url must contain the protocol prefix.

The async method will resolve when the page has finished loading, and rejects if the page fails to load.

A noop rejection handler is already attached, which avoids unhandled rejection errors.

Loads the url in the window. The url must contain the protocol prefix, e.g. the http:// or file://. If the load should bypass http cache then use the pragma header to achieve it.

Parameters:

  • url - URL to load

๐ŸงŠ Task LoadURLAsync(string url, LoadURLOptions options)

Loads the url with additional options.

The async method will resolve when the page has finished loading, and rejects if the page fails to load.

A noop rejection handler is already attached, which avoids unhandled rejection errors.

Loads the url in the window. The url must contain the protocol prefix, e.g. the http:// or file://. If the load should bypass http cache then use the pragma header to achieve it.

Parameters:

  • url - URL to load
  • options - Loading options

๐ŸงŠ void OpenDevTools()

Opens the devtools.

๐ŸงŠ void OpenDevTools(OpenDevToolsOptions openDevToolsOptions)

Opens the devtools with options.

Parameters:

  • openDevToolsOptions - Developer tools options

๐ŸงŠ Task<bool> PrintAsync(PrintOptions options = null)

Prints window's web page.

Parameters:

  • options - Print options

Returns:

Whether the print operation succeeded.

๐ŸงŠ Task<bool> PrintToPDFAsync(string path, PrintToPDFOptions options = null)

Prints window's web page as PDF with Chromium's preview printing custom settings.The landscape will be ignored if @page CSS at-rule is used in the web page. By default, an empty options will be regarded as: Use page-break-before: always; CSS style to force to print to a new page.

Parameters:

  • path - Output file path
  • options - PDF generation options

Returns:

Whether the PDF generation succeeded.

Events

โšก InputEvent

Emitted when an input event is sent to the WebContents.

โšก OnCrashed

Emitted when the renderer process crashes or is killed.

โšก OnDidFailLoad

Emitted when the load failed.

โšก OnDidFinishLoad

Emitted when the navigation is done, i.e. the spinner of the tab has stopped spinning, and the onload event was dispatched.

โšก OnDidNavigate

Emitted when a main frame navigation is done.

โšก OnDidRedirectNavigation

Emitted after a server side redirect occurs during navigation.

โšก OnDidStartNavigation

Emitted when any frame (including main) starts navigating.

โšก OnDomReady

Emitted when the document in the top-level frame is loaded.

โšก OnWillRedirect

Emitted when a server side redirect occurs during navigation.

Usage Examples

Page Loading

// Load URL with options
await webContents.LoadURLAsync("https://example.com", new LoadURLOptions
{
    UserAgent = "MyApp/1.0",
    ExtraHeaders = "Authorization: Bearer token123"
});

// Load local file
await webContents.LoadURLAsync("file://" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "app/index.html"));

// Get current URL
var currentUrl = await webContents.GetUrl();
Console.WriteLine($"Current URL: {currentUrl}");

JavaScript Execution

// Execute simple JavaScript
var result = await webContents.ExecuteJavaScriptAsync("document.title");
Console.WriteLine($"Page title: {result}");

// Execute with user gesture simulation
await webContents.ExecuteJavaScriptAsync("document.requestFullscreen()", true);

// Execute complex code
var userAgent = await webContents.ExecuteJavaScriptAsync("navigator.userAgent");
Console.WriteLine($"User agent: {userAgent}");

Developer Tools

// Open dev tools
webContents.OpenDevTools();

// Open with specific options
webContents.OpenDevTools(new OpenDevToolsOptions
{
    Mode = DevToolsMode.Detached,
    Activate = true
});

CSS Injection

// Inject CSS file
webContents.InsertCSS(true, "styles/custom-theme.css");

// Inject CSS for BrowserView
webContents.InsertCSS(false, "styles/browser-view.css");

Printing Operations

// Print web page
var printSuccess = await webContents.PrintAsync(new PrintOptions
{
    Silent = false,
    PrintBackground = true,
    DeviceName = "My Printer"
});

if (printSuccess)
{
    Console.WriteLine("Print job sent successfully");
}

PDF Generation

// Generate PDF
var pdfSuccess = await webContents.PrintToPDFAsync("document.pdf", new PrintToPDFOptions
{
    MarginsType = PrintToPDFMarginsType.None,
    PageSize = PrintToPDFPageSize.A4,
    PrintBackground = true,
    Landscape = false
});

if (pdfSuccess)
{
    Console.WriteLine("PDF generated successfully");
}

Navigation Monitoring

// Monitor navigation events
webContents.OnDidStartNavigation += (url) =>
{
    Console.WriteLine($"Starting navigation to: {url}");
};

webContents.OnDidNavigate += (navInfo) =>
{
    Console.WriteLine($"Navigated to: {navInfo.Url}");
};

webContents.OnDidFinishLoad += () =>
{
    Console.WriteLine("Page finished loading");
};

webContents.OnDidFailLoad += (failInfo) =>
{
    Console.WriteLine($"Page failed to load: {failInfo.ErrorCode} - {failInfo.ErrorDescription}");
};

Content Interaction

// Wait for DOM ready
webContents.OnDomReady += () =>
{
    Console.WriteLine("DOM is ready");
    // Safe to execute DOM-related JavaScript now
};

// Handle page crashes
webContents.OnCrashed += (killed) =>
{
    Console.WriteLine($"Renderer crashed, killed: {killed}");
    // Optionally reload the page
};

Related APIs

Additional Resources

Wiki pages Pages 35

Clone this wiki locally


Back | FazBrowse Home | New Git URL