简体   繁体   English

如何使用Gtk 3.0和WebKit2 4.0处理Python中的链接点击?

[英]How to handle clicks on Links in Python with Gtk 3.0 and WebKit2 4.0?

I have created my view (wrapped in a window) and loaded an URL like this: 我已经创建了视图(包装在窗口中)并加载了这样的URL:

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). 我的“迷你浏览器”启动,我可以单击本地链接(绑定到JavaScript事件的链接或指向同一域中其他页面的链接)。 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. 我可以编写事件挂钩(onclick)来中断“微型浏览器”并根据自定义逻辑执行操作,还是可以配置跨站点链接。

Did you use a GtkLinkButton ? 您是否使用了GtkLinkBut​​ton 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:// 根据使用默认浏览器打开链接的gtk-show-uri的文档,您还需要安装gvfs以获得对URI方案(例如http://ftp://

For debian based distributions you can install gvfs just like that: 对于基于debian的发行版,您可以像这样安装gvfs:

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 如果那没有帮助,您还可以检查gtk_show_uri的错误消息,以防它返回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. 对于像您这样的自定义浏览器,根据GtkLinkBut​​ton的文档,您需要连接到activate-link信号并从处理程序中返回true……可能您已经这样做了。

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. GtkLinkButton答案确实GtkLinkButton ,因为这与GtkLinkButton完全无关,并且WebKit不使用gvfs加载链接。

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. 您可以使用WebKitWebView :: decide-policy信号来控制此行为,但是默认策略应该是打开链接。

You mentioned in your comment that you were having trouble with links with target _blank. 您在评论中提到,与目标_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. 这个问题很简单:WebKit无法自动为您打开新窗口,您必须为此编写代码。 Fortunately it's pretty easy: connect to WebKitWebView::create and follow the documentation there. 幸运的是,这非常简单:连接到WebKitWebView :: create并遵循那里的文档。 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. 基本上,您需要创建一个新的GtkWindow,创建一个相关的 WebKitWebView,将新视图打包到窗口中,连接到新视图上的WebKitWebView :: ready-to-show ,并在发出该信号时显示该窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM