2020-03-21 01:44:46 +07:00
|
|
|
var lastText = '';
|
|
|
|
var active = window.location.href !== "about:blank";
|
2019-02-10 20:43:08 +07:00
|
|
|
|
|
|
|
function checkFinished() {
|
2020-03-21 01:44:46 +07:00
|
|
|
if (!active) return;
|
2019-02-10 20:43:08 +07:00
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
let area = document.querySelector('textarea[dl-test=translator-target-input]');
|
|
|
|
let text = area ? area.value : '';
|
2019-02-10 20:43:08 +07:00
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
if (text === lastText || text === '')
|
|
|
|
return;
|
|
|
|
|
|
|
|
console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length);
|
|
|
|
lastText = text;
|
|
|
|
active = false;
|
|
|
|
proxy.setTranslated(text);
|
2019-02-10 20:43:08 +07:00
|
|
|
}
|
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
function translate(text, from, to) {
|
|
|
|
console.log('start translate', text, from, to)
|
2021-01-05 21:03:11 +07:00
|
|
|
|
|
|
|
if (text.trim().length == 0) {
|
|
|
|
proxy.setTranslated('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
from = from == 'zh-CN' ? 'zh' : from;
|
|
|
|
to = to == 'zh-CN' ? 'zh' : to;
|
2019-02-10 20:43:08 +07:00
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
let supported = ['ru', 'en', 'de', 'fr', 'es', 'pt', 'it', 'nl', 'pl', 'ja', 'zh']
|
|
|
|
if (supported.indexOf(from) == -1) {
|
|
|
|
proxy.setFailed('Source language not supported');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (supported.indexOf(to) == -1) {
|
|
|
|
proxy.setFailed('Target language not supported');
|
2019-02-10 20:43:08 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-21 01:44:46 +07:00
|
|
|
active = true;
|
|
|
|
|
|
|
|
let langs = from + '/' + to + '/';
|
|
|
|
if (window.location.href.indexOf('www.deepl.com/translator') !== -1
|
|
|
|
&& window.location.href.indexOf(langs) !== -1) {
|
2021-01-05 21:03:11 +07:00
|
|
|
var input = document.querySelector('textarea[dl-test=translator-source-input]');
|
|
|
|
if (input.value == text) {
|
|
|
|
console.log('using cached result');
|
|
|
|
lastText = '';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
input.value = text;
|
|
|
|
input.dispatchEvent(new Event("input", { bubbles: true, cancelable: true }));
|
2019-02-10 20:43:08 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-03 22:38:59 +07:00
|
|
|
let url = 'https://www.deepl.com/translator#' + langs + encodeURIComponent(text);
|
2020-03-21 01:44:46 +07:00
|
|
|
console.log("setting url", url);
|
2020-04-03 22:38:59 +07:00
|
|
|
window.location = url;
|
2020-03-21 01:44:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
proxy.translate.connect(translate);
|
|
|
|
setInterval(checkFinished, 300);
|
2019-02-10 20:43:08 +07:00
|
|
|
}
|