Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4021734
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:23:19+00:00 2026-05-20T10:23:19+00:00

I’m currently developing a Greasemonkey script to translate <textarea> fields in an Intranet app,

  • 0

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 :

Request entity too large

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

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.

Here’s the code :

//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+"\nError : "+responseDetails.statusText);
        }
    }
});
}

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:23:19+00:00Added an answer on May 20, 2026 at 10:23 am

    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+"\nError : "
                          + responseDetails.statusText
                       );
                    }
                 }
    } );
    

    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+"\nError : "
                             + responseDetails.statusText
                          );
                       }
                    }
       } );
    } ) (i);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.