how to read the url under the mouse WebView2

profful

New member
Joined
Nov 14, 2021
Messages
4
Programming Experience
1-3
hi,
how to read the URL under the mouse with a web browser using webview2.
I write the program in VisualBasic2019
 
This works well: Found at: Mouse Event for detecting if mouse is hovering over URL

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
AddHandler WebBrowser1.Document.MouseOver, AddressOf Me.DisplayHyperlinks
End Sub

Public Sub DisplayHyperlinks(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)
TextBox4.Text = e.ToElement.GetAttribute("href")
End Sub

Or:


That appears to be for an Awesomium WebBrowser control. That is not the same as the WebView2 control, but I'm not aware of what similarities and differences there are. Did you test that solution with a WebView2 control to see whether it actually addresses the OP's question?
 
That appears to be for an Awesomium WebBrowser control. That is not the same as the WebView2 control, but I'm not aware of what similarities and differences there are. Did you test that solution with a WebView2 control to see whether it actually address the OP's question?

That appears to be for an Awesomium WebBrowser control. That is not the same as the WebView2 control, but I'm not aware of what similarities and differences there are. Did you test that solution with a WebView2 control to see whether it actually address the OP's question?

I stand corrected, I'm glad you pointed this out to me, I was distracted earlier, but managed to do some research on the OP's question and made a new reply. Thank you for letting me know.
 
Rythorian77 , I write the program in VisualBasic not in Visual C

That would be C# rather than C, which is a different language. C# code can almost always be translated to VB.NET very easily. Even a beginner can do it for simple code, if they read it with an open mind. There are numerous online converters that have various levels of effectiveness that depend on the complexity of the code and how new the language features are that it uses. There is also Instant VB that you can download from Tangible Software Solutions and install locally. It is the best and most current converter that I have encountered.
 
Rythorian77 , I write the program in VisualBasic not in Visual C

I see, did you rename your document? I found this regarding Visual Basics. The issue for this person refers to the renaming of his "startup form" (which yours is documents), but it serves the same principle. If you go to this link and look below the code at the replied answers, it may help. I will keep researching this for you.

Also: You may already know this:
Note that you need to use webView2.CoreWebView2.Navigate(url) to navigate the WebView2 to the URL.
 
how to read the URL under the mouse with a web browser using webview2

That is typically an event of the web-side DOM, such as mouseover. To interact with that you add javascript through ExecuteScriptAsync method, and to get the event back to VB app you can send messages from script with window.chrome.webview.postMessage and handle the WebMessageReceived event.

For scripts I recommend adding a .js file to project and set "Copy to Output Directory" property to "Copy if newer/always", that way you get full intellisense when writing the script in VS. Also much more readable than cramming the script into a string variable. Here is an example of such script that adds mouseover event listener to all A elements:
JavaScript:
document.querySelectorAll("a").forEach(element => {
    element.addEventListener("mouseover", e => {
        window.chrome.webview.postMessage(e.target.href);
    });
});
Then in VB code script is loaded from NavigationCompleted event:
VB.NET:
Private Sub WebViewExample_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebViewExample.NavigationCompleted
    Dim scriptPath = Path.Combine(Application.StartupPath, "script.js")
    Dim script = File.ReadAllText(scriptPath)
    WebViewExample.ExecuteScriptAsync(script)
End Sub
This example output the message to debug:
VB.NET:
Private Sub WebViewExample_WebMessageReceived(sender As Object, e As CoreWebView2WebMessageReceivedEventArgs) Handles WebViewExample.WebMessageReceived
    Debug.WriteLine(e.TryGetWebMessageAsString)
End Sub
 
Back
Top