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

ExecuteScriptAsync doesn't return fetch PromiseResult #2295

Closed
ninoxscutulata opened this issue Mar 22, 2022 · 4 comments
Closed

ExecuteScriptAsync doesn't return fetch PromiseResult #2295

ninoxscutulata opened this issue Mar 22, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@ninoxscutulata
Copy link

ninoxscutulata commented Mar 22, 2022

Version
SDK: 1.0.1189-prerelease
Runtime: 99.0.1150.36
Framework: WInForms
OS: Win10

Repro Steps

string resp1 = await webView.CoreWebView2.ExecuteScriptAsync("async function abc () { let response = await fetch('https://www.yahoo.com/tdv2_fp/api/resource/WeatherLocationService.favoriteLocation?lang=en-US&region=US&site=fp&ssl=1&crumb=N2/kCEJVXBm&returnMeta=true'); let text = await response.json();  return text;} await abc();");
string resp2 = await webView.CoreWebView2.ExecuteScriptAsync("async function abc () { let response = await fetch('https://www.yahoo.com/tdv2_fp/api/resource/WeatherLocationService.favoriteLocation?lang=en-US&region=US&site=fp&ssl=1&crumb=N2/kCEJVXBm&returnMeta=true'); let text = await response.json();  return text;} abc();");
      
Debug.WriteLine(resp1); // "null"
Debug.WriteLine(resp2); // "{}"
      
  // expected response
  // {"data":[],"meta":{}}

In the real Chrome or Edge WebBrowser Devtools Console or chrome webdriver, this script works fine.
But it doesn't work in WebView2.

I think that webview2 should also return a string response because I get a successful string response using a different kind of webdriver, but this is just my guess.

Maybe I don't know how to use it
Is there a way to return the response contents using fetch in webview2?

@ninoxscutulata ninoxscutulata added the bug Something isn't working label Mar 22, 2022
@champnic
Copy link
Member

champnic commented Mar 23, 2022

Hey @ninoxscutulata - thanks for the bug report, and sorry you are running into this. Unfortunately we don't have great support yet for getting async results from a script using ExecuteScriptAsync. The easiest work is to probably have the script send a web message and listen for it using WebMessageReceived when the work is complete. (edit: please see amaitlands comment below for a better workaround) Thanks!

@amaitland
Copy link

An alternative is to use the DevTools protocol to evaluate your JavaScript. WebView2 already has support for this. The relevant DevTools method is https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-evaluate

If your interested in a ready made library then check out an example #416 (comment)

@R0Wi
Copy link

R0Wi commented Jun 20, 2022

Was running into the same problem and solved it by using DevTools protocol method Runtime.evaluate (see https://gist.github.com/R0Wi/e1266fa4ca0dfa5a65a5f05c188f18b6 for a working example).

Alternatively you could use the NuGet package Microsoft.Web.WebView2.DevToolsProtocolExtension. The method Microsoft.Web.WebView2.Core.DevToolsProtocolExtension.DevToolsProtocolHelper.Runtime.EvaluateAsync will behave the same way under the hood but the package adds a few more dependencies to your project.

@CocoDico78
Copy link

Are there any chances of getting this reopened? Are Promsies now supported by ExecuteScriptAsyncExecuteScriptWithResultAsync`?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants