Fred
23rd January 2016, 15:42
Hello!
I have a problem with an injected JavaScript and I don't know why it doesn't work as expected. I use a QWebView and after loading is finished of the current website, I want to inject a JavaScript.
Here a shorted version of the script:
QString injectedScript =
"alert('First alert');"
"function JSGetElement () {"
"alert('Clicked');"
"}"
"function JSOnMouseMove (event) {"
// Making something
"}"
"var body = document.body;"
"body.onclick = function () { JSGetElement(); };"
"body.onmousemove = function () { JSOnMouseMove (event); };"
;
QString one =
"var headID = document.head;"
"var newScript = document.createElement('script');"
"newScript.type = 'text/javascript';"
"newScript.innerHTML = " + injectedScript + ";"
"headID.appendChild(newScript);"
;
ui->webView->page()->mainFrame()->evaluateJavaScript(one);
The script works if an alert box appears when you click on the website.
But if you remove the first line ("alert('First alert');") the script doesn't work anymore and no alert box appears when you click on the website. Can anybody say me why this happen or how I must change the script that it works without the first line?
Thank you.
I have a problem with an injected JavaScript and I don't know why it doesn't work as expected. I use a QWebView and after loading is finished of the current website, I want to inject a JavaScript.
Here a shorted version of the script:
QString injectedScript =
"alert('First alert');"
"function JSGetElement () {"
"alert('Clicked');"
"}"
"function JSOnMouseMove (event) {"
// Making something
"}"
"var body = document.body;"
"body.onclick = function () { JSGetElement(); };"
"body.onmousemove = function () { JSOnMouseMove (event); };"
;
QString one =
"var headID = document.head;"
"var newScript = document.createElement('script');"
"newScript.type = 'text/javascript';"
"newScript.innerHTML = " + injectedScript + ";"
"headID.appendChild(newScript);"
;
ui->webView->page()->mainFrame()->evaluateJavaScript(one);
The script works if an alert box appears when you click on the website.
But if you remove the first line ("alert('First alert');") the script doesn't work anymore and no alert box appears when you click on the website. Can anybody say me why this happen or how I must change the script that it works without the first line?
Thank you.