Eilon / MauiHybridWebView

MIT License
203 stars 41 forks source link

Add Javascript proxy support #3

Open amaitland opened 1 year ago

amaitland commented 1 year ago

I had this idea a while ago to use a Proxy in javascript to intercept method calls and dispatch them to .Net using Promises to fulfil the method calls. This is a basic prototype of that idea, should be something that can be made to work on all supported platforms.

// .Net
// Add an object called 'host' that can be called from JavaScript
myHybridWebView.ObjectHost.AddObject("host", new MyJSInvokeTarget(this));
// JavaScript
// Directly call a .Net method called CallMeFromScript
HybridWebView.SendInvokeMessageToDotNet("host", "CallMeFromScript", ["msg from js", 987]);
// Create a proxy
const myDotNetObjectProxy = HybridWebView.CreateProxy("host");
// Call the method, await the result (promise) to get the returned value.
const result = await myDotNetObjectProxy.CallMeFromScriptReturn("Hello", 42);

Still lots of things I'd like to see added/supported

amaitland commented 1 year ago

Android should now work (tested only in the emulator).

saber-wang commented 1 year ago

Android 9.0 execution failed(Chrome69)

amaitland commented 1 year ago

Android 9.0 execution failed(Chrome69)

That's expected, the browser is too old for the JavaScript in it's current form. In theory a minimum of version Chromium 74 would be required currently.

amaitland commented 1 year ago

I've changed the syntax slightly to lower the minimum required javascript version.