Last active
January 27, 2022 10:31
-
-
Save anna-dolbina/c868afff136cd9fd9948a90d11603e8b to your computer and use it in GitHub Desktop.
Code snippets for DotNetBrowser and CefSharp comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var script = @" | |
document.getElementsByName('question')[0].value = 'CefSharp Example'; | |
document.getElementsByName('btn')[0].click(); | |
"; | |
browser.ExecuteScriptAsync(script); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Execute JavaScript without returning a result. The method returns | |
// before the script has actually been executed. | |
browser.ExecuteJavaScriptAsync("alert('All Resources Have Loaded');"); | |
// Evaluate some Javascript code. The script will be executed asynchronously | |
// and the method returns a Task encapsulating the response from the | |
// JavaScript. | |
JavascriptResponse response = await browser.EvaluateScriptAsync(script); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Take a screenshot | |
var bitmapAsByteArray = await browser.CaptureScreenshotAsync(); | |
// Save the screenshot as PNG | |
var screenshotPath = Path.GetFullPath("screenshot.png"); | |
File.WriteAllBytes(screenshotPath, bitmapAsByteArray); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="CefSharpWpf.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" | |
Title="MainWindow" Height="450" Width="800"> | |
<Grid> | |
<wpf:ChromiumWebBrowser Address="https://www.google.com"/> | |
</Grid> | |
</Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IDocument document = browser.MainFrame.Document; | |
(document.GetElementByName("question") as IInputElement).Value = "DotNetBrowser Example"; | |
document.GetElementByName("btn").Click(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string title = await browser.MainFrame.ExecuteJavaScript<string>("document.title"); | |
IJsObject window = await browser.MainFrame.ExecuteJavaScript<IJsObject>("window"); | |
IElement body = await browser.MainFrame.ExecuteJavaScript<IElement>("document.body"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Take a screenshot | |
DotNetBrowser.Ui.Bitmap image = browser.TakeImage(); | |
// Convert the screenshot to System.Drawing.Bitmap and save it as PNG | |
System.Drawing.Bitmap bitmap = image.ToBitmap(); | |
bitmap.Save("screenshot.png", ImageFormat.Png); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IEngine engine = EngineFactory.Create(new EngineOptions.Builder | |
{ | |
ProprietaryFeatures = ProprietaryFeatures.H264 | ProprietaryFeatures.Aac | |
}.Build()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:WPF="clr-namespace:DotNetBrowser.Wpf;assembly=DotNetBrowser.Wpf" | |
x:Class="Embedding.Wpf.MainWindow" | |
Title="MainWindow" Height="480" Width="800" Closed="Window_Closed"> | |
<Grid> | |
<WPF:BrowserView Name="browserView" /> | |
</Grid> | |
</Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class MainWindow : Window | |
{ | |
private const string Url = "https://www.google.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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment