Results 1 to 10 of 10

Thread: how Adding JavaScript to QWebView

  1. #1
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how Adding JavaScript to QWebView

    I want to add a script javascript for my page loaded with Webkit(WebView) for this raison i have develop a small code like this :
    Qt Code:
    1. QFile filejs ("date.html");
    2. if (!filejs.open(QIODevice::ReadOnly | QIODevice::Text))
    3. { qDebug()<< "le fichier xml valide n'existe pas " ; return;}
    4.  
    5. QTextStream outjs(&filejs);
    6. QString outputjs = outjs.readAll();
    7. filejs.close();
    8.  
    9. vr =v.page()->mainFrame()->evaluateJavaScript (outputjs);//(v is WebView type)
    To copy to clipboard, switch view to plain text mode 

    Where date.html contains a javascript code which allows me to display the date.
    here is the code of date.html code:
    Qt Code:
    1. <HTML><HEAD>
    2. <TITLE> Derni&egrave;re mise &agrave; jour</TITLE>
    3. <SCRIPT LANGUAGE="JavaScript">
    4. function Tableau(n){this.lenght=n; return this; }
    5. function DateModif() {
    6. NomMois=new Tableau(12);
    7. NomMois[1]="janvier";
    8. NomMois[2]="f&eacute;vrier";
    9. NomMois[3]="mars";
    10. NomMois[4]="avril";
    11. NomMois[5]="mai";
    12. NomMois[6]="juin";
    13. NomMois[7]="juillet";
    14. NomMois[8]="ao&ucirc;t";
    15. NomMois[9]="septembre";
    16. NomMois[10]="octobre";
    17. NomMois[11]="novembre";
    18. NomMois[12]="d&eacute;cembre";
    19.  
    20. Date=new Date(document.lastModified)
    21.  
    22. var Mois=NomMois[Date.getMonth()+1]
    23. var Annee=Date.getFullYear()
    24.  
    25. return Date.getDate()+""+Mois+""+Annee }
    26. </SCRIPT>
    27. </HEAD>
    28. <BODY>
    29. Document modifi&eacute; le
    30. <SCRIPT> document.write(DateModif()) </SCRIPT>
    31. </BODY></HTML>
    To copy to clipboard, switch view to plain text mode 


    But I saw nothing, how can I view the result in my page web,I should use "addToJavaScriptWindowObject"?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how Adding JavaScript to QWebView

    That date.html doesn't contain JavaScript source, but an HTML page.

  3. #3
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how Adding JavaScript to QWebView

    Thanks Jacek

    I changed date.html by date.js
    Qt Code:
    1. function Tableau(n){this.lenght=n; return this; }
    2. function DateModif() {
    3. NomMois=new Tableau(12);
    4. NomMois[1]="janvier";
    5. NomMois[2]="f&eacute;vrier";
    6. NomMois[3]="mars";
    7. NomMois[4]="avril";
    8. NomMois[5]="mai";
    9. NomMois[6]="juin";
    10. NomMois[7]="juillet";
    11. NomMois[8]="ao&ucirc;t";
    12. NomMois[9]="septembre";
    13. NomMois[10]="octobre";
    14. NomMois[11]="novembre";
    15. NomMois[12]="d&eacute;cembre";
    16.  
    17. Date=new Date(document.lastModified)
    18.  
    19. var Mois=NomMois[Date.getMonth()+1]
    20. var Annee=Date.getFullYear()
    21.  
    22. return Date.getDate()+""+Mois+""+Annee }
    To copy to clipboard, switch view to plain text mode 

    but how i can applicate tis date.js in my web page

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how Adding JavaScript to QWebView

    Quote Originally Posted by Houda.Qt4 View Post
    but how i can applicate tis date.js in my web page
    If you want to change the document (for example to display that date), you can use DOM.

    http://www.ibm.com/developerworks/web/library/wa-jsdom/

  5. #5
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how Adding JavaScript to QWebView

    Quote Originally Posted by jacek View Post
    If you want to change the document (for example to display that date), you can use DOM.

    http://www.ibm.com/developerworks/web/library/wa-jsdom/
    thank you for the link;

    DOM! my main goal is to build the tree dom for an html page using javascript ie implement a javascript then apply it at a loaded page.
    there is an example for exstract the links :
    Qt Code:
    1. <script type="text/javascript">
    2. <!--
    3. var anchorList = document.getElementsByTagName("a") ;
    4. for (var i = 0; i < anchorList.length ; i++)
    5. {
    6. alert(anchorList[i].href + "\n");
    7. }
    8.  
    9. //-->
    10. </script>
    To copy to clipboard, switch view to plain text mode 
    I begining by the example of a date for learning to manipulate addToJavaScriptWindowObject and
    evaluateJavaScript, my problem is how to use his two function to apply the script at the loaded page.
    I do not want to change the content, but just read the content and extract information

    thank's

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how Adding JavaScript to QWebView

    Quote Originally Posted by Houda.Qt4 View Post
    I do not want to change the content, but just read the content and extract information
    evaluateJavaScript() returns a value, so you can make your script return data encoded in some way.

  7. #7
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how Adding JavaScript to QWebView

    Also make sure JavaScript is enabled in QWebSettings.

  8. #8
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how Adding JavaScript to QWebView

    Hello every body,

    I'm using addToJavaScriptWindowObject and evaluateJavaScript to inject and evaluate the script output2 at a web page,
    the injected script "output2" used to extract the names of javascript function of a web page,
    Then I get the names of these functions and I execute them with the script "ss",
    Qt Code:
    1. void Webkit::applyscript2()
    2. {
    3. QFile file ("exemple/onclick.js");
    4. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    5. { qDebug()<< "le fichier n'existe pas " ; return;}
    6.  
    7. QTextStream out(&file);
    8. QString output2 = out.readAll();
    9. file.close();
    10.  
    11. QWebFrame* f = v.page()->mainFrame();
    12.  
    13. if (!dataScript) {
    14. dataScript = new ApplyScript();
    15. f->addToJavaScriptWindowObject("linkCssOutput", dataScript);
    16. }
    17.  
    18. QVariant vrr = f->evaluateJavaScript(output2);
    19. fct_script =dataScript->getLink();
    20. int j;
    21. for (j=0;j<fct_script.size();j++)
    22. {QString contenu_fct =fct_script[j];
    23.  
    24. QString ss=QString("var f='window.addEventListener(\"load\",'+%1+', false);';"
    25. "var ff='window.attachEvent(\"onload\",%1);';"
    26. " if (window.addEventListener) {alert (\"dans if\" ); "
    27. "eval(f);"
    28. "} else if (document.addEventListener) { "
    29. "eval(f);"
    30. "} else if (window.attachEvent) {"
    31. "eval(ff);"
    32. "}").arg(contenu_fct);
    33. QVariant vrr = f->evaluateJavaScript(ss);
    34. }
    35. }
    36. /////////// setLink et getLink //////////////
    37.  
    38. void ApplyScript::setLink(const QString & data)
    39. {
    40. qDebug()<< "data "<< data;
    41. m_data.push_back(data) ;
    42. }
    43. QVector <QString> & ApplyScript::getLink( )
    44. {
    45. return m_data;
    46. }
    To copy to clipboard, switch view to plain text mode 


    but I can't get their results of execution, can you help me?
    For example I have the javascript fonction:
    Qt Code:
    1. <script type="text/javascript">
    2. function google() {
    3. var a ="http://";
    4. var b="www.google.com" ;
    5. window.location.href = a+b ;
    6. }
    7. </script>
    To copy to clipboard, switch view to plain text mode 
    I get the function name (google()) then I executed it ,but how can I get result of his execution "value of QVariant vrr = f->evaluateJavaScript(ss);".
    in this case is the url "http://www.google.com"

    I have long search a way and I have not find
    can you help me

    thank you for any assistance,

  9. #9
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how Adding JavaScript to QWebView

    hi,

    I have make this:
    Qt Code:
    1. QVariant vrr = f->evaluateJavaScript(ss);
    2. QString reshtml = vrr.value<QString>();
    3. qDebug()<< "valeur === " << reshtml ;
    To copy to clipboard, switch view to plain text mode 

    but i have valeur === " "?

    how I can retrieve the result,
    Thank you for any assistant,

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how Adding JavaScript to QWebView

    Quote Originally Posted by Houda.Qt4 View Post
    but i have valeur === " "?

    how I can retrieve the result
    What happens if you add "return 1;" at the end of your script?

Similar Threads

  1. Adding JavaScript Obj to QWebView issues
    By bpetty in forum Newbie
    Replies: 2
    Last Post: 13th May 2008, 20:44

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.