Qt WebKit – How to Get HTML Content in QML

application-developmentqmlubuntu-sdk

My application is built with QML (Ubuntu SDK), and I need to get the HTML content from a page loaded with a Qt WebView after the page is loaded. Based on some initial research, it would appear that this used to be directly supported, but not anymore. I'm using Qt 5/QtWebKit 3. How can I get the source from the page with JavaScript?

Edit: I found out about the experimental object, but I've still not found what I need. More here: http://rschroll.github.io/beru/2013/08/21/qtwebview.experimental.html

Edit: just a broader perspective on what I'm trying to do. My application attaches to a user's Freshbooks account through OAuth. After they've entered their credentials, I need to be able to access the OAuth verifier code from the content of the page. Via the OAuth protocol, I don't know of any other way to get this code. I can't even have the user copy-paste it over, because you can't select text in the WebView 🙁

Best Answer

For your first question, you should be able to do this by running some Javascript inside the WebView with WebView.experimental.evaluateJavaScript(). This Javascript could get the page source (or better yet, just the content you want, since it'll be better at parsing HTML than the Javascript-in-QML) and report it back via the document title. For example,

webview.experimental.evaluateJavaScript(
    "document.title = document.getElementById('id').innerHTML")

I'll admit that I don't know much about OAuth, but I can't help but feel you're doing something wrong if you need to do this. Have you checked out Online Accounts or this OAuth plugin?