Results 1 to 5 of 5

Thread: QWebView vs. JavaScript controls

  1. #1
    Join Date
    Feb 2015
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Question QWebView vs. JavaScript controls

    Hello!
    I've got a desktop application. The QWebView is a part of it, and it's necessary to automate activity on the page, that contains JavaScript functions. These functions create dialogs and message boxes. I'd like to force my app fill in data on these fields and push message boxes' buttons.
    Could you please give an advice, how to manage this task?

  2. #2
    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: QWebView vs. JavaScript controls

    Retrieve the view's web page, then the main frame of the page.

    QWebFrame allows you to evaluate JavaScript in the context of the web content.
    http://doc.qt.io/qt-5/qwebframe.html#evaluateJavaScript

    Cheers,
    _

  3. #3
    Join Date
    Feb 2015
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: QWebView vs. JavaScript controls

    Quote Originally Posted by anda_skoa View Post
    Retrieve the view's web page, then the main frame of the page.

    QWebFrame allows you to evaluate JavaScript in the context of the web content.
    http://doc.qt.io/qt-5/qwebframe.html#evaluateJavaScript

    Cheers,
    _
    Thanks a lot!
    Is it possible to close window.alert(), that was executed by another function, to fill window.prompt() with the application's text. Worst of all I have no idea how to find out are these modal controls active or not.

  4. #4
    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: QWebView vs. JavaScript controls

    You'll have to ask on a web programming forum.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2015
    Posts
    3
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: QWebView vs. JavaScript controls

    Quote Originally Posted by anda_skoa View Post
    You'll have to ask on a web programming forum.

    Cheers,
    _
    Thanks.
    Actually, I found the Qt solution. These QWebView JS windows effects the same way as QMessageBox alerts - they're modal and blocks the whole GUI. The solution is not complicated.
    QWebView->page() returns a QWebPage class. If you want to suspend these windows, you have to create derived class (MyWebPage for example) and redefine virtual functions.
    Qt Code:
    1. class MyWebPage : public QWebPage
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. QWebView *wv;
    7.  
    8. MyWebPage(QWebView *wvptr, QStatusBar *sbptr) : QWebPage(0)
    9. {
    10. sb = sbptr;
    11. wv = wvptr;
    12. }
    13. ~MyWebPage()
    14. {
    15. sb = NULL;
    16. wv = NULL;
    17. }
    18.  
    19. virtual void javaScriptAlert(QWebFrame *frame, const QString &msg)
    20. {
    21. if(wv->url().toString().contains("http://site.you.need"))
    22. sb->showMessage(tr("SITE.YOU.NEED sent an alert: '%1', it was supressed.").arg(msg), 5000);
    23. else
    24. QWebPage::javaScriptAlert(frame, msg);
    25. }
    26.  
    27. virtual bool javaScriptConfirm(QWebFrame *frame, const QString &msg)
    28. {
    29. if(wv->url().toString().contains("http://site.you.need"))
    30. {
    31. sb->showMessage(tr("SITE.YOU.NEED activated javaScriptConfirm: '%1'. TRUE sended.").arg(msg), 5000);
    32. return true;
    33. }
    34. else
    35. return QWebPage::javaScriptConfirm(frame, msg);
    36. }
    37.  
    38. virtual bool javaScriptPrompt(QWebFrame * frame, const QString & msg, const QString & defaultValue, QString * result)
    39. {
    40. if(wv->url().toString().contains("http://SITE.YOU.NEED"))
    41. {
    42. sb->showMessage(tr("SITE.YOU.NEED activated javaScriptPrompt: Title '%1', default value is '%2'. Sending '%3'.").arg(msg).arg(defaultValue).arg(QGuiApplication::clipboard()->text()), 5000);
    43. result->clear();
    44. result->append(QGuiApplication::clipboard()->text()); // some text from a clipboard
    45.  
    46. return true;
    47. }
    48. else
    49. return QWebPage::javaScriptPrompt(frame, msg, defaultValue, result);
    50. }
    51. };
    52.  
    53. .... in a program ...
    54.  
    55. MyWebPage *page = new MyWebPage(ui->webView, ui->statusBar);
    56. ui->webView->setPage(MyWebPage);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Qwebview <img> javascript replacement question
    By budda in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2012, 01:00
  2. Speed up QWebview javascript
    By Diegol in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 23rd March 2012, 11:10
  3. javascript communication with QWebView
    By bittuthegr8 in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2010, 13:12
  4. Debugging JavaScript from QWebView
    By thiagoalencar22 in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2009, 11:12
  5. how Adding JavaScript to QWebView
    By Houda.Qt4 in forum Qt Programming
    Replies: 9
    Last Post: 12th July 2008, 22:44

Tags for this Thread

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.