Results 1 to 4 of 4

Thread: QWebView: apply an external javascript when a page loads

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QWebView: apply an external javascript when a page loads

    Hi,
    can anybody help me to solve this problem? I would like to apply a javascript on a web page while is loading so that the final webpage is a modified version of the remote one. The script to apply is:


    Qt Code:
    1. (function () {
    2. dl=document.links;
    3. for(i=0;i<dl.length;++i){
    4. if(dl[i].href.substr(-4)==".pdf"){
    5. dl[i].href="http://docs.google.com/viewer?url="+dl[i].href;
    6. }
    7. }
    8. })();
    To copy to clipboard, switch view to plain text mode 

    I have seen that QWebFrame has evaluateJavaScript but I don't know how to use it (if it's the right way!)

    Any tips?

    Best regards
    Giuseppe CalÃ

  2. #2
    Join Date
    Feb 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QWebView: apply an external javascript when a page loads

    The easiest way to do it is just

    qwebview_object->page()->mainFrame()->evaluateJavaScript("your_javascript_program_here_ in_a_single_line");

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWebView: apply an external javascript when a page loads

    Have tryed the following code:

    Qt Code:
    1. void MainWindow::applyScript()
    2. {
    3. QString javascript = "(function () { dl=document.links; for(i=0;i<dl.length;++i){ if(dl[i].href.substr(-4)==\".pdf\"){ dl[i].href=\"http://docs.google.com/viewer?url=\"+dl[i].href; } } })();";
    4. qDebug() << ui->webView->page()->currentFrame()->evaluateJavaScript(javascript).toString();
    5. }
    To copy to clipboard, switch view to plain text mode 

    but I got an empty string.
    Giuseppe CalÃ

  4. #4
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWebView: apply an external javascript when a page loads

    Solved! the correct javascript to insert is:

    Qt Code:
    1. QString javascript = "dl=document.links; for(i=0;i<dl.length;++i){ if(dl[i].href.substr(-4)==\".pdf\"){ dl[i].href=\"http://docs.google.com/viewer?url=\"+dl[i].href; } }";
    To copy to clipboard, switch view to plain text mode 
    Giuseppe CalÃ

Similar Threads

  1. Speed up QWebview javascript
    By Diegol in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 23rd March 2012, 11:10
  2. Debugging JavaScript from QWebView
    By thiagoalencar22 in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2009, 11:12
  3. External Javascript file not loading initially
    By bkudrle in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2009, 17:05
  4. how Adding JavaScript to QWebView
    By Houda.Qt4 in forum Qt Programming
    Replies: 9
    Last Post: 12th July 2008, 22:44
  5. Replies: 1
    Last Post: 27th November 2007, 10:10

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.