QWebEngineUrlRequestInterceptor 不工作 [英] QWebEngineUrlRequestInterceptor not working

查看:104
本文介绍了QWebEngineUrlRequestInterceptor 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用程序从 PyQt4 迁移到 PyQt5.

Im migrating an application from PyQt4 to PyQt5.

我正在尝试覆盖请求拦截器,但由于某些奇怪的原因这不起作用,这没有被接受.我正在使用 PyQt==5.10.0

Im trying to override the request interceptor but this doesn't work for some strange reason, this is not getting picked up. I am using PyQt==5.10.0

class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
    def __init__(self, parent=None):
        super().__init__(parent)

    def interceptRequest(self, info):
        # info.setHttpHeader("X-Frame-Options", "ALLOWALL")
        print("test")
        print(info.requestUrl()) 
class MyWebEnginePage(QWebEnginePage):

    # adblocker = Filter(open('easylist.txt', encoding="utf8"))

    def __init__(self, parent=None):
        super().__init__(parent)

    def acceptNavigationRequest(self, url, _type, isMainFrame):
        # urlString = url.toString()
        # resp = False
        # resp = WebPage.adblocker.match(url.toString())
        #
        # if resp:
        #     print("Blocking url --- "+url.toString())
        #     return False
        # else:
        #     print("TYPE", _type)
        #     return True

         print(url)

         return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)

这就是我加载浏览器的方式

This is how I load the browser

# init browser
browser = QWebEngineView()
# init profile
profile = QWebEngineProfile()
# add interceptor to profile
interceptor = WebEngineUrlRequestInterceptor()
profile.setRequestInterceptor(interceptor)
# init page setting profile
page = MyWebEnginePage(profile)
page.setUrl(qurl)
browser.setPage(page)

推荐答案

问题是由使用 MyWebEnginePage 的构造函数引起的,因为您使用构造函数放置它:

The problem is caused by the constructor that is using MyWebEnginePage, as you have placed it using the constructor:

QWebEnginePage::QWebEnginePage(QObject *parent = Q_NULLPTR)

使用父父级构造一个空的 QWebEnginePage.

Constructs an empty QWebEnginePage with the parent parent.

代替第二个构造函数:

QWebEnginePage::QWebEnginePage(QWebEngineProfile *profile, QObject*parent = Q_NULLPTR)

在网络引擎配置文件中构造一个空的网络引擎页面与父母一起.

Constructs an empty web engine page in the web engine profile profile with the parent parent.

如果配置文件不是默认配置文件,调用者必须确保只要页面存在,个人资料就会一直存在.

If the profile is not the default profile, the caller must ensure that the profile stays alive for as long as the page does.

这个函数是在 Qt 5.5 中引入的.

This function was introduced in Qt 5.5.

解决方案是传递 2 个参数:profile 和 parent,如果您不打算在构造函数中添加任何内容,则无需覆盖它,如下所示:

The solution is to pass 2 parameters: the profile and the parent, also if you are not going to add anything in the constructor it is not necessary to overwrite it as shown below:

import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineProfile


class WebEngineUrlRequestInterceptor(QWebEngineUrlRequestInterceptor):
    def interceptRequest(self, info):
        # info.setHttpHeader("X-Frame-Options", "ALLOWALL")
        print("interceptRequest")
        print(info.requestUrl()) 

class MyWebEnginePage(QWebEnginePage):
    def acceptNavigationRequest(self, url, _type, isMainFrame):
        print("acceptNavigationRequest")
        print(url)
        return QWebEnginePage.acceptNavigationRequest(self, url, _type, isMainFrame)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    browser = QWebEngineView()
    interceptor = WebEngineUrlRequestInterceptor()
    profile = QWebEngineProfile()
    profile.setRequestInterceptor(interceptor)
    page = MyWebEnginePage(profile, browser)
    page.setUrl(QUrl("https://stackoverflow.com/questions/50786186/qwebengineurlrequestinterceptor-not-working"))
    browser.setPage(page)
    browser.show()
    sys.exit(app.exec_())

这篇关于QWebEngineUrlRequestInterceptor 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆