pythongtkwebkitwebkitgtkwebkit2

How to get link hover event or display target URL WebKit


I have created a more-or-less simple browser using the Python binding for WebKitGTK, my code looks like that:

import gi
gi.require_version("Gtk","3.0")
gi.require_version("WebKit2","4.0")
from gi.repository import Gtk, WebKit2
webv=WebKit2.WebView()
window=Gtk.Window()
window.add(webv)
window.show_all()
Gtk.main()

It is more complex, but it does not matter. My question is: how to a) display target URL in a bar on hover, b) if a) is not possible, get the mouseover event of the link?

I browsed the documentation, which is rich, but written only for C, and even there, I have not found anything.


Solution

  • I found the solution:

    import gi
    gi.require_version("Gtk","3.0")
    gi.require_version("WebKit2","4.0")
    from gi.repository import Gtk, WebKit2
    webv=WebKit2.WebView() # create the WebView object
    def displayuri(d,hittestresult,u): # d and u are not necessary, the name does not matter, only their position
            if (hittestresult.context_is_link()==True): # if the hover event is a link hover
                # get link URI with hittestresult.get_link_uri()
    webv.connect("mouse-target-changed",displayuri) # mouse-target-changed occurs when the cursor enters or leaves media/link
    window=Gtk.Window()#the window to place the WebView in
    window.add(webv)
    window.show_all()
    Gtk.main()# mainloop
    
    

    In summary, this detects all hovers/dishovers over media and links, then filters the link hovers, and finally gets the link URI.