Skip to content

Instantly share code, notes, and snippets.

@amaitland
Created May 12, 2020 00:13
Show Gist options
  • Save amaitland/57201bb964c87eacb8fcda6429a0f962 to your computer and use it in GitHub Desktop.
Save amaitland/57201bb964c87eacb8fcda6429a0f962 to your computer and use it in GitHub Desktop.
LoadHtml Demo
using System.Reflection;
using System.Windows;
namespace CefSharp.MinimalExample.Wpf
{
public partial class MainWindow : Window
{
private bool _firstTime = true;
public MainWindow()
{
InitializeComponent();
//Propertys should be assigned immediately after InitializeComponent
//for simplicity
Browser.RequestHandler = new MinimalExampleHandler();
Browser.FrameLoadEnd += OnBrowserFrameLoadEnd;
}
private async void OnBrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
if (_firstTime)
{
var executingAssembly = Assembly.GetExecutingAssembly();
var resourcePath = "CefSharp.MinimalExample.Wpf" + ".web.index.html";
using (var stream = executingAssembly.GetManifestResourceStream(resourcePath))
using (var streamReader = new System.IO.StreamReader(stream))
{
var html = streamReader.ReadToEnd();
//https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
//The cross origin request from the data uri to httpbin.org is not allowing the auth request
//It's possible this is a bug in CEF or it's a change in the Chromium CORS policy.
//Either way it's easy to workaround
//Load our fake demo url
//The Browser.LoadHtml(string, bool) overload uses a data uri
Browser.LoadHtml(html, "http://httpbin.org/cefsharpdemo", System.Text.Encoding.UTF8, oneTimeUse: true);
}
_firstTime = false;
}
else
{
if (e.Frame.IsMain)
{
var result = await e.Frame.EvaluateScriptAsync($"httpGet('http://httpbin.org/basic-auth/undefined/undefined?accept=json')");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment