MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
453 stars 55 forks source link

Javascript return values discrepancies #105

Open TcT2k opened 4 years ago

TcT2k commented 4 years ago

While I am integrating WebView2 into wxWidgets somebody came across the following discrepancies in javascript return values between Edge and Edge via WebView2 APIs ExecuteScript(). I'm pretty sure these are a problem in the WebView2 API:

A small javascript function f(){var person = new Object();}f(); returns this:

Return undefined Edge backend: "null" IE backend: "undefined" Edge browser: "undefined"

This script returns function f(){var d = new Date('10/08/2017 21:30:40'); var tzoffset = d.getTimezoneOffset() * 60000; return new Date(d.getTime() - tzoffset);}f();:

Return date Edge backend: "{}" Edge browser: "Sun Oct 08 2017 23:30:40 GMT+0200 (Central European Summer Time)" IE 11 browser: "Sun Oct 08 2017 23:30:40 GMT+0200 (Central Europe Daylight Time)" Firefox: "Sun Oct 08 2017 23:30:40 GMT+0200 (Central European Summer Time)"

https://github.com/wxWidgets/wxWidgets/pull/1700#issuecomment-575794999

AB#28632017

pagoe-msft commented 4 years ago

Hi @TcT2k,

This is actually the desired behavior, for now ...

We only support values that can be serialized to JSON, and anything else, including undefined, gets returned as null. We do get more information from CDP, but (especially in the case of errors), we discard it to stay backwards compatible with a previous non-CDP implementation of ExecuteScript that returned null for these kinds of values.

Updating this behavior will be a breaking change, which is something we are more than willing to explore. I'll circle back to this thread if we update this behavior!

quietfanatic commented 4 years ago

Looks like Date objects being returned as {} is a quirk of CDP. I'm not sure there's any way to work around this on the C++ side, but in the Javascript you can work around it by calling toString() on the Date.

TcT2k commented 4 years ago

@quietfanatic

Looks like Date objects being returned as {} is a quirk of CDP. I'm not sure there's any way to work around this on the C++ side, but in the Javascript you can work around it by calling toString() on the Date.

Sure that's an easy workaround, would just be nice to have scripts the same regardless which webview get's used.

@pagoe-msft Thanks for the answer (not really sure what CDP stands for). I know the ExecuteScript documentation stats that it only returns JSON serialized values, but it was still surprising to me that a simple string was returned JSON encoded. Maybe some variant of ExecuteScript could be added that returns the results more "raw", because that would enable the host application to just use a returned string without having to decode it first.