How to get innerHtml with runJavaScript
-
I am using Qt 5.7. I want to grab the innerHtml. In my source code I am using QGuiApplication, QtWebView::initialize() and QQmlApplicationEngine as in the minibrowser example.
My Qml code is:
WebView { id: webView anchors.fill: parent url: "https://www.google.com" onLoadProgressChanged: { if(loadProgress === WebView.LoadSucceededStatus) webView.runJavaScript("document.documentElement.innerHTML", function(html) { console.log(result); }); utils.html(webView.runJavaScript("document.documentElement.innerHTML", function(result) { console.log(result); })); } }
And my Qt code is:
class Utils : public QObject { Q_OBJECT public: Utils(QObject* parent = 0) : QObject(parent) { } Q_INVOKABLE static QUrl fromUserInput(const QString& userInput); Q_INVOKABLE static void html(const QString html); }; QUrl Utils::fromUserInput(const QString userInput) { if (userInput.isEmpty()) return QUrl::fromUserInput("about:blank"); const QUrl result = QUrl::fromUserInput(userInput); return result.isValid() ? result : QUrl::fromUserInput("about:blank"); } void Utils::html(const QString html) { qDebug()<<html; }
This returns an empty QString.
What am I doing wrong?
-
@bandito
first of all why do you create a new thread and don't continue the initial one regarding this topic?second, your code should rather look like this:
onLoadProgressChanged: { if(loadProgress === WebView.LoadSucceededStatus) webView.runJavaScript("document.documentElement.innerHTML", function(result) { utils.html(result); }); }
-
@raven-worx said in How to get innerHtml with runJavaScript:
onLoadProgressChanged: {
if(loadProgress === WebView.LoadSucceededStatus)
webView.runJavaScript("document.documentElement.innerHTML", function(result) {
utils.html(result); });
}That worked great.
Thank you very much for the reply and sorry about the extra thread.
I did notice that part of the reason I wasn't getting the html was because
WebView.LoadSucceededStatus
can happen before the page is completely loaded. I notice it happens a few times during a page load. The work around I used wasif(webView.loadProgress === 100)
. Works like a charm.