Search code examples
javascriptpythonpyqt5qtwebengineqwebengineview

How to run JavaScript code before document is fully loaded with QWebEngineView?


I would like to evaluate JavaScript before page's document is constructed and its scripts evaluated, in essence emulating Page.addScriptToEvaluateOnNewDocument of Chrome Devtools Protocol.

I tried to evaluate the JS code on loadStarted and loadProgress events of QWebEngineView but the script was evaluated only after page fully loaded.


Solution

  • You can use the QWebEngineScript to do this. It allows you to set an injection point and execution world for your script (similar to e.g. greasemonkey).