<tfoot id='TRWez'></tfoot>

<small id='TRWez'></small><noframes id='TRWez'>

    1. <i id='TRWez'><tr id='TRWez'><dt id='TRWez'><q id='TRWez'><span id='TRWez'><b id='TRWez'><form id='TRWez'><ins id='TRWez'></ins><ul id='TRWez'></ul><sub id='TRWez'></sub></form><legend id='TRWez'></legend><bdo id='TRWez'><pre id='TRWez'><center id='TRWez'></center></pre></bdo></b><th id='TRWez'></th></span></q></dt></tr></i><div id='TRWez'><tfoot id='TRWez'></tfoot><dl id='TRWez'><fieldset id='TRWez'></fieldset></dl></div>
    2. <legend id='TRWez'><style id='TRWez'><dir id='TRWez'><q id='TRWez'></q></dir></style></legend>
        • <bdo id='TRWez'></bdo><ul id='TRWez'></ul>

        如何在用户脚本中处理多个 AJAX 结果?

        时间:2023-10-14
          <tbody id='jKFBC'></tbody>

        <small id='jKFBC'></small><noframes id='jKFBC'>

          <bdo id='jKFBC'></bdo><ul id='jKFBC'></ul>

            <tfoot id='jKFBC'></tfoot>
              <legend id='jKFBC'><style id='jKFBC'><dir id='jKFBC'><q id='jKFBC'></q></dir></style></legend>
              • <i id='jKFBC'><tr id='jKFBC'><dt id='jKFBC'><q id='jKFBC'><span id='jKFBC'><b id='jKFBC'><form id='jKFBC'><ins id='jKFBC'></ins><ul id='jKFBC'></ul><sub id='jKFBC'></sub></form><legend id='jKFBC'></legend><bdo id='jKFBC'><pre id='jKFBC'><center id='jKFBC'></center></pre></bdo></b><th id='jKFBC'></th></span></q></dt></tr></i><div id='jKFBC'><tfoot id='jKFBC'></tfoot><dl id='jKFBC'><fieldset id='jKFBC'></fieldset></dl></div>
                • 本文介绍了如何在用户脚本中处理多个 AJAX 结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我目前正在开发一个 Greasemonkey 脚本来翻译 Intranet 应用程序中的 <textarea> 字段,使用 Google Translation API.

                  I'm currently developing a Greasemonkey script to translate <textarea> fields in an Intranet app, using Google Translation API.

                  但有些文本太大而无法仅通过一个请求进行翻译.尝试时出现此错误:

                  But some texts are way too large to be translated with only one request. I get this error when trying :

                  请求实体太大

                  无论如何,我找到了一种将文本分割成片段的方法,并在单独的请求中发送它们.棘手的地方是,我应该如何替换原始文本区域中的这些片段,尤其是在正确的位置.

                  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.

                  在尝试了几种方法都没有成功后,我在文本区域中插入了占位符,对应于需要翻译的文本片段:

                  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}
                  ...
                  

                  但是现在在我的 XHR 的成功回调中,我必须用翻译后的文本替换占位符.问题是,我的 XHR 在 for 循环中,遍历包含原始文本片段的表,当请求完成时,循环很长,我不知道如何获取把翻译放在哪里.

                  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(/&quot;/g,""").replace(/&#39;/g,""").replace(/&gt;/g,">");
                              textarea.text(textarea.text().replace("{"+i+"}",translated_text[i]));
                          }
                          else
                          {
                              alert('Request Failed : '+responseDetails.status+"
                  Error : "+responseDetails.statusText);
                          }
                      }
                  });
                  }
                  

                  PS : 我不能使用 jQuery 的 AJAX 方法,因为这是一个跨域请求,所以这里不能使用新的 $.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,您现在可以通过 a contextDoc:

                  Update: With newer versions of Greasemonkey and Tampermonkey, you can now pass a 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 is `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+"
                  Error : "
                                        + responseDetails.statusText
                                     );
                                  }
                               }
                  } );
                  

                  <小时>

                  对于其他/较旧的平台,要使用 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 (/&quot;/g,""")
                                                           .replace (/&#39;/g,""").replace (/&gt;/g,">")
                                                           ;
                                        textarea.text (textarea.text ().replace ("{"+i+"}",translated_text[i]) );
                                     }
                                     else {
                                        alert (
                                           'Request Failed : '+responseDetails.status+"
                  Error : "
                                           + responseDetails.statusText
                                        );
                                     }
                                  }
                     } );
                  } ) (i);
                  

                  这篇关于如何在用户脚本中处理多个 AJAX 结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:重新创建 jQuery 的 ajaxStart 和 ajaxComplete 功能 下一篇:XMLHttpRequest() &amp;净::ERR_NAME_NOT_RESOLVED

                  相关文章

                  最新文章

                • <small id='cwH45'></small><noframes id='cwH45'>

                • <tfoot id='cwH45'></tfoot>
                  1. <i id='cwH45'><tr id='cwH45'><dt id='cwH45'><q id='cwH45'><span id='cwH45'><b id='cwH45'><form id='cwH45'><ins id='cwH45'></ins><ul id='cwH45'></ul><sub id='cwH45'></sub></form><legend id='cwH45'></legend><bdo id='cwH45'><pre id='cwH45'><center id='cwH45'></center></pre></bdo></b><th id='cwH45'></th></span></q></dt></tr></i><div id='cwH45'><tfoot id='cwH45'></tfoot><dl id='cwH45'><fieldset id='cwH45'></fieldset></dl></div>

                    <legend id='cwH45'><style id='cwH45'><dir id='cwH45'><q id='cwH45'></q></dir></style></legend>
                        <bdo id='cwH45'></bdo><ul id='cwH45'></ul>