Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you retreive the result of a JS callback function called by a host object? #515

Open
G33kDude opened this issue Oct 9, 2020 · 5 comments
Labels
feature request feature request tracked We are tracking this work internally.

Comments

@G33kDude
Copy link

G33kDude commented Oct 9, 2020

I've been experimenting with the AddHostObjectToScript functionality, and am most intrigued by the ability to pass callback functions back and forth between the host application and the WebView2 instance. I've had success in invoking a callback function as defined in JavaScript (such as showing a page alert), but it is not clear to me how to retrieve the result of the callback function after it has finished its asynchronous execution.

Here is some mock JavaScript and C++ pseudo code which may give a bit more insight into the situation. I'm developing my application in another language (not C++) so I may have made some error in the implementation below, but I think it should get the point across.

window.chrome.webview.hostObjects.hostObject.callback_test( () => 42 );
HRESULT callback_test(IDispatch* callbackParameter)
{
    VARIANT result;
    result.vt = 0;
    callbackParameter->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, 0, &result, 0, 0);
    cout << result.vt; // 0
}

It appears from my tests that the IDispatch interface for the callback function does not alter the data pointed to by the pVarResult parameter. My hope was that it would return a new IDispatch object representing a promise, where I could invoke a then method and give it a host object to be called once the result was available.

I suppose I could rewrite the JavaScript to explicitly invoke a host object instead of returning a value, but that seems non-ideal. Is there there a way to retrieve the return value, or, if not, is there planned to be a way to retrieve it?

AB#29826508

@ysc3839
Copy link

ysc3839 commented Oct 9, 2020

Personally I suggest not using host object. Instead, use a helper JavaScript to do the communication.
I modified the JavaScript code of WebViewJavascriptBridge to make it works with Edge WebView2 in my project.
If you want, I can make a standalone project.

@champnic champnic added feature request feature request tracked We are tracking this work internally. labels Oct 16, 2020
@champnic
Copy link
Member

I don't think this is currently supported, so I added a scenario to our backlog. Thanks!

@rshunter1982
Copy link

@ysc3839 , I'm trying to use WebView2 to get a JWT after login. I'm currently grabbing the html content, is there an easier way?

@jaredkrinke
Copy link

This issue makes it clear that you can't obtain the result of invoking a JavaScript callback set on a native host object from native code via Invoke, but is it at least possible to know when the callback has completed?

For what it's worth, it's possible to hack around this limitation using ExecuteScript:

  • Given a native host object with a property named OnSomethingHappened that holds a JavaScript callback
  • If that host object is exposed as chrome.webview.hostObjects.sync.someHostObject (via AddHostObjectToScript)
  • You can call ExecuteScript(L"chrome.webview.hostObjects.sync.someHostObject.OnSomethingHappened()", ...) and supply ICoreWebView2ExecuteScriptCompletedHandler to get the result (as a JSON-ified string)

@xiaoyaocode163
Copy link

js get result from vb6(host exe)
vb6 edge webview2 demo by IUnknown,without rc6.dll-VBForums

https://www.vbforums.com/showthread.php?900231-vb6-edge-webview2-demo-by-IUnknown%26%2365292%3Bwithout-rc6-dll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request feature request tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

6 participants