views:

103

answers:

2

I'm developing a greasemonkey plugin, which is supposed to send a form in background using POST (GM_xmlhttpRequest) on an application not under my control. That application is written in PHP and seems to expect all its input in windows-1250 encoding. What I need to do is to take all the form fields as they are, edit just one of them and resubmit. Some of the fields use accented characters and are limited in length.

Not a problem in theory - I iterate over all form fields, use the encodeURIComponent function on the values and concatenate everything to a post request body. HOWEVER. The encodeURIComponent function always encodes characters according to UTF-8, which leads to all sorts of problems. Because PHP doesn't seem to recode my request to windows-1250 properly, it misinterprets multibyte strings and comes to the conclusion that the resubmitted values are longer than the allowed 40 characters and dies on me. Or the script just dies silently without giving me any sort of useful feedback.

I have tested this by looking at the POST body firefox is sending when I submit the form in a browser window and then resending the same data to the server using xhr. Which worked. For example the string:

Zajišťujeme profesionální modelky

Looks as follows, when encoded by encodeURIComponent:

Zaji%C5%A1%C5%A5ujeme%20profesion%C3%A1ln%C3%AD%20modelky

Same thing using urlencode in PHP (source text in windows-1250) or Firefox:

Zaji%9A%9Dujeme+profesion%E1ln%ED+modelky

Apparently, I need to encode the post body as if it were in windows-1250 or somehow make the server accept utf-8 (which I doubt is possible). I tried all kinds of other function like escape or encodeURI, but the output is not much different - all seem to output in utf-8.

Is there any way out of this?

A: 

Let the browser encode the form. Put it in a hidden iframe and call submit() on it.

Jeremy Stein
If I do it that way, will I be able to get notified when the submission finishes?
VoY
The page that is returned when you submit would need to notify you.
Jeremy Stein
+1  A: 

Another way to get Firefox to encode a URL is to set it as the href of a link. The property (NOT attribute) will always read back as an absolute link urlencoded in the page's encoding.

For a GET request you would simply set the href as http://server/cgi?var=value and read back the encoded form. For a POST request you would have to take the extra step to separate the data (you can't use ?var=value on its own because the link reads back as an absolute link).

Neil
Hey, that's clever!
Jeremy Stein
This is a great idea, thank you!
VoY