Submitting forms

32 views
Skip to the first unread message

Chris

unread,
4 Mar 2007, 3:57:53 pm4/3/07
to greasemonkey-users
I'm wondering if it is possible to use greasemonkey to submit a form
and get the page that generates without the user being aware this is
happening. I then want to use the data generated to display useful
information on that page.

That last part I can do but I'm having trouble with the first bit. I
think I should be using GM_xmlhttpRequest but I can't seem to get it
to work. Can anyone help?

Vectorspace

unread,
4 Mar 2007, 4:15:05 pm4/3/07
to greasemon...@googlegroups.com
Using an xmlhttprequest wouldn't work to well, as the response page
would be returned as the raw source plain text instead of a rendered
document object - searching it would be quite a chore.

What I would do is create an iframe with zero dimensions on the page,
whose source is a data: url so you can define its source as a string. It
would be empty but for a form element. Then you can create new child
elements in the iframe form to match those in the page's form, and then
submit the iframe form. The response page would load in the iframe, and
you can search that.

Csaba Gabor

unread,
4 Mar 2007, 6:55:30 pm4/3/07
to greasemon...@googlegroups.com
This is such an obvious thing to want to do, and it seems to me is completely underrepresented
in terms of standard solutions (I hope to hear that I'm wrong).

The IFrame route is standard:
Step 1. Direct the form response to a (hidden or zero size) IFrame. This is easy. Create the
iframe if it's not there, name it mytargetiframe and set the form's target to myfavoriteiframe.

Disclaimer. This is all from memory of when I wrote an app to put a hint button on my
websudoku.com page. The page that did the solution lived elsewhere, and I had to fill out the
remote page's sudoku form then submit it to get a hint, and then parse the hint page, then
return the extract to the main page. I have posted this on the GM mailing list before it moved
to this most recent version. See:
http://www.nabble.com/Just-say-no-to-page-scraping-and-GM_xmlhttpRequest-t1095066.html

Step 2. Muck with the response. Use GM, natch. However, consider that you are going to want
to send info back to the caller page. The standard scenario, therefore, is to use the same GM
script since you're going to want to use GM_setValue, etc. Your GM script will be responsible
for detecting whether it's in the main page or the "sub page". In fact the sub page could chain
in this fashion. The point is that pages loaded in iframes will still kick off GM scripts.

Step 3. Now this is the problem child. We want to get data back to the calling page. The
problem is WHEN. The proper way to do this is on an event driven basis. When the called page
in the iframe is finished, it should then kick off a function in the calling page. I have not
found any clean way of doing this, even when I rewrote part of the GM source code.

The point is that the two pages can't get at each other because of cross domain issues. Trying
to set an event, even in GM (at least when I tried) resulted in me (the user) being asked for
confirmation EACH TIME. Of course we can always poll, as in my example, but I hate that
approach.

Consideration: This technique is all well and good, but what happens when your page might be
called multiple times? Simultaneously. The approach above is definitely not industrial
strength as is. You must ensure that your called page matches 1 to 1 with the calling page. So
that no page usurps what isn't meant for it. Not to mention listening/polling for the right
response.


Csaba Gabor from Vienna



____________________________________________________________________________________
Be a PS3 game guru.
Get your game face on with the latest PS3 news and previews at Yahoo! Games.
http://videogames.yahoo.com/platform?platform=120121

Reply all
Reply to author
Forward
0 new messages