diff --git a/package.json b/package.json index eb0c976..5a79e25 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ "url": "https://github.com/k3rn31p4nic/google-translate-api/issues" }, "dependencies": { - "got": "^9.2.2", - "tunnel": "0.0.6" + "got": "^9.2.2" }, "devDependencies": { "eslint": "^5.6.1" diff --git a/src/index.js b/src/index.js index 3d8db0d..f706215 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,6 @@ const languages = require('./languages'); const tokenGenerator = require('./tokenGenerator'); const querystring = require('querystring'); const got = require('got'); -const tunnel = require('tunnel'); /** * @function translate @@ -41,7 +40,7 @@ async function translate(text, options) { let token = await tokenGenerator.generate(text); // URL & query string required by Google Translate. - let url = 'https://translate.google.com/translate_a/single'; + let baseUrl = 'https://translate.google.com/translate_a/single'; let data = { client: 'gtx', sl: options.from, @@ -59,19 +58,20 @@ async function translate(text, options) { }; // Append query string to the request URL. - url = `${url}?${querystring.stringify(data)}`; + let url = `${baseUrl}?${querystring.stringify(data)}`; let requestOptions; // If request URL is greater than 2048 characters, use POST method. if (url.length > 2048) { delete data.q; requestOptions = [ - `${url}?${querystring.stringify(data)}`, + `${baseUrl}?${querystring.stringify(data)}`, { method: 'POST', - body: JSON.stringify({ + form: true, + body: { q: text - }) + } } ]; } @@ -79,16 +79,6 @@ async function translate(text, options) { requestOptions = [ url ]; } - let proxies = []; - if (Array.isArray(options.proxies)) { - proxies = options.proxies; - - requestOptions[1] = { - agent: tunnel.httpOverHttp({ - proxy: proxies[Math.floor(Math.random() * proxies.length)] - }) - }; - } // Request translation from Google Translate. let response = await got(...requestOptions); diff --git a/src/tokenGenerator.js b/src/tokenGenerator.js index 5f11f1f..16c41dd 100644 --- a/src/tokenGenerator.js +++ b/src/tokenGenerator.js @@ -76,22 +76,16 @@ function updateTKK() { else { let res = await got('https://translate.google.com'); - const code = res.body.match(/TKK=(.*?)\(\)\)'\);/g); + const code = res.body.match(/tkk:'\d+.\d+'/g); + // code will extract something like tkk:'1232135.131231321312', we need only value - if (code) { - eval(code[0]); - /* eslint-disable no-undef */ - if (typeof TKK !== 'undefined') { - window.TKK = TKK; - config.set('TKK', TKK); - } - /* eslint-enable no-undef */ - } + if (code.length > 0) { + // extracting value tkk:'1232135.131231321312', this will extract only token: 1232135.131231321312 + const xt = code[0].split(':')[1].replace(/'/g, ''); - /** - * Note: If the regex or the eval fail, there is no need to worry. The - * server will accept relatively old seeds. - */ + window.TKK = xt; + config.set('TKK', xt); + } resolve(); }