ScreenTranslator/translators/yandex.js

36 lines
1.1 KiB
JavaScript
Raw Normal View History

2015-10-10 03:33:49 +07:00
var isPageLoaded = false;
var isTranslationFinished = true; // async translation request
var isScheduled = false;
function checkFinished () {
if (!isPageLoaded || !isTranslationFinished || isScheduled) return;
isScheduled = true;
setTimeout(function () {
2017-05-27 16:33:43 +07:00
var spans = [].slice.call (document.querySelectorAll ('span.translation-chunk'));
2015-10-10 03:33:49 +07:00
var text = spans.reduce (function (res, i) {
2017-05-27 16:33:43 +07:00
return res + i.innerText + ' ';
2015-10-10 03:33:49 +07:00
}, '');
st_wtp.translated (text);
2017-05-27 16:33:43 +07:00
isTranslationFinished = isScheduled = false;
2015-11-07 01:13:18 +07:00
}, 2000); // wait for gui fill
2015-10-10 03:33:49 +07:00
}
function onResourceLoad (url) {
2017-05-27 16:33:43 +07:00
if (url.indexOf ('/tr.json/translate?') > -1) {
isTranslationFinished = true;
checkFinished ();
}
2015-10-10 03:33:49 +07:00
}
st_wtp.resourceLoaded.connect (onResourceLoad);
function onPageLoad () {
isPageLoaded = true;
checkFinished ();
}
window.onload = onPageLoad();
function translate (){
2017-06-01 00:32:18 +07:00
var url = 'https://translate.yandex.ru/?lang=' + st_wtp.sourceLanguage + '-' +
st_wtp.resultLanguage + '&text=' + st_wtp.sourceText ;
2015-10-10 03:33:49 +07:00
url = url.replace(new RegExp(' ','g') , '%20')
window.location = (url);
}