PDA

View Full Version : QWebView: apply an external javascript when a page loads



jiveaxe
24th February 2010, 16:19
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:



(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;
}
}
})();

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

roland8454
24th February 2010, 16:38
The easiest way to do it is just

qwebview_object->page()->mainFrame()->evaluateJavaScript("your_javascript_program_here_in_a_single_line");

jiveaxe
24th February 2010, 17:35
Have tryed the following code:


void MainWindow::applyScript()
{
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; } } })();";
qDebug() << ui->webView->page()->currentFrame()->evaluateJavaScript(javascript).toString();
}

but I got an empty string.

jiveaxe
25th February 2010, 08:53
Solved! the correct javascript to insert is:


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; } }";