Skip to content

Instantly share code, notes, and snippets.

@podrezo
Created December 3, 2015 20:28
Show Gist options
  • Save podrezo/5c84eb6407e94fab80cf to your computer and use it in GitHub Desktop.
Save podrezo/5c84eb6407e94fab80cf to your computer and use it in GitHub Desktop.
Interacting with Embedded Chromium and Embedded IE in .NET/WPF
<!doctype html>
<html>
<body>
<script>
function recieveMessage(message) {
document.write(message);
}
// Chrome
if(myObject && myObject.sendMessage) {
myObject.sendMessage('hello, I am Chromium!');
}
// Internet Explorer
if(window.external && window.external.SendMessage) {
myObject.sendMessage('hello, I am Internet Explorer!');
}
</script>
</body>
</html>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Initialize Internet Explorer
var ieScriptingObject = new ScriptingObject();
embeddedInternetExplorer.Navigate('/path/to/index.html');
embeddedInternetExplorer.ObjectForScripting = ieScriptingObject;
// Initialize Chromium
var cefScriptingObject = new ScriptingObject();
embeddedChromium.RegisterJsObject("myObject", cefScriptingObject);
embeddedChromium.Address = '/path/to/index.html';
// Tell them both hello from WPF
embeddedInternetExplorer.InvokeScript("recieveMessage", "Hello from WPF!");
CefSharp.WebBrowserExtensions.ExecuteScriptAsync(embeddedChromium, "recieveMessage('Hello from WPF!')");
}
}
[ComVisible(true)]
public class ScriptingObject
{
// This method is invoked from the webview
public void SendMessage(string message)
{
// Do something here with the message
Debug.WriteLine(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment