ScreenTranslator/translators/bing.js

45 lines
1.3 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 () {
2018-10-17 23:33:35 +07:00
var spans = [].slice.call (document.querySelectorAll ('#t_txtoutblk textarea'));
2015-10-10 03:33:49 +07:00
var text = spans.reduce (function (res, i) {
2018-10-17 23:33:35 +07:00
return res + i.value;
2015-10-10 03:33:49 +07:00
}, '');
console.log (text);
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) {
2018-10-17 23:33:35 +07:00
if (url.indexOf ('bing.com/translator/?') > -1) {
2015-10-10 03:33:49 +07:00
isTranslationFinished = true;
2018-10-17 23:33:35 +07:00
if (isPageLoaded) {
checkFinished ();
}
2015-10-10 03:33:49 +07:00
}
}
st_wtp.resourceLoaded.connect (onResourceLoad);
function onPageLoad () {
2018-10-17 23:33:35 +07:00
if (window.location.href.indexOf('about:blank') === 0) {
translate ();
return;
}
2015-10-10 03:33:49 +07:00
isPageLoaded = true;
2018-10-17 23:33:35 +07:00
if (isTranslationFinished) {
checkFinished ();
}
2015-10-10 03:33:49 +07:00
}
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);
}