How do i read data as text from a BLOB Url in WebView2 ? I've tried WebView2 callback from Javascript which i couldn't make it work. I appreciate whether it is a Javascript solution or not but i prefer C++.
Unfortunately, Webview2 doesn't support async function results in ExecuteScript. I managed to get data by performing synchronous request with ajax.
wstring jquery(L"var jqry = document.createElement('script');"
L"jqry.src = 'https://code.jquery.com/jquery-3.3.1.min.js';"
L"document.getElementsByTagName('head')[0].appendChild(jqry);");
webview->ExecuteScript(jquery.c_str(), Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
[](HRESULT errorCode, PCWSTR result) -> HRESULT {
return S_OK;
}).Get());
Sleep(500);
wstring script(L"jQuery.noConflict();"
L"function testAjax() {"
L"var result='';"
L"jQuery.ajax({"
L"url:document.getElementById('test').href,"
L"async: false,"
L"success:function(data) {"
L"result = data; "
L"}"
L"});"
L"return result;"
L"}"
L"(() => {"
L"return testAjax()"
L"})();");
webview->ExecuteScript(script.c_str(), Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
[](HRESULT errorCode, LPCWSTR result) -> HRESULT {
wprintf(L"%ls\n", result);
return S_OK;
}).Get());
But synchronous calls block the web code while executing. This is not recommended, but it is ok for my case. If you are looking for another way without blocking the web code, posting the text back to the native side maybe a better idea as @David Risney suggested.