ScreenTranslator/translators/bing.js

34 lines
1.0 KiB
JavaScript
Raw Normal View History

2015-10-10 03:33:49 +07:00
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 ('#OutputText span'));
var text = spans.reduce (function (res, i) {
return res + i.innerText;
}, '');
st_wtp.translated (text);
2015-11-07 01:13:18 +07:00
}, 2000); // wait for gui fill
2015-10-10 03:33:49 +07:00
}
function onResourceLoad (url) {
if (url.indexOf ('/api.microsofttranslator.com/') > -1) {
isTranslationFinished = true;
checkFinished ();
}
}
st_wtp.resourceLoaded.connect (onResourceLoad);
function onPageLoad () {
isPageLoaded = true;
checkFinished ();
}
window.onload = onPageLoad();
function translate (){
var url = 'https://bing.com/translator/?text=' + st_wtp.sourceText + '#auto/' +
st_wtp.resultLanguage;
window.location = encodeURI (url);
}