waiting on GM_xmlhttprequest to finish

Discussion in 'Javascript' started by matej, Jun 25, 2008.

  1. matej

    matej Guest

    (sorry, this is probably slightly offtopic here and would be more
    appropriate in greasemonkey-users Google Group, but I haven't got any
    reply there when posted; I guess, it would work exactly the same with
    plain xmlhttprequest, not only the GM_* one; actually, thinking about
    it, I can use plain xmlhttprequest, because this is from the same
    domain as the page)

    Hi,

    I have a script which works on http://bugzilla.redhat.com (it works
    probably just for logged in users, who are employees of Red Hat --
    normal users have slightly different webpages) and uses XMLRPC (via
    http://webdev.yuan.cc/lib/xmlrpc.js and @require) to change MIME types
    of some/all attachments to a bug. Whole script is also available on
    http://mcepl.fedorapeople.org/scripts/fixAttachmentTypes.user.js

    The problem I have is that after all GM_xmlhttpRequests I would like
    to fire document.location.reload() Obviously GM_xmlhttpRequest is
    asynchronous, so I tried this to cout how many outstanding requests
    are unfinished and to fire the reload() only when it is zero:

    /* global variable in the top of the script */
    var reqCounter = 0;

    /* ... skip ... */

    /**
    * Sends XMLRPC request
    *
    * @param url string with URL of the XML-RPC interface
    * @param data string with XML of the data to be sent
    * @param method string -- either 'post' or 'get'
    * @param callback function catching callback
    */
    function sendRequest(url,data,method,callback) {
    GM_xmlhttpRequest({
    method: method,
    url: url,
    headers: {
    'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey
    fixAttType
    XMLRPC',
    'Accept': 'application/atom+xml,application/xml,text/xml',
    'Content-type': 'text/xml',
    },
    data: data,
    onload: callback,
    });

    }

    /**
    * Callback function for the XMLRPC request
    *
    * @param ret object with xmlhttprequest response
    * with attributes:
    * + status -- int return code
    * + statusText
    * + responseHeaders
    * + responseText
    */
    callBack = function(ret) {
    if (ret.status != 200) {
    alert([ret.status,ret.statusText,ret.responseHeaders,
    ret.responseText]);
    }
    if (--reqCounter == 0) {
    document.location.reload();
    }

    }

    /**
    * The worker function -- call XMLRPC to fix MIME type of the
    * particular attachment
    *
    * @param id integer with the attachment id to be fixed
    * @param type string with the new MIME type, e.g. "text/plain"
    *
    */
    fixAttachById = function(id,type) {
    var msg = new XMLRPCMessage("bugzilla.updateAttachMimeType");
    msg.addParameter({'attach_id':id, 'mime_type':type});
    msg.addParameter(login);
    msg.addParameter(password);
    try {
    var ret = sendRequest(XMLRPCurl,
    msg.xml(),'post',callBack);
    }
    catch (e) {
    window.alert(e);
    }
    reqCounter++;

    }

    I hoped that document.location.reload() now happens only when XMLRPC
    requests are complete. Unfortunately, I still get webpage as if some
    attachment types were not changed. Do I do something wrong, or do I
    have to just add waiting for a second or two (presumably to wait on
    bugzilla to catch up on the changes in the internal database)?

    Any more thoughts?

    Thanks for any reply,

    Matej Cepl
    matej, Jun 25, 2008
    #1
    1. Advertising

  2. * matej wrote in comp.lang.javascript:
    >/* global variable in the top of the script */
    >var reqCounter = 0;


    > if (--reqCounter == 0) {
    > document.location.reload();
    > }


    > try {
    > var ret = sendRequest(XMLRPCurl,
    > msg.xml(),'post',callBack);
    > }
    > catch (e) {
    > window.alert(e);
    > }
    > reqCounter++;


    It is not clear from these excerpts that you ensure the counter does not
    reach zero because the requests finish faster than you can schedule them
    (consider you send one request, it finishes, and then you never send the
    second request). Also, if sendRequest fails you probably don't want to
    increase the counter, though I don't know what sendRequest does.

    >I hoped that document.location.reload() now happens only when XMLRPC
    >requests are complete. Unfortunately, I still get webpage as if some
    >attachment types were not changed. Do I do something wrong, or do I
    >have to just add waiting for a second or two (presumably to wait on
    >bugzilla to catch up on the changes in the internal database)?


    Generally such problems should be debugged by looking at the actual net-
    work traffic using a network analyzer like WireShark or at least some
    browser add-on that gives you access to what's going on there. That way
    you can rule out many problems.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
    Bjoern Hoehrmann, Jun 25, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    14,108
    Matt Humphrey
    Apr 22, 2005
  2. Behrang Dadsetan

    Waiting for processes to finish under Solaris

    Behrang Dadsetan, Jul 15, 2003, in forum: Python
    Replies:
    1
    Views:
    523
    Behrang Dadsetan
    Jul 15, 2003
  3. Martin DeMello
    Replies:
    1
    Views:
    180
    Farrel Lifson
    Nov 13, 2006
  4. Brett

    Waiting for a site to finish loading

    Brett, May 28, 2005, in forum: Javascript
    Replies:
    3
    Views:
    109
    Randy Webb
    May 28, 2005
  5. Roedy Green
    Replies:
    31
    Views:
    585
    Robert Klemme
    Jan 26, 2013
Loading...

Share This Page