How to handle clicks on links in Python with Gtk 3.0 and WebKit2 4.0?

I created my view (wrapped in a window) and downloaded the following URL:

self.web_view = WebKit2.WebView() self.web_view.load_uri("https://en.wikipedia.org") 

My "mini-browser" will start up and I can click on the local links (links linked to JavaScript events or links to other pages in the same domain). But when links point to other domains, nothing happens. How to catch clicks on external links? Or how can I open these links in the default system browser?

UPDATE: links to the site are not processed by the "mini-browser." Can I write an event binding (onclick) to interrupt the "Mini Browser" and act on the basis of user logic or is there a way to configure cross-site links.

+6
source share
2 answers

Have you used GtkLinkButton ? According to the gtk-show-uri document, which uses the default browser to open links, you also need to install gvfs in order to get support for uri schemes like http:// or ftp://

For debian-based distributions, you can install gvfs this way:

sudo apt-get install gvfs gvfs-backends gvfs-fuse

If this does not help, you can also check the gtk_show_uri error message if it returns FALSE

For custom browsers like yours, according to the GtkLinkButton document , you need to connect to activate the link and return true from the handler ... maybe you already did it.

+2
source

GtkLinkButton answer really doesn't match the value because it has nothing to do with GtkLinkButton , and WebKit does not use gvfs to download links.

I do not know why cross-domain links do not work for you. They should work by default. You can control this behavior using the WebKitWebView :: solve-policy signal, but the link should be open by default.

You mentioned in your comment that you are having problems with links with target _blank. Perhaps these were the only cross-domain links that you tested? This problem is simple: WebKit cannot automatically open new windows for you, you need to write code for this. Fortunately, it's pretty simple: connect to WebKitWebView :: create and follow the documentation there. You basically need to create a new GtkWindow, create a linked WebKitWebView, pack a new view into a window, connect to WebKitWebView :: ready to display on a new view and showing the window when this signal is emitted.

0
source

All Articles