Results 1 to 15 of 15

Thread: How to run JavaScript with the contencs of a QNetworkReply

  1. #1
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question How to run JavaScript with the contencs of a QNetworkReply

    Hello all I hope someone here can set me straight on this since I can't for the heck of it figure it out.
    Am no pro so please bear with me.

    I have a program that downloads a HTML file and parses out some information.
    And its all working good, but some of the information is being displayed by a JavaScript.
    So in the QString witch I have the complete source code of the page I see the code for the JavaScript.
    Not the info I want, is there a way I can run the entire script so I can get the info I need?

    here is a snip of the code showing how i store the html page.
    Qt Code:
    1. // function executed when QNetworkReply emits finished()
    2. void getHTMLFile::replyFinsihed(QNetworkReply *reply)
    3. {
    4. QByteArray qbData = reply->readAll();
    5. sData.clear();
    6. sData = qbData;
    7. // continue with parsing data.
    8. }
    To copy to clipboard, switch view to plain text mode 

    I have googeld around for a while and found this.
    Qt Code:
    1. QWebSettings::globalSettings()->setAttribute(..);
    To copy to clipboard, switch view to plain text mode 
    But have not found a setting like
    Qt Code:
    1. ::evaluateJavaScript || ::enableScript
    To copy to clipboard, switch view to plain text mode 
    There is a function in
    Qt Code:
    1. QWebView::evaluateJavaScript()
    To copy to clipboard, switch view to plain text mode 
    But from what i could find of information its not what i need (I may be wrong ofcorse..)

    Any help and or guidance is much appreciated.

    Cheers
    WetCode

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    Quote Originally Posted by WetCode View Post
    But from what i could find of information its not what i need (I may be wrong ofcorse..)
    Get the page using QWebPage. It should run the script and you can query the page for results you need.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    Thank you for your reply much appreciated, i will try this when i get home from work.
    But when you say query the page what do you mean exactly?
    Since in my mind i query the page when i enter the URL for the web page.
    Or are you refering to the parsing of the page after its been "downloaded" to the QWebView?

    Cheers
    WetCode

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    Quote Originally Posted by WetCode View Post
    But when you say query the page what do you mean exactly?
    I mean do with the page whatever you wanted to do with it in the first place ("parse the page" as you said in the opening post).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    Ok thank you for clarifying .

  6. #6
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: How to run JavaScript with the contencs of a QNetworkReply

    So I have try the QWebView and got the script running with almost no effort thank you Qt.
    But I have failed to get the QWebVeiw content in a QByteArray or QString, is there no readAll() for QWebView or QWebPage?

    Thanks for any help.

    Cheers
    WetCode

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    A regular approach would be to use QWebElement API to parse page content.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: How to run JavaScript with the contencs of a QNetworkReply

    I have read the docs on QWebView , QWebPage , QWebFrame, QWebElement sevrel times now and i stile cant figur this out.
    Take the QWebView that have finished and have all the data in the page \ Widget. (Widget is not needed btw its suposed to do this in the background).
    So some JavaScript have been executed and all data is visable, but messed around with the classes i mentiond and cant get it into a QString.

    I dont like to do this but, whould anyone be so kind to give me an example on how take all the text even the text that got generated by the JavaScript.
    And place the data in a QString \ QWebView to QWebElement to QString if that is the more correct way?
    Am so confiused right now and my Qt books are making me even more confiused.

    Aprichiate all the help.

    Cheers
    WetCode

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    Why do you want to take all the text from the page? It is much easier to query the page's DOM for whatever data you require.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    Its not that its essential to get the entire page.
    Am just having trouble with how to go about getting to the data.
    When using the classes and reading the docs I seem to be missing functions.
    This leads me to think am not doing it correctly and I may be asking the "wrong" questions.

    Cheers
    WetCode
    Last edited by WetCode; 11th October 2013 at 08:52.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    You should start with QWebFrame::findFirstElement().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: How to run JavaScript with the contencs of a QNetworkReply

    Ok in the Documentation it says that I can use the classes with out the QWebView. (since I don't need to display the entire page.)
    But I can't find ::setURL() or ::load(QUrl) on QWebPage or QWebFrame can't get it to work.
    So when you say start with QWebFrame::findFirstElement(). Am guessing this is after I have loaded the page URL.

    Since from what I managed to understand is that QWebPage is the entire page and the QWebFrame is the content on that page while QWebElement is used to search the content?

    EDIT: I found this. http://www.qtcentre.org/threads/5383...parent-classes
    Qt Code:
    1. void MainWindow::replyFinished(QNetworkReply *reply)
    2. {
    3. QWebPage page;
    4. QWebFrame *frame = page.mainFrame();
    5.  
    6. QByteArray html = reply->peek(reply->size()); //~10kB
    7. frame->setContent(html);
    8. QWebElement htmlElement = frame->documentElement();
    9. ...
    To copy to clipboard, switch view to plain text mode 
    I Think i know what to do now.
    Last edited by WetCode; 11th October 2013 at 09:34.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: How to run JavaScript with the contencs of a QNetworkReply

    Quote Originally Posted by wysota View Post
    Thanks for you patience wysota much appreciated.
    I have been having some issues where the functions don't show even though I have the correct headers. ( Think)
    Anyways I finally got the functions to show as they should think it was that I had forgot to run QMake.

    But now my application crashes for some reason am I not using it correctly?
    webTest1.pro:
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2013-10-11T10:09:41
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8. QT += webkit
    9. QT += network
    10. QT += webkit webkitwidgets
    11.  
    12. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    13.  
    14. TARGET = webTest1
    15. TEMPLATE = app
    16.  
    17.  
    18. SOURCES += main.cpp\
    19. mainwindow.cpp
    20.  
    21. HEADERS += mainwindow.h
    22.  
    23. FORMS += mainwindow.ui
    To copy to clipboard, switch view to plain text mode 
    mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtWebKit/QWebElement>
    6. #include <QtWebKit>
    7. #include <QtWebKitWidgets>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20. QWebPage *myPage;
    21. QWebFrame *myFrame;
    22. QWebElement *myElement;
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. myPage->mainFrame()->load( QUrl("http://google.com") );
    10. }
    11.  
    12. MainWindow::~MainWindow()
    13. {
    14. delete ui;
    15. }
    To copy to clipboard, switch view to plain text mode 
    I also tryed:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. myFrame->load(QUrl("http://google.com"));
    7. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to run JavaScript with the contencs of a QNetworkReply

    Quote Originally Posted by WetCode View Post
    But now my application crashes for some reason am I not using it correctly?
    Hint: calling a method on a pointer requires that the pointer point to a valid object.

    Cheers,
    _

Similar Threads

  1. QnetworkReply Problems
    By sai_3289 in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2013, 06:46
  2. QNetworkReply: how to know when data is sent?
    By mentalmushroom in forum Qt Programming
    Replies: 17
    Last Post: 24th June 2011, 08:22
  3. Subclassing QNetworkReply
    By piotr.dobrogost in forum Qt Programming
    Replies: 6
    Last Post: 19th December 2010, 08:42
  4. Replies: 9
    Last Post: 29th November 2010, 12:12
  5. When to delete QNetworkReply?
    By QPlace in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2009, 12:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.