QtWebKit: Hijack post variables and dump request
Hi,
I have my webView connected to a linkClicked handler. It's only meant to surf local pages, and I do some special magic behind the scenes etc.
When you click links, it works fine, but when you click a form submit button, it does not. It loads the page, but not through the click handler.
What I would like to do is hijack the button click in a non-hackish way, grab out all of the POST or GET variables and pump them into a JS object on the targeted page.
Now, I know some hackish ways to accomplish this, and they would be pretty straightforward, I am just wondering, is there an easy/robust webkit way to hijack submit button clicks, yank get/post vars, and kill the network access and just manually load the content?
I have been reading the NetworkAccessManager and Reply docs, and have found one or two blog posts claiming something similar, but not exact is possible. Unfortunately, I can't figure out how to do it.
Essentially, I don't fully understand what is happening with the button click, so some insight would be totally awesome!
Many thanks,
Jason
Re: QtWebKit: Hijack post variables and dump request
I didn't get any replies so far, so I kluged it. In the spirit of sharing in case someone else is looking to get this working with minimal effort:
1. Create some public slots on a QObject you intend to add to the javascript object, or add one for only this purpose.
Code:
public slots:
...
void setPost(const QMap<QString,QVariant>& object);
QMap<QString, QVariant> getPost();
void setGet(const QMap<QString,QVariant>& object);
QMap<QString, QVariant> getGet();
...
private:
QMap<QString, QVariant> _post;
QMap<QString, QVariant> _get;
2. Define these functions along these lines:
Code:
...
void MyQObject::setPost(const QMap<QString,QVariant>& object) {
_post = object;
}
QMap<QString, QVariant> MyQObject::getPost() {
return _post;
}
void MyQObject::setGet(const QMap<QString,QVariant>& object) {
_get = object;
}
QMap<QString, QVariant> MyQObject::getGet() {
return _get;
}
...
3. Created a required js include file:
Code:
/* Code based on that written by Tobias Cohen, tobiascohen.com*/
$.fn.serializeObject = function(){
var object = {};
var sArray = this.serializeArray();
$.each(sArray , function() {
if (object[this.name]) {
if (!object[this.name].push) {
object[this.name] = [object[this.name]];
}
object[this.name].push(this.value || '');
} else {
object[this.name] = this.value || '';
}
});
return object;
};
var POST = MyQObject.getPost();
var GET = MyQObject.getGet();
function MyLibSubmit(form_selector) {
var ob = $(form_selector).serializeObject();
if ($(form_selector).attr('method').toLowerCase() == 'post') {
MyQObject.setPost(ob);
} else {
MyQObject.setGet(ob);
}
return true;
}
4. define your form thusly:
Code:
<form id="testForm" method="post" action="index.html" onsubmit="return MyLibSubmit('#testForm')" >
<input type="text" name="val" value="Some value" />
<input type="submit" name="submit" value="Send"/>
</form>
You will now have access to JS variables named POST and GET in your JS Scripts.
Cheers