-
Notifications
You must be signed in to change notification settings - Fork 0
WebContents
Render and control web pages.
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.
Parameters:
-
url- URL to load
Description: Loads the url in the window. The url must contain the protocol prefix.
Parameters:
-
url- URL to load -
options- Loading options
Description: Loads the url with additional options.
Returns: URL of the loaded page
Description: Get the URL of the loaded page.
Parameters:
-
code- The code to execute -
userGesture(optional) - If set to true simulate a user gesture
Returns: The result of the executed code
Description: Evaluates script code in page.
Description: Opens the devtools.
Parameters:
-
openDevToolsOptions- Developer tools options
Description: Opens the devtools with options.
Parameters:
-
isBrowserWindow- Whether the webContents belong to a BrowserWindow -
path- Absolute path to the CSS file location
Description: Inserts CSS into the web page.
Returns: Array of available printers
Description: Get system printers.
Parameters:
-
options(optional) - Print options
Returns: Whether the print operation succeeded
Description: Prints window's web page.
Parameters:
-
path- Output file path -
options(optional) - PDF generation options
Returns: Whether the PDF generation succeeded
Description: Prints window's web page as PDF.
Type: int
Description: Gets the unique identifier for this web contents.
Type: Session
Description: Manage browser sessions, cookies, cache, proxy settings, etc.
Description: Emitted when the renderer process crashes or is killed.
Description: Emitted when the navigation is done, i.e. the spinner of the tab has stopped spinning, and the onload event was dispatched.
Description: Emitted when any frame (including main) starts navigating.
Description: Emitted when a main frame navigation is done.
Description: Emitted when a server side redirect occurs during navigation.
Description: Emitted after a server side redirect occurs during navigation.
Description: Emitted when the load failed.
Description: Emitted when an input event is sent to the WebContents.
Description: Emitted when the document in the top-level frame is loaded.
// 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}");// 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}");// Open dev tools
webContents.OpenDevTools();
// Open with specific options
webContents.OpenDevTools(new OpenDevToolsOptions
{
Mode = DevToolsMode.Detached,
Activate = true
});// Inject CSS file
webContents.InsertCSS(true, "styles/custom-theme.css");
// Inject CSS for BrowserView
webContents.InsertCSS(false, "styles/browser-view.css");// 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");
}// 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");
}// 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}");
};// 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
};- Electron.WindowManager - Windows containing web contents
- Electron.Session - Session management for web contents
- Electron.IpcMain - Communication with web contents
- Electron.WindowManager - Create windows with web contents
- Electron.Session - Manage session data and cookies
- Electron.IpcMain - Communicate with renderer processes
- API Overview - Explore other Electron APIs
- Electron WebContents Documentation - Official Electron web contents API
- Web Content Management - Understanding web content handling
- Security Considerations - Secure web content integration
Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.