ScreenTranslator/translators/google.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-02-21 00:45:53 +07:00
var lastText = '';
var active = window.location.href !== "about:blank";
2015-10-08 22:36:02 +07:00
function checkFinished () {
2020-02-21 00:45:53 +07:00
if (!active) return;
let spans = [].slice.call (document.querySelectorAll ('span.translation > span, #result_box > span'));
let text = spans.reduce (function (res, i) {
return res + ' ' + i.innerText;
}, '');
if (text === lastText || text === '')
2017-07-22 00:24:53 +07:00
return;
2020-02-21 00:45:53 +07:00
console.log ('translated text', text, 'old', lastText, 'size', text.length, lastText.length);
lastText = text;
active = false;
proxy.setTranslated (text);
2015-10-08 22:36:02 +07:00
}
2020-02-21 00:45:53 +07:00
function translate (text, from, to){
console.log('start translate', text, from, to)
active = true;
if (window.location.href.indexOf('//translate.google') !== -1
&& window.location.href.indexOf('&tl='+to+'&') !== -1) {
document.querySelector('textarea#source').value=text;
2017-07-22 00:24:53 +07:00
return;
}
2020-02-21 00:45:53 +07:00
// var url = 'https://translate.google.com/#auto/' + to + '/' + text;
let url = 'https://translate.google.com/#view=home&op=translate&sl=auto&tl=' + to + '&text=' + text;
console.log("setting url", url);
2015-10-10 03:33:49 +07:00
window.location = encodeURI (url);
2020-02-21 00:45:53 +07:00
2015-10-08 22:36:02 +07:00
}
2020-02-21 00:45:53 +07:00
function init() {
proxy.translate.connect (translate);
setInterval(checkFinished, 300);
}