Search code examples
webkitpygtk

WebKit pyGTK handle custom protocol (apt://)


I'd like a regular GTK box with a web code. That code will have an install buttons with apt (Firefox is opening that URLs into the Software Center).

    self.web = builder.get_object('boxWeb')
    self.web_view = WebKit.WebView()
    self.web_view.open("http://web_with_apt_links")
    self.web_view.show()
    self.web.add(self.web_view)

But when I try it, I get an URL error:

Unable to load page
Problem occurred while loading the URL apt:package
URL cannot be shown

Can I capture the apt links in Linux? Thanks in advance!


Solution

  • You have to connect to the navigation-requested signal. Here is an example:

    from gi.repository import Gtk, WebKit
    
    class window(Gtk.Window):
        def __init__(self):
            Gtk.Window.__init__(self)
            self.connect('delete-event', Gtk.main_quit)
            webview = WebKit.WebView()
            self.add(webview)
            webview.connect('navigation-requested', self.on_navigation_requested)
            webview.open('http://google.de')
            #webview.open('apt://test')  uncomment to test apt URIs
    
        def on_navigation_requested(self, view, frame, req):
            uri = req.get_uri()
            if uri and uri.startswith('apt'):
                print('apt uri')
                return WebKit.NavigationResponse.IGNORE
            return WebKit.NavigationResponse.ACCEPT
    
    if __name__ == '__main__':
        win = window()
        win.show_all()
        Gtk.main()