ScreenTranslator/translators/bing.js

35 lines
1.1 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 () {
2017-05-27 16:33:43 +07:00
var spans = [].slice.call (document.querySelectorAll ('#destText span'));
2015-10-10 03:33:49 +07:00
var text = spans.reduce (function (res, i) {
return res + i.innerText;
}, '');
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 ('/translator/api/Dictionary/Lookup?') > -1) {
2015-10-10 03:33:49 +07:00
isTranslationFinished = true;
checkFinished ();
}
}
st_wtp.resourceLoaded.connect (onResourceLoad);
function onPageLoad () {
isPageLoaded = true;
checkFinished ();
}
window.onload = onPageLoad();
function translate (){
2017-05-27 16:33:43 +07:00
var url = 'https://bing.com/translator/?from=auto&to=' + st_wtp.resultLanguage +
'&text=' + st_wtp.sourceText;
2015-10-10 03:33:49 +07:00
window.location = encodeURI (url);
}