根据文档,当 webkit 创建新窗口时会调用 create-new-window 信号。我一直在尝试覆盖它来处理<a target='_blank'
PyGTK webkit 浏览器中的链接。在 WebView 的子类中,我有:
...
self.connect("create-web-view", self.newWin)
...
def newWin(view, frame, data):
print view.get_property('uri')
print frame.get_property('uri')
print data.get_property('uri')
单击新窗口链接时调用它,但由于某种原因,所有这些对象都显示相同的 url,终端打印出当前页面 url 3 次。如何找到应该传递到新窗口的 url?
感谢 ptomato,我找到了解决方案。将信号设置为此功能有效:
...
self.connect("new-window-policy-decision-requested", self.newWin) #requires webkit 1.1.4
...
def newWin(self, view, frame, request, nav_action, policy_decision):
"""
Calls the default browser on external link requests.
"""
functiontoviewurl(request.get_uri())
# According to the documentation: http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitwebview.html#WebKitWebView-new-window-policy-decision-requested
# call ignore on the policy decision, then return true (that is, we handled it).
policy_decision.ignore()
return True