External Javascript in QWebView
-
Hello,
I am trying to implement screen scraping on some webpages (as the webview kept on throwing panic(bugs have been already reported)). Using Qt 4.6.3 for Symbian.
I get the form element out of the webview using
@ QWebView *webView = new QWebView();
webView->setHtml(htmlStr);
QWebElementCollection formElements = webView->page()->mainFrame()->findAllElements("form");QWebElement element; for(int i = 0; i < formElements.count(); i++) { element = formElements.at(i); QString attName = element.attribute("name", "-999"); if(attName.compare("login") == 0) { break; } }@
After that i load the form element on to a new webiew and evaluate javascript
@QWebView *ftwWebView = new QWebView();
ftwWebView->setHtml(element.toOuterXml());
QWebFrame *ftwFrame = ftwWebView->page()->mainFrame();
result = ftwFrame->evaluateJavaScript("document.getElementById('username').value = 'someusername';");
result = ftwFrame->evaluateJavaScript("document.getElementById('password').value = 'somepassword';");
result = ftwFrame->evaluateJavaScript("function include(destination) { var e=window.document.createElement('script'); e.setAttribute('src',destination); window.document.body.appendChild(e); } include('http://somelink.com/somefile.js');");
result = ftwFrame->evaluateJavaScript("validate_form( document.forms[0], true );");
@The validate_form function in located in somefile.js file. If i repeat the same steps in browser it works perfectly but QWebView is failing to run the function.
My question is will the webview load functions from external .js files if i load the webview using setHtml instead of loading it from an URL.
Thanks,
Sri
-
Hello,
Thanks for the reply. I am already doing that
@function include(destination)
{
var e=window.document.createElement('script');
e.setAttribute('src',destination);
window.document.body.appendChild(e);
}include('http://somelink.com/somefile.js');@
The function is in the js file. In the src attribute the url path is also correct but none of the functions get called. I think it is not auto downloading. Is there a way i can initiate it?
Thanks,
Sri
-
Hi,
I tried it with no luck
@e.setAttribute('type', "text/javascript");@
I also included this
@ftwWebView->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);@
Again with no luck. Can't QWebView load js functions from the link? Or is it because i am setting setHTML?
Thanks,
Sri
-
Hello,
This is what i wanted to do:
- Get a form element out of web-view.
- Load the contents of the form i.e., username and password.
- Encrypt the "input" tags wherever necessary using the js functions.
- Submit the form.
- Get the cookies of that session.
Initially i tried to open the webview ask the user to enter all the details and then i used to close the webview after taking the cookies. But the webview for some sites used to throw panic on symbian devices. So i took up screen scraping method.
Now to run the JS i could have used QScriptEngine but there were more than 6-7 interlinked JS files.
Thanks for the help and this is the best alternate way i found. But i will continue to search as you mentioned this might not be a correct way.
Thanks,
Sri
-
I load JS files that contain functions into webview frequently using the following method:
Connect the javaScriptWindowObjectCleared signal from QWebFrame to a local jsCleared function. Note have to use this signal or the js loses context.
In jsCleared read in the js file that contains your functions. I save my functions in a resource file but you can load in any file from any source (including libraries like jQuery).
Once the js file has been read in then just use the evaluateJavaScript method to load the entire js file. In my case when I read the resource js text file it is read as a QByteArray and I have to convert it to a QString before using evaluateJavaScript.
Once the jsCleared method has loaded the js file then you can use any function defined in the file using evaluateJavaScript.
This is the simplest method I found for loading js libraries (either internal or external) that I don't want to be a permanent part of the HTML file.
Note: I use this method with Qt 4.8.1 on a desktop.
Finally I noticed that in your original post you set the result variable to the return value of the evaluateJavaScript method but result is a QVariant object and you have to convert it to the correct data type in order to use it.