我想通过子类 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 )
就这样。