ScreenTranslator/translators/google.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-10-08 22:36:02 +07:00
var isPageLoaded = false;
var isTranslationFinished = false; // async translation request
2015-10-10 03:33:49 +07:00
var isScheduled = false;
2015-10-08 22:36:02 +07:00
function checkFinished () {
2015-10-10 03:33:49 +07:00
if (!isPageLoaded || !isTranslationFinished || isScheduled) return;
isScheduled = true;
2015-10-08 22:36:02 +07:00
setTimeout(function () {
var spans = [].slice.call (document.querySelectorAll ('#result_box > span'));
var text = spans.reduce (function (res, i) {
return res + ' ' + i.innerText;
}, '');
console.log (text);
2015-10-08 22:36:02 +07:00
st_wtp.translated (text);
2015-10-10 03:33:49 +07:00
isTranslationFinished = isScheduled = false;
2015-11-07 01:13:18 +07:00
}, 2000); // wait for gui fill
2015-10-08 22:36:02 +07:00
}
function onResourceLoad (url) {
if (url.indexOf ('/translate_a/single') > -1) {
isTranslationFinished = true;
2017-07-22 00:24:53 +07:00
if (isPageLoaded) {
checkFinished ();
}
2015-10-08 22:36:02 +07:00
}
}
st_wtp.resourceLoaded.connect (onResourceLoad);
function onPageLoad () {
2017-07-22 00:24:53 +07:00
if (window.location.href.indexOf('about:blank') === 0) {
translate ();
return;
}
2015-10-08 22:36:02 +07:00
isPageLoaded = true;
2017-07-22 00:24:53 +07:00
if (isTranslationFinished) {
checkFinished ();
}
2015-10-08 22:36:02 +07:00
}
window.onload = onPageLoad();
function translate (){
2017-07-22 00:24:53 +07:00
if (window.location.href.indexOf('https://translate.google') === 0) {
window.location = 'about:blank';
return;
}
2015-10-08 22:36:02 +07:00
var url = 'https://translate.google.com/#auto/' +
st_wtp.resultLanguage + '/' + st_wtp.sourceText;
2015-10-10 03:33:49 +07:00
window.location = encodeURI (url);
2015-10-08 22:36:02 +07:00
}