QWebEngineUrlRequestInterceptor not working

荒凉一梦 提交于 2021-01-28 06:07:14

问题


Im migrating an application from PyQt4 to PyQt5.

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)

回答1:


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)

Constructs an empty QWebEnginePage with the parent parent.

Instead of the second constructor:

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.

This function was introduced in Qt 5.5.

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_())


来源:https://stackoverflow.com/questions/50786186/qwebengineurlrequestinterceptor-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!