問題描述
我目前正在開發(fā)一個(gè) Greasemonkey 腳本來翻譯 Intranet 應(yīng)用程序中的 <textarea>
字段,使用 Google Translation API.
I'm currently developing a Greasemonkey script to translate <textarea>
fields in an Intranet app, using Google Translation API.
但有些文本太大而無法僅通過一個(gè)請(qǐng)求進(jìn)行翻譯.嘗試時(shí)出現(xiàn)此錯(cuò)誤:
But some texts are way too large to be translated with only one request. I get this error when trying :
請(qǐng)求實(shí)體太大
無論如何,我找到了一種將文本分割成片段的方法,并在單獨(dú)的請(qǐng)求中發(fā)送它們.棘手的地方是,我應(yīng)該如何替換原始文本區(qū)域中的這些片段,尤其是在正確的位置.
Anyway, I found a way to cut the texts in fragments, and send them in separate requests. Where it gets tricky, is how I should replace those fragments in their original textareas, and especially at the right place.
在嘗試了幾種方法都沒有成功后,我在文本區(qū)域中插入了占位符,對(duì)應(yīng)于需要翻譯的文本片段:
After trying several methods without any success, I inserted placeholders in the textarea, corresponding to the fragments of text that have to be translated :
{1}
{2}
...
但是現(xiàn)在在我的 XHR 的成功回調(diào)中,我必須用翻譯后的文本替換占位符.問題是,我的 XHR 在 for
循環(huán)中,遍歷包含原始文本片段的表,當(dāng)請(qǐng)求完成時(shí),循環(huán)很長(zhǎng),我不知道如何獲取把翻譯放在哪里.
But now in the success callback of my XHR, I have to replace the placeholder with the translated text. The thing is, my XHR is inside a for
loop, iterating over my table containing the fragments of original text, and when the requests finish, the loop is long finished and I don't know how to get where to put the translation.
代碼如下:
//Array text[] contains the fragments of original text
var translated_text = [];
var l = text.length;
for(var i = 0; i < l; i++)
{
var fullurl = apiurl+encodeURIComponent(text[i]);
GM_xmlhttpRequest({
method: 'GET',
url: fullurl,
headers:
{
'User-agent': 'Mozilla/5.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function(responseDetails)
{
var destination = "{"+i+"}";
if(responseDetails.status == 200)
{
var data = $.parseJSON(responseDetails.responseText);
translated_text[i] = data.responseData.translatedText.replace(/"/g,""").replace(/'/g,""").replace(/>/g,">");
textarea.text(textarea.text().replace("{"+i+"}",translated_text[i]));
}
else
{
alert('Request Failed : '+responseDetails.status+"
Error : "+responseDetails.statusText);
}
}
});
}
PS : 我不能使用 jQuery 的 AJAX 方法,因?yàn)檫@是一個(gè)跨域請(qǐng)求,所以這里不能使用新的 $.when
功能(遺憾)
PS : I cannot use jQuery's AJAX methods, because this is a Cross Domain request, so the new $.when
functionality cannot be used here (sadly)
推薦答案
更新:使用較新版本的 Greasemonkey 和 Tampermonkey,您現(xiàn)在可以通過 a context
Doc:
Update: With newer versions of Greasemonkey and Tampermonkey, you can now pass a context
Doc:
GM_xmlhttpRequest ( {
method: 'GET',
url: fullurl,
context: i,
headers: {
'User-agent': 'Mozilla/5.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function (responseDetails) {
var destination = "{" + responseDetails.context + "}"; // context is `i`
if (responseDetails.status == 200) {
var data = $.parseJSON (responseDetails.responseText);
translated_text[i] = data.responseData.translatedText.replace (/"/g,""")
.replace (/'/g,""").replace (/>/g,">")
;
textarea.text (textarea.text ().replace ("{"+i+"}",translated_text[i]) );
}
else {
alert (
'Request Failed : '+responseDetails.status+"
Error : "
+ responseDetails.statusText
);
}
}
} );
<小時(shí)>
對(duì)于其他/較舊的平臺(tái),要使用 i
的值,您需要 將其包裝在 JavaScript 閉包. 一種方法是:
For other/older platforms, to use the value of i
, you need to wrap it in a JavaScript closure. One way to do do that is:
( function (i) {
GM_xmlhttpRequest ( {
method: 'GET',
url: fullurl,
headers: {
'User-agent': 'Mozilla/5.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function (responseDetails) {
var destination = "{"+i+"}";
if (responseDetails.status == 200) {
var data = $.parseJSON (responseDetails.responseText);
translated_text[i] = data.responseData.translatedText.replace (/"/g,""")
.replace (/'/g,""").replace (/>/g,">")
;
textarea.text (textarea.text ().replace ("{"+i+"}",translated_text[i]) );
}
else {
alert (
'Request Failed : '+responseDetails.status+"
Error : "
+ responseDetails.statusText
);
}
}
} );
} ) (i);
這篇關(guān)于如何在用戶腳本中處理多個(gè) AJAX 結(jié)果?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!