Results 1 to 8 of 8

Thread: runJavascript result returned

  1. #1
    Join Date
    May 2017
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default runJavascript result returned

    I'm trying to get the results from my JS statements, however I don't seem to get the desired result. It just appears 2 return an empty string.

    For example I load google -> runJavascript document.body. and it prints an empty string.

    Is this intended? is there a workaround so I would get the desired result?

  2. #2
    Join Date
    Jul 2008
    Posts
    412
    Thanks
    9
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: runJavascript result returned

    Hi, could you show us your code?

    Ginsengelf

  3. #3
    Join Date
    May 2017
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: runJavascript result returned

    Yeah sure, couldn't find the edit button, if there is any. so I'll post it here instead of editing main post


    Qt Code:
    1. #include <QApplication>
    2. #include <QWebEngineView>
    3. #include <iostream>
    4. using namespace std;
    5.  
    6.  
    7. class Webkit: public QWebEngineView{
    8. public:
    9. Webkit() {
    10. connect(this->page(), &QWebEnginePage::loadFinished, this, &Webkit::onFinishedLoading);
    11. }
    12. public slots:
    13. void onFinishedLoading(bool ok);
    14. };
    15.  
    16.  
    17. void Webkit::onFinishedLoading(bool ok){
    18. this->page()->runJavaScript("document.body;", [](const QVariant &v) { cout << "JS Result:" << v.toString().toStdString() << endl; });
    19. }
    20.  
    21. int main(int argc, char *argv[])
    22. {
    23. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    24. QApplication app(argc, argv);
    25.  
    26. Webkit kit;
    27. kit.show();
    28. kit.setUrl(QUrl("http://google.com"));
    29.  
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

    One would expect the following code to have some output on the JS result, however it's empty. (Qt 5.8)

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: runJavascript result returned

    runJavaScript is to run your own javascript i think..
    not script from other ...

    i load Qt5::Script to test but i see javascript alert().

    add_executable(${PROJECT_NAME} "main.cpp")

    target_link_libraries(${PROJECT_NAME} Qt5::Core)
    target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
    target_link_libraries(${PROJECT_NAME} Qt5::WebEngineWidgets)
    target_link_libraries(${PROJECT_NAME} Qt5::Script)

    Qt Code:
    1. #include <QApplication>
    2. #include <QWebEngineView>
    3. #include <iostream>
    4.  
    5. using namespace std;
    6.  
    7.  
    8. class Webkit: public QWebEngineView{
    9. public:
    10. Webkit() {
    11. connect(this->page(), &QWebEnginePage::loadFinished, this, &Webkit::onFinishedLoading);
    12. }
    13. public slots:
    14. void onFinishedLoading(bool ok);
    15. };
    16.  
    17.  
    18. void Webkit::onFinishedLoading(bool ok){
    19. this->page()->runJavaScript("document.body;alert(\"I am an alert box!\")", [](const QVariant &v) { cout << "JS Result:" << v.toString().toStdString() << endl; });
    20. }
    21.  
    22. int main(int argc, char *argv[])
    23. {
    24. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    25. QApplication app(argc, argv);
    26.  
    27. Webkit kit;
    28. kit.show();
    29. kit.setUrl(QUrl("https://wiki.selfhtml.org/wiki/JavaScript/Objekte/Date/getFullYear"));
    30.  
    31. return app.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2017
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: runJavascript result returned

    If it was to run my own javascripts, that'd be fine. but what would be the purpose of a result lambda option then?

    Could this be a bug or is it intended?

  6. #6
    Join Date
    Jul 2008
    Posts
    412
    Thanks
    9
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: runJavascript result returned

    Quote Originally Posted by Kasea View Post
    If it was to run my own javascripts, that'd be fine. but what would be the purpose of a result lambda option then?
    I have used this to extract info like the used font or parts of the HTML of a file.

    Ginsengelf

  7. #7
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: runJavascript result returned

    Yes or to take resultCallback ... from javascript like a pointer..
    On this way i think we can use google api to translate o other external game..

  8. #8
    Join Date
    May 2017
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: runJavascript result returned

    I see.. Well I'll find a workaround for my problem then, thanks.

Similar Threads

  1. Im not getting the expected result
    By sauerplayer in forum General Programming
    Replies: 5
    Last Post: 24th November 2016, 10:30
  2. runJavaScript() crashes my program.
    By ayanda83 in forum Qt Programming
    Replies: 5
    Last Post: 21st November 2016, 15:45
  3. Getting image directly with runJavaScript
    By Fred in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2016, 16:37
  4. QRegExp - get only last result
    By kabanek in forum Newbie
    Replies: 2
    Last Post: 3rd November 2010, 23:17
  5. How to display result
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 09:39

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.