久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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>
      1. <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 class="qwawimqqmiuu" id='jKFBC'><tfoot id='jKFBC'></tfoot><dl id='jKFBC'><fieldset id='jKFBC'></fieldset></dl></div>
      2. 如何在用戶腳本中處理多個(gè) AJAX 結(jié)果?

        How can I handle multiple AJAX results in a userscript?(如何在用戶腳本中處理多個(gè) AJAX 結(jié)果?)
        <tfoot id='TRWez'></tfoot>

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

            • <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 class="qwawimqqmiuu" id='TRWez'><tfoot id='TRWez'></tfoot><dl id='TRWez'><fieldset id='TRWez'></fieldset></dl></div>
            • <legend id='TRWez'><style id='TRWez'><dir id='TRWez'><q id='TRWez'></q></dir></style></legend>
                • <bdo id='TRWez'></bdo><ul id='TRWez'></ul>
                    <tbody id='TRWez'></tbody>
                  本文介紹了如何在用戶腳本中處理多個(gè) AJAX 結(jié)果?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我目前正在開發(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(/&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 方法,因?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 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
                                     );
                                  }
                               }
                  } );
                  

                  <小時(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 (/&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);
                  

                  這篇關(guān)于如何在用戶腳本中處理多個(gè) AJAX 結(jié)果?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調(diào)用 abort (jQuery) 之后,瀏覽器也會(huì)等待 ajax 調(diào)用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請(qǐng)求的資源上不存在“Access-Control-Allow-Origin標(biāo)頭) - IT屋-程序員軟件開發(fā)技術(shù)分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請(qǐng)求是否有可能不遵循重定向 (301 302))
                  NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 異常 101)
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內(nèi)容)

                  • <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 class="qwawimqqmiuu" 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>

                              <tbody id='cwH45'></tbody>
                            主站蜘蛛池模板: 国产黄色在线观看 | 久久精品国产99国产精品 | 久久久久成人精品免费播放动漫 | 一区观看 | 国产一区二区三区 | 日韩在线观看视频一区 | 一区二区三区四区av | 伊人网99 | 五月天天丁香婷婷在线中 | 综合二区 | 亚洲一区二区三区四区五区午夜 | 日韩成人免费中文字幕 | 欧美性视频在线播放 | 亚洲精品一区二三区不卡 | 91一区二区三区 | av中文在线播放 | 国产成人a亚洲精品 | 中国一级特黄视频 | 国产美女免费视频 | 久久久免费观看视频 | 精品欧美黑人一区二区三区 | 日韩一区二区三区在线视频 | 久久久久久www | 亚洲www啪成人一区二区 | 国产一区二区免费 | 欧美久久国产 | 成人av播放 | 日韩精品一区二区三区中文在线 | 亚洲一区二区网站 | www.夜夜骑.com| 国产精品91久久久久久 | 国产一区在线视频 | 国产中文 | 神马久久av | 日韩av成人在线 | 日韩精品一区二区三区 | 天天操综合网 | 欧美日韩国产在线观看 | 在线日韩福利 | 亚洲欧美日韩在线不卡 | 91 在线|