Results 1 to 2 of 2

Thread: QtWebKit: Hijack post variables and dump request

  1. #1
    Join Date
    Mar 2010
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Question 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

  2. #2
    Join Date
    Mar 2010
    Posts
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.
    Qt Code:
    1. public slots:
    2. ...
    3. void setPost(const QMap<QString,QVariant>& object);
    4. QMap<QString, QVariant> getPost();
    5. void setGet(const QMap<QString,QVariant>& object);
    6. QMap<QString, QVariant> getGet();
    7. ...
    8.  
    9. private:
    10. QMap<QString, QVariant> _post;
    11. QMap<QString, QVariant> _get;
    To copy to clipboard, switch view to plain text mode 

    2. Define these functions along these lines:

    Qt Code:
    1. ...
    2. void MyQObject::setPost(const QMap<QString,QVariant>& object) {
    3. _post = object;
    4. }
    5. QMap<QString, QVariant> MyQObject::getPost() {
    6. return _post;
    7. }
    8. void MyQObject::setGet(const QMap<QString,QVariant>& object) {
    9. _get = object;
    10. }
    11. QMap<QString, QVariant> MyQObject::getGet() {
    12. return _get;
    13. }
    14. ...
    To copy to clipboard, switch view to plain text mode 

    3. Created a required js include file:

    Qt Code:
    1. /* Code based on that written by Tobias Cohen, tobiascohen.com*/
    2. $.fn.serializeObject = function(){
    3. var object = {};
    4. var sArray = this.serializeArray();
    5. $.each(sArray , function() {
    6. if (object[this.name]) {
    7. if (!object[this.name].push) {
    8. object[this.name] = [object[this.name]];
    9. }
    10. object[this.name].push(this.value || '');
    11. } else {
    12. object[this.name] = this.value || '';
    13. }
    14. });
    15. return object;
    16. };
    17. var POST = MyQObject.getPost();
    18. var GET = MyQObject.getGet();
    19. function MyLibSubmit(form_selector) {
    20. var ob = $(form_selector).serializeObject();
    21. if ($(form_selector).attr('method').toLowerCase() == 'post') {
    22. MyQObject.setPost(ob);
    23. } else {
    24. MyQObject.setGet(ob);
    25. }
    26. return true;
    27. }
    To copy to clipboard, switch view to plain text mode 

    4. define your form thusly:

    Qt Code:
    1. <form id="testForm" method="post" action="index.html" onsubmit="return MyLibSubmit('#testForm')" >
    2. <input type="text" name="val" value="Some value" />
    3. <input type="submit" name="submit" value="Send"/>
    4. </form>
    To copy to clipboard, switch view to plain text mode 

    You will now have access to JS variables named POST and GET in your JS Scripts.

    Cheers

Similar Threads

  1. Replies: 0
    Last Post: 13th March 2010, 22:00
  2. How I get POST variables?
    By emental86 in forum Qt Programming
    Replies: 3
    Last Post: 27th March 2009, 18:19
  3. POST request to a web service
    By QPlace in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 09:05
  4. Request for comment: Post event filter
    By wysota in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2008, 22:55
  5. Https POST Request
    By munna in forum Qt Programming
    Replies: 10
    Last Post: 11th November 2006, 15:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.