Skip to content

Commit

Permalink
add support for winforms legacy browser
Browse files Browse the repository at this point in the history
  • Loading branch information
camnewnham committed Aug 17, 2024
1 parent a3b3fa3 commit 2ace42f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Eto.Wpf/Forms/Controls/SwfWebViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public SwfWebViewHandler()
};
Browser.HandleCreated += (sender, e) => HookDocumentEvents();

Browser.ObjectForScripting = new ScriptingObject(this);

Control = new swf.Integration.WindowsFormsHost
{
Child = Browser
Expand Down Expand Up @@ -131,6 +133,9 @@ public override void AttachEvent(string handler)
Callback.OnDocumentTitleChanged(Widget, new WebViewTitleEventArgs(Browser.DocumentTitle));
};
break;
case WebView.MessageReceivedEvent:
this.Browser.DocumentCompleted += OnDocumentLoadedInjectScript;
break;
default:
base.AttachEvent(handler);
break;
Expand Down Expand Up @@ -164,6 +169,37 @@ public string DocumentTitle
}
}

/// <summary>
/// Object exposed to JavaScript via window.external
/// </summary>
[ComVisible(true)]
public class ScriptingObject
{
private readonly SwfWebViewHandler Owner;
public ScriptingObject(SwfWebViewHandler owner)
{
Owner = owner;
}

/// <summary>
/// Called from JS via window.eto.postMessage (window.external.postMessage)
/// </summary>
public void postMessage(string message)
{
Owner.Callback.OnMessageReceived(Owner.Widget, new WebViewMessageEventArgs(message));

}
}

/// <summary>
/// Called on document load.
/// Wraps calls from window.external.postMessage to window.eto.postMessage
/// </summary>
private void OnDocumentLoadedInjectScript(object sender, EventArgs args)
{
ExecuteScript(@"window.eto = window.eto || {}; window.eto.postMessage = function(message) { window.external.postMessage(message); };");
}

public string ExecuteScript(string script)
{
var fullScript = string.Format("var _fn = function() {{ {0} }}; _fn();", script);
Expand Down

0 comments on commit 2ace42f

Please sign in to comment.