View Full Version : how Adding JavaScript to QWebView
Houda.Qt4
8th June 2008, 11:30
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 :
QVariant vr;
QFile filejs ("date.html");
if (!filejs.open(QIODevice::ReadOnly | QIODevice::Text))
{ qDebug()<< "le fichier xml valide n'existe pas " ; return;}
QTextStream outjs(&filejs);
QString outputjs = outjs.readAll();
filejs.close();
vr =v.page()->mainFrame()->evaluateJavaScript (outputjs);//(v is WebView type)
Where date.html contains a javascript code which allows me to display the date.
here is the code of date.html code:
<HTML><HEAD>
<TITLE> Dernière mise à jour</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function Tableau(n){this.lenght=n; return this; }
function DateModif() {
NomMois=new Tableau(12);
NomMois[1]="janvier";
NomMois[2]="février";
NomMois[3]="mars";
NomMois[4]="avril";
NomMois[5]="mai";
NomMois[6]="juin";
NomMois[7]="juillet";
NomMois[8]="août";
NomMois[9]="septembre";
NomMois[10]="octobre";
NomMois[11]="novembre";
NomMois[12]="décembre";
Date=new Date(document.lastModified)
var Mois=NomMois[Date.getMonth()+1]
var Annee=Date.getFullYear()
return Date.getDate()+""+Mois+""+Annee }
</SCRIPT>
</HEAD>
<BODY>
Document modifié le
<SCRIPT> document.write(DateModif()) </SCRIPT>
</BODY></HTML>
But I saw nothing, how can I view the result in my page web,I should use "addToJavaScriptWindowObject"?
jacek
8th June 2008, 14:18
That date.html doesn't contain JavaScript source, but an HTML page.
Houda.Qt4
8th June 2008, 15:02
Thanks Jacek :)
I changed date.html by date.js
function Tableau(n){this.lenght=n; return this; }
function DateModif() {
NomMois=new Tableau(12);
NomMois[1]="janvier";
NomMois[2]="février";
NomMois[3]="mars";
NomMois[4]="avril";
NomMois[5]="mai";
NomMois[6]="juin";
NomMois[7]="juillet";
NomMois[8]="août";
NomMois[9]="septembre";
NomMois[10]="octobre";
NomMois[11]="novembre";
NomMois[12]="décembre";
Date=new Date(document.lastModified)
var Mois=NomMois[Date.getMonth()+1]
var Annee=Date.getFullYear()
return Date.getDate()+""+Mois+""+Annee }
but how i can applicate tis date.js in my web page:confused:
jacek
8th June 2008, 16:09
but how i can applicate tis date.js in my web page:confused:
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/
Houda.Qt4
8th June 2008, 17:04
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 :D 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 :
<script type="text/javascript">
<!--
var anchorList = document.getElementsByTagName("a") ;
for (var i = 0; i < anchorList.length ; i++)
{
alert(anchorList[i].href + "\n");
}
//-->
</script>
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 :confused:
thank's
jacek
10th June 2008, 01:19
I do not want to change the content, but just read the content and extract information :confused:
evaluateJavaScript() returns a value, so you can make your script return data encoded in some way.
Brandybuck
10th June 2008, 18:54
Also make sure JavaScript is enabled in QWebSettings.
Houda.Qt4
26th June 2008, 12:13
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",
void Webkit::applyscript2()
{
QFile file ("exemple/onclick.js");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ qDebug()<< "le fichier n'existe pas " ; return;}
QTextStream out(&file);
QString output2 = out.readAll();
file.close();
QWebFrame* f = v.page()->mainFrame();
if (!dataScript) {
dataScript = new ApplyScript();
f->addToJavaScriptWindowObject("linkCssOutput", dataScript);
}
QVariant vrr = f->evaluateJavaScript(output2);
fct_script =dataScript->getLink();
int j;
for (j=0;j<fct_script.size();j++)
{QString contenu_fct =fct_script[j];
QString ss=QString("var f='window.addEventListener(\"load\",'+%1+', false);';"
"var ff='window.attachEvent(\"onload\",%1);';"
" if (window.addEventListener) {alert (\"dans if\" ); "
"eval(f);"
"} else if (document.addEventListener) { "
"eval(f);"
"} else if (window.attachEvent) {"
"eval(ff);"
"}").arg(contenu_fct);
QVariant vrr = f->evaluateJavaScript(ss);
}
}
/////////// setLink et getLink //////////////
void ApplyScript::setLink(const QString & data)
{
qDebug()<< "data "<< data;
m_data.push_back(data) ;
}
QVector <QString> & ApplyScript::getLink( )
{
return m_data;
}
but I can't get their results of execution, can you help me?
For example I have the javascript fonction:
<script type="text/javascript">
function google() {
var a ="http://";
var b="www.google.com" ;
window.location.href = a+b ;
}
</script>
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,
Houda.Qt4
26th June 2008, 13:47
hi,
I have make this:
QVariant vrr = f->evaluateJavaScript(ss);
QString reshtml = vrr.value<QString>();
qDebug()<< "valeur === " << reshtml ;
but i have valeur === " "?
how I can retrieve the result,
Thank you for any assistant,
jacek
12th July 2008, 22:44
but i have valeur === " "?
how I can retrieve the result
What happens if you add "return 1;" at the end of your script?
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.