6

I have created my view (wrapped in a window) and loaded an URL like this:

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

My "Mini-Browser" starts and I can click on local links (links which are bound to JavaScript events or links to other pages on the same domain). But when the links point to other domains, nothing happens. How do I catch clicks on external links? Or how can I open these links in the system default browser?

UPDATE: Cross site links are not handled by the "Mini-Browser". Can I write an event hook(onclick) to interrupt the "Mini-Browser" and act based on custom logic or is there a way to configure cross-site links.

Software Mechanic
  • 974
  • 2
  • 9
  • 25
Witek
  • 6,160
  • 7
  • 43
  • 63
  • You are using the default webkit ? There is a python based webkit out there for gtk: [pywebkitgtk](https://code.google.com/archive/p/pywebkitgtk), possibly it works better than the default python-webkit2 . [Here](https://stackoverflow.com/questions/647041/need-a-simple-hello-world-example-using-the-webkit-library-in-python) is some usage-example – Alex Aug 04 '16 at 13:07
  • Here an [example of a python browser](https://gist.github.com/kklimonda/890640#file-pybrowser-py) which uses gtk and webkit, I just took a try, `https://en.wikipedia.org` is working for me. Basically the same calls like you do are used .. you as well inherit from `Gtk.VBox` ? – Alex Aug 04 '16 at 13:30
  • 1
    @Alex The example is based on old gtk and webkit version :-( – Witek Aug 04 '16 at 13:33
  • So do you really want to catch these clicks and/or open them in a different browser? Or should they just be opened in the same WebView instance? The latter should just work by default. – elya5 Oct 23 '16 at 20:58
  • Some have target _blank. These I need to open in an other browser. – Witek Oct 24 '16 at 09:07

2 Answers2

1

Did you use a GtkLinkButton ? According to the doc of gtk-show-uri which uses the default-browser to open links, you additionally need to install the gvfs to get support for uri schemes such as http:// or ftp://

For debian based distributions you can install gvfs just like that:

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

If that does not help, you additionally can check the error-message of gtk_show_uri, in case it returns FALSE

For custom-browsers, like yours, according to the doc of GtkLinkButton you need to connect to the activate-link signal and return true from the handler ... probably you already did so.

Alex
  • 1,602
  • 20
  • 33
  • Is there any documentation how to use GtkLinkButton from python? – Witek Aug 03 '16 at 09:09
  • [Here is the official gtk python doc](https://developer.gnome.org/pygtk/stable/class-gtklinkbutton.html) for the link-button and [here](http://python-gtk-3-tutorial.readthedocs.io/en/latest/button_widgets.html) is some tutorial which as well provides python-snippets for the link-button. – Alex Aug 03 '16 at 13:35
  • Now I don't get it. What do I use the LinkButton for? I need the links in my WebView to work... – Witek Aug 03 '16 at 15:05
  • Connecting "activate-link" does not work either: 'TypeError: : unknown signal name: activate-link' – Witek Aug 03 '16 at 15:10
  • First thing to check: Is gvfs installed on your system ? If not, gtk3 will not be able to handle links to other domains. – Alex Aug 03 '16 at 15:26
  • If it is already installed (like all the other gvfs-packages), and if it still does not work, I would recommend to first take a try with a simple GtkLinkButton, to see if extern domains work at all. ( In order to check if your problem is a general gtk3 problem, or if it is webkit-related ) – Alex Aug 03 '16 at 15:30
  • Gtk.LinkButton("http://www.gtk.org", "Visit GTK+ Homepage") works fine. – Witek Aug 03 '16 at 15:36
  • Ok, so it seems to be a webkit-problem, not related to gtk ... at least we know that much now. – Alex Aug 04 '16 at 09:58
1

The GtkLinkButton answer is really off the mark, because this has nothing at all to do with GtkLinkButton and WebKit does not use gvfs to load links.

I don't know why cross-domain links don't work for you. They should work by default. You can control this behavior using the WebKitWebView::decide-policy signal, but the default policy should be to open the link.

You mentioned in your comment that you were having trouble with links with target _blank. Is it possible these were the only cross-domain links that you were testing? That problem is straightforward: WebKit can't open new windows for you automatically, you have to write the code for that. Fortunately it's pretty easy: connect to WebKitWebView::create and follow the documentation there. You basically need to create a new GtkWindow, create a related WebKitWebView, pack the new view into the window, connect to WebKitWebView::ready-to-show on the new view, and show the window when that signal is emitted.

Michael C.
  • 319
  • 1
  • 10