MicrosoftEdge / MicrosoftEdge/WebView2Feedback

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

Open
#515 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the AddHostObjectToScript documentation and the callback_test example using IDispatch::Invoke. Check the five-comment thread and linked AB#29826508 for the current status and intended behavior. Done means documenting or establishing whether a JavaScript callback's asynchronous return value can be retrieved by the host.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, javascript
Domain
api, desktop
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.