C# Webview2 WebMessageReceived event not firing anymore after update of MS Edge browser and Webview2 runtime

T.Zacks 3,986 Reputation points
2024-03-20T19:43:04.9433333+00:00

I am developing a WinForms project using VS2022 IDE. as per our requirement I am using Webview2 to embed the browser in our WinForms. web site load properly. my requirement was if user select any data from Webview2 at runtime then I need to capture the html of that selected text. this way i did it and it was working fine.

please see me code which was working for last 30 days.

public partial class Form1 : Form
{
        Microsoft.Web.WebView2.WinForms.WebView2 webViewControl = null;
        string selectedHtml = "";
        public Form1()
        {
            InitializeComponent();
            InitBrowserControl();
        }

        private async void InitBrowserControl()
        {
            webViewControl = new Microsoft.Web.WebView2.WinForms.WebView2();
            webViewControl.Dock = System.Windows.Forms.DockStyle.Fill;

            // Handle the WebView2's CoreWebView2InitializationCompleted event
            webViewControl.CoreWebView2InitializationCompleted += async (s, evt) =>
            {
                // Inject JavaScript to retrieve selected text's HTML by javascript
                string script = @"
                    console.log('Script is running in the browser context!');
                    document.onmouseup = function() {
                        var selection = window.getSelection();
                        var range = selection.getRangeAt(0);
                        var clonedRange = range.cloneRange();
                        var div = document.createElement('div');
                        div.appendChild(clonedRange.cloneContents());
                        var selectedHtml = div.innerHTML;
                        console.log(div.innerHTML);
                        window.chrome.webview.postMessage(selectedHtml);
                    };
                ";

                await webViewControl.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(script);
                await Task.Delay(0); // Yield to event loop
            };

            // Handle the WebView2's WebMessageReceived event
            webViewControl.WebMessageReceived += (s, evt) =>
            {
                selectedHtml = evt.WebMessageAsJson;
            };

            await webViewControl.EnsureCoreWebView2Async(null);

            // Attach the event handler
            webViewControl.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
            webViewControl.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;
            webViewControl.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
            webViewControl.CoreWebView2.NavigationStarting += CoreWebView2_NavigationStarting;
            panel1.Controls.Add(webViewControl);
        }
}   

if anyone see my code then they can understand i inject a JavaScript into Webview2 browser which will fire onmouseup and store html into a js variable and later push that data to client side by window.chrome.webview.postMessage(selectedHtml)

i was getting html data from WebMessageReceived event.

everything was working as expected but just 2 days back my MS Edge Browser and Webview2 runtime was updated automatically which causes the problem. after update of MS Edge Browser and Webview2 runtime my WebMessageReceived event not firing anymore. last 2 days i searched google lot but still no luck.

Please guys help me to figure out the problem and how to fix this issue as a result WebMessageReceived event should fire like before.

my MS Edge Browser current version is 122.0.2365.92 (Official build) (64-bit) my Webview2 runtime version is also 122.0.2365.92

Please tell me if i made any mistake in code. Please help me how to figure out and fix this issue.

Thanks

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,130 questions
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,829 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,250 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more