Greasemonkey 与需要 windows-1250 编码的服务器通信
我正在开发一个 greasemonkey 插件,该插件应该在不受我控制的应用程序上使用 POST (GM_xmlhttpRequest) 在后台发送表单。该应用程序是用 PHP 编写的,并且似乎期望其所有输入都采用 windows-1250 编码。我需要做的就是按原样保留所有表单字段,仅编辑其中之一并重新提交。某些字段使用重音字符并且长度有限。
理论上这不是问题 - 我迭代所有表单字段,对值使用encodeURIComponent 函数并将所有内容连接到发布请求正文。然而。 encodeURIComponent 函数始终根据 UTF-8 对字符进行编码,这会导致各种问题。因为 PHP 似乎没有正确地重新编码我对 windows-1250 的请求,所以它会误解多字节字符串,并得出结论:重新提交的值比允许的 40 个字符长,所以我就死掉了。或者脚本只是默默地死掉,没有给我任何有用的反馈。
我通过查看当我在浏览器窗口中提交表单时 Firefox 发送的 POST 正文,然后使用 xhr 将相同的数据重新发送到服务器来测试这一点。这有效。例如,字符串:
Zajišťujeme profesionální modelky
当由encodeURIComponent编码时,如下所示:
Zaji%C5%A1%C5%A5ujeme%20profesion%C3%A1ln%C3%AD%20modelky
在 PHP(windows-1250 中的源文本)或 Firefox 中使用 urlencode 进行同样的操作:
Zaji%9A%9Dujeme+profesion%E1ln%ED+modelky
显然,我需要对帖子正文进行编码,就好像它在 windows-1250 中或以某种方式使服务器接受 utf-8 (我怀疑这是可能的)。我尝试了各种其他函数,例如 escape 或encodeURI,但输出没有太大不同 - 似乎都以 utf-8 输出。
有什么办法可以解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让 Firefox 对 URL 进行编码的另一种方法是将其设置为链接的 href。该属性(非属性)将始终作为页面编码中 urlencoded 的绝对链接读回。
对于 GET 请求,您只需将 href 设置为
http://server/cgi?var=value
并读回编码形式。对于 POST 请求,您必须采取额外的步骤来分离数据(您不能单独使用?var=value
,因为链接会作为绝对链接读回)。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).让浏览器对表单进行编码。将其放入隐藏的 iframe 中并对其调用submit()。
Let the browser encode the form. Put it in a hidden iframe and call submit() on it.