javascript - How can I handle multiple AJAX results in a userscript? -


i'm developing greasemonkey script translate <textarea> fields in intranet app, using google translation api.

but texts way large translated 1 request. error when trying :

request entity large

anyway, found way cut texts in fragments, , send them in separate requests. gets tricky, how should replace fragments in original textareas, , @ right place.

after trying several methods without success, inserted placeholders in textarea, corresponding fragments of text have translated :

{1} {2} ... 

but in success callback of xhr, have replace placeholder translated text. thing is, xhr inside for loop, iterating on table containing fragments of original text, , when requests finish, loop long finished , don't know how put translation.

here's code :

//array text[] contains fragments of original text var translated_text = []; var l = text.length; for(var = 0; < 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(/&quot;/g,"\"").replace(/&#39;/g,"\"").replace(/&gt;/g,">");             textarea.text(textarea.text().replace("{"+i+"}",translated_text[i]));         }         else         {             alert('request failed : '+responsedetails.status+"\nerror : "+responsedetails.statustext);         }     } }); } 

ps : cannot use jquery's ajax methods, because cross domain request, new $.when functionality cannot used here (sadly)

update: newer versions of greasemonkey , tampermonkey, can pass contextdoc:

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 `i`                 if (responsedetails.status == 200) {                    var data           = $.parsejson (responsedetails.responsetext);                    translated_text[i] = data.responsedata.translatedtext.replace (/&quot;/g,"\"")                                       .replace (/&#39;/g,"\"").replace (/&gt;/g,">")                                       ;                    textarea.text (textarea.text ().replace ("{"+i+"}",translated_text[i]) );                 }                 else {                    alert (                       'request failed : '+responsedetails.status+"\nerror : "                       + responsedetails.statustext                    );                 }              } } ); 

for other/older platforms, use value of i, need wrap in javascript closure. 1 way do 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 (/&quot;/g,"\"")                                          .replace (/&#39;/g,"\"").replace (/&gt;/g,">")                                          ;                       textarea.text (textarea.text ().replace ("{"+i+"}",translated_text[i]) );                    }                    else {                       alert (                          'request failed : '+responsedetails.status+"\nerror : "                          + responsedetails.statustext                       );                    }                 }    } ); } ) (i); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -