ScreenTranslator/translators/bing.js
2018-10-17 19:33:35 +03:00

45 lines
1.3 KiB
JavaScript

var isPageLoaded = false;
var isTranslationFinished = false; // async translation request
var isScheduled = false;
function checkFinished () {
if (!isPageLoaded || !isTranslationFinished || isScheduled) return;
isScheduled = true;
setTimeout(function () {
var spans = [].slice.call (document.querySelectorAll ('#t_txtoutblk textarea'));
var text = spans.reduce (function (res, i) {
return res + i.value;
}, '');
console.log (text);
st_wtp.translated (text);
isTranslationFinished = isScheduled = false;
}, 2000); // wait for gui fill
}
function onResourceLoad (url) {
if (url.indexOf ('bing.com/translator/?') > -1) {
isTranslationFinished = true;
if (isPageLoaded) {
checkFinished ();
}
}
}
st_wtp.resourceLoaded.connect (onResourceLoad);
function onPageLoad () {
if (window.location.href.indexOf('about:blank') === 0) {
translate ();
return;
}
isPageLoaded = true;
if (isTranslationFinished) {
checkFinished ();
}
}
window.onload = onPageLoad();
function translate (){
var url = 'https://bing.com/translator/?from=auto&to=' + st_wtp.resultLanguage +
'&text=' + st_wtp.sourceText;
window.location = encodeURI (url);
}