Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to get innerHtml with runJavaScript

How to get innerHtml with runJavaScript

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 961 Views
  • 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.
  • B Offline
    B Offline
    bandito
    wrote on 7 Jul 2018, 15:25 last edited by bandito 7 Jul 2018, 15:26
    #1

    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?

    R 1 Reply Last reply 9 Jul 2018, 06:55
    0
    • B bandito
      7 Jul 2018, 15:25

      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?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 9 Jul 2018, 06:55 last edited by
      #2

      @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
      2
      • B Offline
        B Offline
        bandito
        wrote on 9 Jul 2018, 20:58 last edited by
        #3

        @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
        0

        1/3

        7 Jul 2018, 15:25

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved