サブクラス QWebEngineUrlRequestInterceptor によって別の URL リクエストをインターセプトしたい:
class RequestInterceptor(QWebEngineUrlRequestInterceptor):
def interceptRequest(self,info):
print('#################interceptRequest')
print(info.requestUrl(),info.firstPartyUrl(),info.NavigationType,info.resourceType(),info.requestMethod())
if info.requestUrl().endswith("/jquery.js"):
info.redirect('/jqueryTest.js')
app = QApplication([])
p = QWebEnginePage()
v = QWebEngineView()
v.setPage(p)
p.profile().setRequestInterceptor(RequestInterceptor())
c.registerObject('bridge', p)
url = "http://127.0.0.1:8000/test.html?t=5"
v.setUrl(QUrl(url))
v.show()
app.exec_()
コードを実行すると、インターセプターが機能しません!
誰かが私に助けてくれることを願っています、ありがとう!
PS: pythonのガベージコレクションが原因かもしれません。 そこで、コードを修正してインターセプタを変数に格納します
p.profile().setRequestInterceptor(RequestInterceptor())
に
interceptor = RequestInterceptor()
p.profile().setRequestInterceptor(interceptor )
それで全部です。