-1

In a C++ (MFC) app using WebView2, I can't find a way to simply wait until the script passed to AddScriptToExecuteOnDocumentCreated() is ready. My ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler() is just never called if I add some waiting code (e.g. WaitForSingleObject()) after calling AddScriptToExecuteOnDocumentCreated().

Without the wait, the ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler() is reached and I can see it's invoked on the message thread. Then, obviously, if I have to make the UI (i.e. message thread) wait, it can't work.

Does someone know how we're supposed to use this properly?

Doc: https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2?view=webview2-1.0.1150.38#addscripttoexecuteondocumentcreated

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Cylvoon
  • 1
  • 2
  • 1
    Welcome to Stack Overflow! I noticed some formatting issues in your question. Please make sure that you read [Editing help](https://stackoverflow.com/editing-help) before asking any question. –  Mar 14 '22 at 21:50
  • As mentioned in the documentation, this method runs asynchronously, you may execute this method like `std::sync()`. I'm not sure if the method WaitForSingleObject() work. If possible, please provide relevant sample code, it will help to solve the problem. You could also simple refer to [this case](https://stackoverflow.com/questions/5645375/how-do-i-make-a-function-asynchronous-in-c). – Xudong Peng Mar 15 '22 at 06:12
  • Show the code you inject. Why does that have to 'wait'? – Poul Bak Mar 15 '22 at 13:37
  • @PoulBak It has to wait, because I need to be sure the injected script will be executed on next navigate, which doesn't seem to always be the case especially if the script is pretty big. – Cylvoon Mar 15 '22 at 16:34
  • Ok, I found a solution to my problem. Although I could make it work using the old dirty MFC message pumping trick (which I really don't like), I could finally refactor some code and call the navigate from the handler directly. Pretty simple. Thanks for your replies. ```auto res = webView->AddScriptToExecuteOnDocumentCreated(L"Some script", Microsoft::WRL::Callback( [](HRESULT error, PCWSTR id)->HRESULT { // call Navigate here return S_OK; }).Get());``` – Cylvoon Mar 15 '22 at 19:02

1 Answers1

0

Ok, I found a solution to my problem. Although I could make it work using the old dirty MFC message pumping trick (which I really don't like), I could finally refactor some code and call the navigate from the handler directly. Pretty simple. Thanks for your replies.

auto res = webView->AddScriptToExecuteOnDocumentCreated(L"Some script", Microsoft::WRL::Callback<ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler>(     [](HRESULT error, PCWSTR id)->HRESULT {     // call Navigate here   return S_OK; }).Get()); 
Cylvoon
  • 1
  • 2
  • Thanks for posting the solution for this issue. You can mark your answer as an accepted answer after 48 hrs, when it is available to mark. It can help other community members in future in similar kind of issues. Thanks for your understanding. – Xudong Peng Mar 18 '22 at 06:41