Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to get innerHtml with runJavaScript

    QML and Qt Quick
    2
    3
    748
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • bandito
      bandito last edited by bandito

      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?

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @bandito last edited by

        @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); });
        }
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply Reply Quote 2
        • bandito
          bandito last edited by

          @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 was if(webView.loadProgress === 100). Works like a charm.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post