### Install and Start SSH Server in Container Source: https://teamdev.com/dotnetbrowser/blog/deploying-dotnetbrowser-applications-with-docker#creating-the-dockerfile Install the OpenSSH server within the container and start the service. This enables SSH access from the host machine. ```bash apt install -y openssh-server service ssh start ``` -------------------------------- ### C# DotNetBrowser Quick Start Source: https://teamdev.com/dotnetbrowser#can-you-backport-a-fix-from-the-latest-version-to-the-version-i-currently-use Demonstrates basic usage of DotNetBrowser to load a URL, extract text content, and print it to the console. Ensure DotNetBrowser is properly installed and configured. ```csharp Imports DotNetBrowser.Browser Imports DotNetBrowser.Engine Module Program Sub Main(args() As String) Dim builder = new EngineOptions.Builder() Using engine = EngineFactory.Create(builder.Build()) Dim browser = engine.CreateBrowser() browser.Navigation \ .LoadUrl("https://quotes.toscrape.com/random").Wait() Dim document = browser.MainFrame.Document Dim quote = document.GetElementByClassName("text")? \ .InnerText Dim author = document.GetElementByClassName("author")? \ .InnerText System.Console.WriteLine(quote) System.Console.WriteLine($"— {author}") End Using End Sub End Module ``` -------------------------------- ### Initialize DotNetBrowser and Load Page Source: https://teamdev.com/dotnetbrowser/blog/how-to-generate-pdf-in-c-sharp#results Set up DotNetBrowser with off-screen rendering and a license key, then create a browser instance to load an HTML template file. ```csharp using DotNetBrowser; using DotNetBrowser.Engine; using DotNetBrowser.Print; using System; using System.IO; using System.Threading.Tasks; class Program { private static async Task Main() { var options = new EngineOptions.Builder { RenderingMode = RenderingMode.OffScreen, LicenseKey = "your license key" }.Build(); using var engine = EngineFactory.Create(engineOptions); using var browser = engine.CreateBrowser(); // The page is a resource in the project. var pageUrl = Path.GetFullPath("template.html"); await browser.Navigation.LoadUrl(pageUrl); } } ``` -------------------------------- ### Download CRX from Chrome Web Store Source: https://teamdev.com/dotnetbrowser/docs/guides/gs/extensions#peculiarities--limitations Register the InstallExtensionCallback to get the CRX file path and copy it to a custom location. This callback is invoked when an extension is about to be installed from the Chrome Web Store. The example returns 'Cancel' to prevent default installation. ```csharp extensions.InstallExtensionHandler = new Handler(p => { string name = p.ExtensionName; string version = p.ExtensionVersion; string sourceCrxFilePath = Path.GetFullPath(p.ExtensionCrxFile); string targetCrxFilePath = Path.GetFullPath(name + "-" + version + ".crx"); try { File.Copy(sourceCrxFilePath, targetCrxFilePath); } catch (IOException e) { throw new InvalidOperationException(e.Message, e); } return InstallExtensionResponse.Cancel; }); ``` ```vbnet extensions.InstallExtensionHandler = New Handler(Of InstallExtensionParameters, InstallExtensionResponse)( Function(p) Dim name As String = p.ExtensionName Dim version As String = p.ExtensionVersion Dim sourceCrxFilePath As String = Path.GetFullPath(p.ExtensionCrxFile) Dim targetCrxFilePath As String = Path.GetFullPath(name & "-" & version & ".crx") Try File.Copy(sourceCrxFilePath, targetCrxFilePath) Catch e As IOException Throw New InvalidOperationException(e.Message, e) End Try Return InstallExtensionResponse.Cancel End Function) ``` -------------------------------- ### Create WPF Application with DotNetBrowser (C#) Source: https://teamdev.com/dotnetbrowser/docs/3/quickstart/wpf#2-get-trial-license Creates a new WPF application project named 'Example.Wpf' with DotNetBrowser integrated, using C# as the language. Replace '' with your actual license key. ```bash dotnet new dotnetbrowser.wpf.app -o Example.Wpf -li ``` -------------------------------- ### Create Startup Script for Docker Source: https://teamdev.com/dotnetbrowser/blog/deploying-dotnetbrowser-applications-with-docker#adding-the-application This script handles X server initialization for headless environments. It starts Xvfb if DISPLAY is not set, ensuring graphical applications can run without a physical display. ```shell #!/bin/sh set -e if [ -z "${DISPLAY:-}" ]; then Xvfb :0 -screen 0 1920x1080x24 & export DISPLAY=:0 fi exec dotnet Example.Docker.dll ``` -------------------------------- ### Handle Extension Installation and Copy CRX Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/extensions#extension-action-popups Register an InstallExtensionHandler to get the CRX file path and copy it to a custom location. This callback is invoked when an extension is about to be installed from the Chrome Web Store. It returns InstallExtensionResponse.Cancel to prevent the default installation. ```csharp extensions.InstallExtensionHandler = new Handler(p => { string name = p.ExtensionName; string version = p.ExtensionVersion; string sourceCrxFilePath = Path.GetFullPath(p.ExtensionCrxFile); string targetCrxFilePath = Path.GetFullPath(name + "-" + version + ".crx"); try { File.Copy(sourceCrxFilePath, targetCrxFilePath); } catch (IOException e) { throw new InvalidOperationException(e.Message, e); } return InstallExtensionResponse.Cancel; }); ``` ```vbnet extensions.InstallExtensionHandler = New Handler(Of InstallExtensionParameters, InstallExtensionResponse)( Function(p) Dim name As String = p.ExtensionName Dim version As String = p.ExtensionVersion Dim sourceCrxFilePath As String = Path.GetFullPath(p.ExtensionCrxFile) Dim targetCrxFilePath As String = Path.GetFullPath(name & "-" & version & ".crx") Try File.Copy(sourceCrxFilePath, targetCrxFilePath) Catch e As IOException Throw New InvalidOperationException(e.Message, e) End Try Return InstallExtensionResponse.Cancel End Function) ``` -------------------------------- ### Initialize DotNetBrowser and Load HTML Page Source: https://teamdev.com/dotnetbrowser/blog/how-to-generate-pdf-in-c-sharp#step-3-load-the-page Set up the DotNetBrowser engine with off-screen rendering and a license key. Then, create a browser instance and load a local HTML file. ```csharp using DotNetBrowser; using DotNetBrowser.Engine; using DotNetBrowser.Print using System; using System.IO; using System.Threading.Tasks; class Program { private static async Task Main() { var options = new EngineOptions.Builder { RenderingMode = RenderingMode.OffScreen, LicenseKey = "your license key" }.Build(); using var engine = EngineFactory.Create(engineOptions); using var browser = engine.CreateBrowser(); // The page is a resource in the project. var pageUrl = Path.GetFullPath("template.html"); await browser.Navigation.LoadUrl(pageUrl); } } ``` -------------------------------- ### Handle Extension Installation and Copy CRX - VB.NET Source: https://teamdev.com/dotnetbrowser/docs/guides/gs/extensions#uninstalling-extensions This VB.NET example demonstrates how to use the `InstallExtensionHandler` to capture and save CRX files from the Chrome Web Store. The installation is then cancelled. ```vbnet extensions.InstallExtensionHandler = New Handler(Of InstallExtensionParameters, InstallExtensionResponse)( Function(p) Dim name As String = p.ExtensionName Dim version As String = p.ExtensionVersion Dim sourceCrxFilePath As String = Path.GetFullPath(p.ExtensionCrxFile) Dim targetCrxFilePath As String = Path.GetFullPath(name & "-" & version & ".crx") Try File.Copy(sourceCrxFilePath, targetCrxFilePath) Catch e As IOException Throw New InvalidOperationException(e.Message, e) End Try Return InstallExtensionResponse.Cancel End Function) ``` -------------------------------- ### Detecting Cast Session Start Failures with Event Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/media#camera--microphone Use the `ICastSessions.StartFailed` event to detect when a cast session fails to start, for example, if the media receiver is not found or has disconnected. The event provides an `ErrorMessage`. ```csharp IMediaReceiver receiver = await mediaReceivers.RetrieveAsync(it => it.Name.Contains("Samsung")); profile.MediaCasting.CastSessions.StartFailed += (o, args) => { string errorMessage = args.ErrorMessage; }; ICastSession castSession = await browser.Cast.CastContent(receiver); ``` ```vb Dim receiver As IMediaReceiver = Await mediaReceivers.RetrieveAsync(Function(it) it.Name.Contains("Samsung")) AddHandler profile.MediaCasting.CastSessions.StartFailed, Sub(o, args) Dim errorMessage As String = args.ErrorMessage End Sub Dim castSession As ICastSession = Await browser.Cast.CastContent(receiver) ``` -------------------------------- ### Detecting Cast Session Start Failures with Events Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/media#audio Use the `ICastSessions.StartFailed` event to detect when a cast session fails to start, for example, if the media receiver is not found or has disconnected. The event handler provides an `ErrorMessage`. ```csharp IMediaReceiver receiver = await mediaReceivers.RetrieveAsync(it => it.Name.Contains("Samsung")); profile.MediaCasting.CastSessions.StartFailed += (o, args) => { string errorMessage = args.ErrorMessage; }; ICastSession castSession = await browser.Cast.CastContent(receiver); ``` ```vb Dim receiver As IMediaReceiver = Await mediaReceivers.RetrieveAsync(Function(it) it.Name.Contains("Samsung")) AddHandler profile.MediaCasting.CastSessions.StartFailed, Sub(o, args) Dim errorMessage As String = args.ErrorMessage End Sub Dim castSession As ICastSession = Await browser.Cast.CastContent(receiver) ``` -------------------------------- ### C# Download Handling (DotNetBrowser) Source: https://teamdev.com/dotnetbrowser/blog/migrating-from-cefsharp-to-dotnetbrowser#calling-javascript-from-net Use StartDownloadHandler in DotNetBrowser to manage downloads. This example specifies the target path and handles the completion event. ```csharp browser.StartDownloadHandler = new Handler(p => { var fileName = p.Download.Info.SuggestedFileName; var targetPath = Path.GetFullPath(fileName); p.Download.Finished += (sender, args) => { Console.WriteLine("Download completed"); }; // Return StartDownloadResponse.Cancel() to cancel the download. return StartDownloadResponse.DownloadTo(targetPath); }); ``` -------------------------------- ### Open raspi-config for Auto-Login Setup Source: https://teamdev.com/dotnetbrowser/blog/kiosk-on-raspberry-with-avalonia-dotnetbrowser#launching-the-kiosk Launches the Raspberry Pi configuration utility to set up automatic login. ```bash sudo raspi-config ``` -------------------------------- ### Get All Available Plugins Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/plugins Iterates through all installed and available Chromium plugins to retrieve their name, description, and version. ```csharp foreach (Plugin plugin in engine.Profiles.Default.Plugins.AvailablePlugins) { string name = plugin.Name; string description = plugin.Description; string version = plugin.Version; } ``` ```vb For Each plugin As Plugin In engine.Profiles.Default.Plugins.AvailablePlugins Dim name As String = plugin.Name Dim description As String = plugin.Description Dim version As String = plugin.Version Next ``` -------------------------------- ### Create DotNetBrowser Console App (C#) Source: https://teamdev.com/dotnetbrowser/docs/3/quickstart/console#2-get-trial-license Creates a new .NET console application with DotNetBrowser integration. Replace `` with your actual trial license key. The project will be created in the `Example.Console` directory. ```bash dotnet new dotnetbrowser.console.app -o Example.Console -li ``` -------------------------------- ### Initialize ITextFinder in VB Source: https://teamdev.com/dotnetbrowser/docs/guides/gs/content#default-encoding Get an instance of the ITextFinder interface from the browser object to start text searching. ```vb Dim textFinder As ITextFinder = browser.TextFinder ``` -------------------------------- ### Create WPF Application with DotNetBrowser (VB) Source: https://teamdev.com/dotnetbrowser/docs/3/quickstart/wpf#2-get-trial-license Creates a new WPF application project named 'Example.Wpf' with DotNetBrowser integrated, using Visual Basic as the language. Replace '' with your actual license key. ```bash dotnet new dotnetbrowser.wpf.app -o Example.Wpf -lang VisualBasic -li ``` -------------------------------- ### Initialize ITextFinder in C# Source: https://teamdev.com/dotnetbrowser/docs/guides/gs/content#default-encoding Get an instance of the ITextFinder interface from the browser object to start text searching. ```csharp ITextFinder textFinder = browser.TextFinder; ``` -------------------------------- ### Headless Mode: DotNetBrowser IBrowser Setup Source: https://teamdev.com/dotnetbrowser/blog/migrating-from-cefsharp-to-dotnetbrowser#calling-net-from-javascript Sets up a DotNetBrowser IBrowser instance for headless operation. The IEngine is created with a specified rendering mode and disposed of using a 'using' statement. Navigation is asynchronous and can be awaited. ```csharp using DotNetBrowser.Browser; using DotNetBrowser.Engine; ... public static class Program { public static int Main(string[] args) { using (IEngine engine = EngineFactory.Create(RenderingMode.HardwareAccelerated)) { IBrowser browser = engine.CreateBrowser(); browser.Navigation .LoadUrl("https://www.google.com/").Wait(); ... // Just wait to keep the process running. Console.ReadKey(); return 0; } } } ``` -------------------------------- ### Register InstallExtensionCallback in VB.NET Source: https://teamdev.com/dotnetbrowser/docs/guides/gs/extensions#extension-action Register the InstallExtensionCallback to get the CRX file path and copy it to a custom location. This callback is invoked when an extension is about to be installed from the Chrome Web Store. It returns InstallExtensionResponse.Cancel to prevent the default installation. ```vbnet extensions.InstallExtensionHandler = New Handler(Of InstallExtensionParameters, InstallExtensionResponse)( Function(p) Dim name As String = p.ExtensionName Dim version As String = p.ExtensionVersion Dim sourceCrxFilePath As String = Path.GetFullPath(p.ExtensionCrxFile) Dim targetCrxFilePath As String = Path.GetFullPath(name & "-" & version & ".crx") Try File.Copy(sourceCrxFilePath, targetCrxFilePath) Catch e As IOException Throw New InvalidOperationException(e.Message, e) End Try Return InstallExtensionResponse.Cancel End Function) ``` -------------------------------- ### Register InstallExtensionCallback in VB Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/extensions Register the `InstallExtensionCallback` to get the CRX file path and copy it to a desired location. This callback is invoked when an extension is about to be installed from the Chrome Web Store. It returns `InstallExtensionResponse.Cancel` to prevent the default installation. ```vbnet extensions.InstallExtensionHandler = New Handler(Of InstallExtensionParameters, InstallExtensionResponse)( Function(p) Dim name As String = p.ExtensionName Dim version As String = p.ExtensionVersion Dim sourceCrxFilePath As String = Path.GetFullPath(p.ExtensionCrxFile) Dim targetCrxFilePath As String = Path.GetFullPath(name & "-" & version & ".crx") Try File.Copy(sourceCrxFilePath, targetCrxFilePath) Catch e As IOException Throw New InvalidOperationException(e.Message, e) End Try Return InstallExtensionResponse.Cancel End Function) ``` -------------------------------- ### Create EngineOptions and IEngine Instance (VB) Source: https://teamdev.com/dotnetbrowser/docs/3/guides/design Use the EngineOptions.Builder to configure rendering mode and language, then create an IEngine instance. ```vbnet Dim options As EngineOptions = New EngineOptions.Builder With { .RenderingMode = RenderingMode.HardwareAccelerated, .Language = Language.EnglishUs }.Build() Dim engine As IEngine = EngineFactory.Create(options) ``` -------------------------------- ### Configure Kiosk Auto-Start Script Source: https://teamdev.com/dotnetbrowser/blog/kiosk-on-raspberry-with-avalonia-dotnetbrowser#configure-runtime-and-deployment Creates a bash script to navigate to the application directory and launch the kiosk application using startx. Makes the script executable. ```bash #!/bin/bash cd ~/rpi startx ./RaspberryKiosk ``` ```bash chmod +x ~/start-kiosk.sh ``` -------------------------------- ### Register InstallExtensionCallback in C# Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/extensions Register the `InstallExtensionCallback` to get the CRX file path and copy it to a desired location. This callback is invoked when an extension is about to be installed from the Chrome Web Store. It returns `InstallExtensionResponse.Cancel` to prevent the default installation. ```csharp extensions.InstallExtensionHandler = new Handler(p => { string name = p.ExtensionName; string version = p.ExtensionVersion; string sourceCrxFilePath = Path.GetFullPath(p.ExtensionCrxFile); string targetCrxFilePath = Path.GetFullPath(name + "-" + version + ".crx"); try { File.Copy(sourceCrxFilePath, targetCrxFilePath); } catch (IOException e) { throw new InvalidOperationException(e.Message, e); } return InstallExtensionResponse.Cancel; }); ``` -------------------------------- ### Create EngineOptions and IEngine Instance Source: https://teamdev.com/dotnetbrowser/docs/3/guides/design#thread-safety Use the builder pattern to configure EngineOptions and then create an IEngine instance. Hardware acceleration and language can be specified. ```csharp EngineOptions options = new EngineOptions.Builder { RenderingMode = RenderingMode.HardwareAccelerated, Language = Language.EnglishUs }.Build(); IEngine engine = EngineFactory.Create(options); ``` ```vb Dim options As EngineOptions = New EngineOptions.Builder With { .RenderingMode = RenderingMode.HardwareAccelerated, .Language = Language.EnglishUs }.Build() Dim engine As IEngine = EngineFactory.Create(options) ``` -------------------------------- ### WPF CefSharp Browser Setup Source: https://teamdev.com/dotnetbrowser/blog/migrating-from-cefsharp-to-dotnetbrowser#disposing-the-browser Example of setting up a CefSharp ChromiumWebBrowser in WPF. Requires Cef.Initialize in a static constructor. ```xaml ``` ```csharp public partial class MainWindow : Window { static MainWindow() { CefSettings settings = new CefSettings(); Cef.Initialize(settings); } public MainWindow() { InitializeComponent(); } } ``` -------------------------------- ### Download CRX from Chrome Web Store Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/extensions#extension-popups Register the InstallExtensionCallback to get the CRX file path and copy it to a desired location. This callback is invoked when an extension is about to be installed from the Chrome Web Store. It returns InstallExtensionResponse.Cancel to prevent the default installation. ```csharp extensions.InstallExtensionHandler = new Handler(p => { string name = p.ExtensionName; string version = p.ExtensionVersion; string sourceCrxFilePath = Path.GetFullPath(p.ExtensionCrxFile); string targetCrxFilePath = Path.GetFullPath(name + "-" + version + ".crx"); try { File.Copy(sourceCrxFilePath, targetCrxFilePath); } catch (IOException e) { throw new InvalidOperationException(e.Message, e); } return InstallExtensionResponse.Cancel; }); ``` ```vbnet extensions.InstallExtensionHandler = New Handler(Of InstallExtensionParameters, InstallExtensionResponse)( Function(p) Dim name As String = p.ExtensionName Dim version As String = p.ExtensionVersion Dim sourceCrxFilePath As String = Path.GetFullPath(p.ExtensionCrxFile) Dim targetCrxFilePath As String = Path.GetFullPath(name & "-" & version & ".crx") Try File.Copy(sourceCrxFilePath, targetCrxFilePath) Catch e As IOException Throw New InvalidOperationException(e.Message, e) End Try Return InstallExtensionResponse.Cancel End Function) ``` -------------------------------- ### Initialize Engine with Project License (C#) Source: https://teamdev.com/dotnetbrowser/docs/3/guides/licensing Use this snippet to initialize the IEngine with a project license key in C#. Ensure the license is tied to the current namespace or an outer namespace. ```csharp namespace ProductNamespace { namespace MyNamespace { public class MyClass { public void InitializeEngine() { IEngine engine = EngineFactory.Create(new EngineOptions.Builder { LicenseKey = "your_project_license_key" }.Build()); } } } } ``` -------------------------------- ### Handle Navigation Started Event Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/navigation Subscribe to the NavigationStarted event to get information about the URL and whether the navigation is within the same document. ```csharp navigation.NavigationStarted += (s, e) => { string url = e.Url; // Indicates whether the navigation will be performed // in the scope of the same document. bool isSameDocument = e.IsSameDocument; }; ``` ```vb AddHandler navigation.NavigationStarted, Sub(s, e) Dim url As String = e.Url ' Indicates whether the navigation will be performed ' in the scope of the same document. Dim isSameDocument As Boolean = e.IsSameDocument End Sub ``` -------------------------------- ### Complete WPF Application Example Source: https://teamdev.com/dotnetbrowser/docs/3/guides/gs/browser-view#off-screen This example demonstrates the full integration of DotNetBrowser into a WPF application, including engine and browser creation, BrowserView initialization, and navigation. ```xml ``` ```csharp // MainWindow.xaml.cs using System; using System.Windows; using DotNetBrowser.Browser; using DotNetBrowser.Engine; namespace Embedding.Wpf { /// /// This example demonstrates how to embed DotNetBrowser /// into a WPF application. /// public partial class MainWindow : Window { private const string Url = "https://html5test.teamdev.com/"; private readonly IBrowser browser; private readonly IEngine engine; public MainWindow() { // Create and initialize the IEngine instance. EngineOptions engineOptions = new EngineOptions.Builder { RenderingMode = RenderingMode.HardwareAccelerated }.Build(); engine = EngineFactory.Create(engineOptions); // Create the IBrowser instance. browser = engine.CreateBrowser(); InitializeComponent(); // Initialize the WPF BrowserView control. browserView.InitializeFrom(browser); browser.Navigation.LoadUrl(Url); } private void Window_Closed(object sender, EventArgs e) { browser?.Dispose(); engine?.Dispose(); } } } ``` ```vbnet ' MainWindow.xaml.vb Imports System.Windows Imports DotNetBrowser.Browser Imports DotNetBrowser.Engine Namespace Embedding.Wpf ''' ''' This example demonstrates how to embed DotNetBrowser ''' into a WPF application. ''' Partial Public Class MainWindow Inherits Window Private Const Url As String = "https://html5test.teamdev.com/" Private ReadOnly browser As IBrowser Private ReadOnly engine As IEngine Public Sub New() ' Create and initialize the IEngine instance. Dim engineOptions As EngineOptions = New EngineOptions.Builder With { .RenderingMode = RenderingMode.HardwareAccelerated }.Build() engine = EngineFactory.Create(engineOptions) ' Create the IBrowser instance. browser = engine.CreateBrowser() InitializeComponent() ' Initialize the WPF BrowserView control. browserView.InitializeFrom(browser) browser.Navigation.LoadUrl(Url) End Sub Private Sub Window_Closed(sender As Object, e As EventArgs) browser?.Dispose() engine?.Dispose() End Sub End Class End Namespace ``` -------------------------------- ### Register InstallExtensionHandler in C# Source: https://teamdev.com/dotnetbrowser/docs/guides/gs/extensions#extension-action-popups Register the InstallExtensionHandler to intercept extension installations. This example copies the CRX file to a specified location. ```csharp extensions.InstallExtensionHandler = new Handler(p => { string name = p.ExtensionName; string version = p.ExtensionVersion; string sourceCrxFilePath = Path.GetFullPath(p.ExtensionCrxFile); string targetCrxFilePath = Path.GetFullPath(name + "-" + version + ".crx"); try { File.Copy(sourceCrxFilePath, targetCrxFilePath); } catch (IOException e) { throw new InvalidOperationException(e.Message, e); } return InstallExtensionResponse.Cancel; }); ``` -------------------------------- ### Initialize Engine with Project License (VB) Source: https://teamdev.com/dotnetbrowser/docs/3/guides/licensing Use this snippet to initialize the IEngine with a project license key in VB. Ensure the license is tied to the current namespace or an outer namespace. ```vbnet Namespace ProductNamespace Namespace MyNamespace Public Class [MyClass] Public Sub InitializeEngine() Dim engine As IEngine = EngineFactory.Create(New EngineOptions.Builder With { .LicenseKey = "your_project_license_key" }.Build()) End Sub End Class End Namespace End Namespace ```