MicrosoftEdge / MicrosoftEdge/WebView2Feedback

AddScriptToExecuteOnDocumentCreated doesn't work properly with NavigateWithWebResourceRequest

Open
#3,048 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-low tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Description
We are replacing WebBrowser control with WebView2 control in our one of the application and tried to add event listener to catch double click. It works fine when you use simple Navigate, but it doesn't work correctly when you use NavigateWithWebResourceRequest.

Version
SDK: 1.0.1462.37
Runtime: 107.0.1418.62
Framework: Win32
OS: Win10

Repro Steps

webView2->add_WebMessageReceived(Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(this, &CWebBrowser::OnWebMessageReceived).Get(), &tknWebMessageReceived);
webView2->AddScriptToExecuteOnDocumentCreated(
		L"document.addEventListener(\"dblclick\", SetClick);\
			function SetClick(evt) {\
                             alert('test');\
                             document.getElementsByClassName('L3eUgb')[0].style.backgroundColor = \"green\";\
			     window.chrome.webview.postMessage('dblclick');\
			}", Microsoft::WRL::Callback<ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler>(this, &CWebBrowser::OnAddScriptToExecuteOnDocumentCreatedCompleted).Get());
	
	//NavigateWithWebResourceRequest(url, header, postData);
	webView2->Navigate(_T("https:\\google.com"));

Works fine, we can see alert, element background color changed to green (for example) and message are received. But if we use NavigateWithWebResourceRequest (and OnNavigateCompleted raises several times), we see alert, script knows nothing about document and we don't receive message.

AB#42605853

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

Reproduce the issue using AddScriptToExecuteOnDocumentCreated with both Navigate and NavigateWithWebResourceRequest, observing OnNavigateCompleted and WebMessageReceived. Compare whether the document-created script runs against the expected document and whether the double-click message is received; done means equivalent behavior for both navigation paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.