[Greasemonkey] Arrgghh!!! it runs asynch!!! (was: Looking for a hotshot GM coder...)

Tom Emerson osnut at pacbell.net
Wed Jun 28 02:06:35 EDT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

chris feldmann wrote:
> Damn, got a spare slash in there somehow. Take 2:
> imgUri = refText.match(/<img\ssrc="(
> http://photos.eharmony.com/photos/servlet[^<http://photos.eharmony.com/photos/servlet%5B%5E>"]+).+>/g);

As it turns out, "photos.eharmony.com" is unique on the page, so the
match can be optimized to this:

    var imgURI = refText.match(/(http:\/\/photos.eharmony.com[^"]+)/g);

(i.e., start with http and continue on until the terminating double quote)

HOWEVER -- that's the least of my problems.

the log output looks essentially like this:

- ----- starting eharmony icon replacement
found icon to replace (id=123456789)
found icon to replace (id=987654321)
found icon to replace (id=456123789)
- ----- done replacing eharmony icons
- ---> successfully retrieved member details
     new URI is photos.eharmony.com/servlet?s=blah-blah-blah
- ---> successfully retrieved member details
     new URI is photos.eharmony.com/servlet?s=blah-blah-blah
- ---> successfully retrieved member details
     new URI is photos.eharmony.com/servlet?s=blah-blah-blah

In short, it ain't synchronous!

by the the first "GM_xmlhttprequest" completes, the script is over and
done with the entire page -- only the last image gets updated!

Now that I'm down to that last niggling detail, here is the script-thus-far:

//---------------------------------
// ==UserScript==
// @name          eHarmony_Showpics
// @description   replaces "info" icon in the general listing

                  w/thumbnail pic of match from the match details page
// @include  http://www.eharmony.com/singles/servlet/user/mymatches/*
// ==/UserScript==

var allCells, thisCell;

    GM_log('-----Beginning eHarmony info-icon replacement ');

    // look for table cells containing the image "ic-info.gif"
    allCells = document.evaluate(
"//img[@src='http://static.eharmony.com:80/static/images/mymatches/ic-info.gif']/parent::*",
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);

    for (var i = 0; i < allCells.snapshotLength; i++) {
        thisCell = allCells.snapshotItem(i);
        var page = thisCell.getAttribute('page');
        GM_log('-----Found ' + page);
        GM_xmlhttpRequest({
            method : 'GET',
            url : 'http://www.eharmony.com/singles' + page,

            onload: function(responseDetails) {
                    var refText = responseDetails.responseText;
                    var imgURI =
refText.match(/(http:\/\/photos.eharmony.com[^"]+)/g);
                    GM_log('New URI: ' + imgURI);
                    if (imgURI != null)
         // only replace the icon if found -- some members don't have
         // pics or have chosen not to share them just yet
                        var oldImg = thisCell.firstChild;
                        oldImg.setAttribute('src', imgURI);
                    }
	    }
        )
    };
    GM_log('-----End of    eHarmony info-icon replacement ');
;
//---------------------------------


Any suggestions on how to make the for-loop wait until the httprequest
completes before iterating?

- --
Top o' the Blog: It's good to be the King...
http://osnut.homelinux.net/mtblog/ya_index.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEojiLV/YHUqq2SwsRArZqAJ9WRTRachUSHLv4fdneeqCn2QAB8ACeOPiz
DiFEXXNXKZEuiFlNovcuYpE=
=oIlD
-----END PGP SIGNATURE-----


More information about the Greasemonkey mailing list