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
Forum Updated to NodeBB v4.3 + New Features

How to get innerHtml with runJavaScript

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 986 Views 1 Watching
  • 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.
  • banditoB Offline
    banditoB Offline
    bandito
    wrote on last edited by bandito
    #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?

    raven-worxR 1 Reply Last reply
    0
    • banditoB 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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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
      • banditoB Offline
        banditoB Offline
        bandito
        wrote on 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

        • Login

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