2020-02-21 00:45:53 +07:00
|
|
|
var lastText = '';
|
|
|
|
var active = window.location.href !== "about:blank";
|
2015-10-10 03:33:49 +07:00
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
function checkFinished() {
|
2020-02-21 00:45:53 +07:00
|
|
|
if (!active) return;
|
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
let spans = [].slice.call(document.querySelectorAll('span.translation-chunk'));
|
|
|
|
let text = spans.reduce(function (res, i) {
|
2020-02-21 00:45:53 +07:00
|
|
|
return res + ' ' + i.innerText;
|
|
|
|
}, '');
|
|
|
|
|
|
|
|
if (text === lastText || text === '')
|
|
|
|
return;
|
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length);
|
2020-02-21 00:45:53 +07:00
|
|
|
lastText = text;
|
|
|
|
active = false;
|
2020-03-21 01:44:46 +07:00
|
|
|
proxy.setTranslated(text);
|
2015-10-10 03:33:49 +07:00
|
|
|
}
|
2020-02-21 00:45:53 +07:00
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
function translate(text, from, to) {
|
2020-02-21 00:45:53 +07:00
|
|
|
console.log('start translate', text, from, to)
|
|
|
|
active = true;
|
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
let langs = 'lang=' + from + '-' + to;
|
2020-02-21 00:45:53 +07:00
|
|
|
if (window.location.href.indexOf('//translate.yandex') !== -1
|
2020-03-21 01:44:46 +07:00
|
|
|
&& window.location.href.indexOf(langs) !== -1) {
|
|
|
|
document.querySelector('textarea#textarea').value = text
|
2020-02-21 00:45:53 +07:00
|
|
|
document.querySelector('div#textbox').dispatchEvent(
|
2020-03-21 01:44:46 +07:00
|
|
|
new Event("input", { bubbles: true, cancelable: true }));
|
2020-02-21 00:45:53 +07:00
|
|
|
return;
|
2017-05-27 16:33:43 +07:00
|
|
|
}
|
2015-10-10 03:33:49 +07:00
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
let url = 'https://translate.yandex.ru/?' + langs + '&text=' + text;
|
|
|
|
url = url.replace(new RegExp(' ', 'g'), '%20')
|
|
|
|
console.log("setting url", url);
|
2020-02-21 00:45:53 +07:00
|
|
|
window.location = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
2020-03-21 01:44:46 +07:00
|
|
|
proxy.translate.connect(translate);
|
2020-02-21 00:45:53 +07:00
|
|
|
setInterval(checkFinished, 300);
|
2015-10-10 03:33:49 +07:00
|
|
|
}
|