ScreenTranslator/translators/google_api.js

31 lines
845 B
JavaScript
Raw Normal View History

2020-03-21 01:44:46 +07:00
function httpGetAsync(url, callback) {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
2018-12-25 00:26:47 +07:00
}
2020-03-21 01:44:46 +07:00
function translate(text, from, to) {
console.log('start translate', text, from, to)
let url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=' + to + '&dt=t&q=' + text;
console.log("loading url", url);
httpGetAsync(url, function (response) {
console.log('received', response);
let object = JSON.parse(response);
let result = '';
object[0].forEach(function (element) {
result += element[0] + ' ';
2018-12-25 00:26:47 +07:00
});
2020-03-21 01:44:46 +07:00
proxy.setTranslated(result);
});
}
function init() {
proxy.translate.connect(translate);
2018-12-25 00:26:47 +07:00
}