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:
Code:
(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
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");
Re: QWebView: apply an external javascript when a page loads
Have tryed the following code:
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.
Re: QWebView: apply an external javascript when a page loads
Solved! the correct javascript to insert is:
Code:
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; } }";