Search code examples
javascriptpythonqtwebkituserscriptsspynner

QtWebKit - Userscript/Javascript Injection


I've been doing testing work in Python using QtWebkit/Spynner. As QtWebKit has Javascript support just like Chrome's Webkit, is it possible to inject a userscript or a piece of javascript at the beginning of a page just like you would a regular user script in Chrome?

Hopefully a simple question for those experience! Thanks in advance!


Solution

  • You can use the following approach:

    1. Register a handler for QWebPage.frameCreated
    2. Inside that event handler, register a new event handler for QWebFrame.javaScriptWindowObjectCleared
    3. In that second event handler, use QWebFrame.evaluateJavaScript (or Spynner's runjs method) to run your Javascript.

    This is the approach taken by capybara-webkit to execute its utility scripts. Unfortunately I can't give you example Python code because I'm not familiar with the Qt Python bindings.