ScreenTranslator/translators/deepl.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

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
2023-02-07 03:40:07 +07:00
let area = document.querySelector('div#target-dummydiv');
let text = area ? area.innerHTML.trim() : '';
2023-05-20 03:30:44 +07:00
if (area == null) {
area = document.querySelector('d-textarea.lmt__target_textarea p');
text = area ? area.innerText.trim() : '';
}
2023-09-03 01:09:27 +07:00
if (area == null) {
area = document.querySelector('d-textarea[data-testid=translator-target-input] p');
text = area ? area.innerText.trim() : '';
}
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) {
2023-09-03 01:09:27 +07:00
proxy.setTranslated('');
return;
2021-01-05 21:03:11 +07:00
}
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
2022-10-08 01:09:43 +07:00
let supported = ['ru', 'en', 'de', 'fr', 'es', 'pt', 'it', 'nl', 'pl', 'ja', 'zh',
2023-09-03 01:09:27 +07:00
'uk', 'bg', 'hu', 'el', 'da', 'id', 'lt', 'pt', 'ro', 'sk', 'sk', 'tr', 'fi', 'cs',
'sv', 'et']
2020-03-21 01:44:46 +07:00
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;
2023-02-12 18:17:17 +07:00
var singleLineText = text.replace(/(?:\r\n|\r|\n)/g, ' ');
2020-03-21 01:44:46 +07:00
let langs = from + '/' + to + '/';
if (window.location.href.indexOf('www.deepl.com/translator') !== -1
&& window.location.href.indexOf(langs) !== -1) {
2023-09-03 01:09:27 +07:00
var input = document.querySelector('d-textarea[dl-test=translator-source-input] p');
if (input == null)
input = document.querySelector('d-textarea.lmt__source_textarea p');
if (input == null)
input = document.querySelector('d-textarea[data-testid=translator-source-input] p');
2023-02-12 18:17:17 +07:00
if (input.innerText == singleLineText) {
2023-09-03 01:09:27 +07:00
console.log('using cached result');
lastText = '';
return;
2021-01-05 21:03:11 +07:00
}
2023-02-12 18:17:17 +07:00
input.innerText = singleLineText;
2023-05-20 03:30:44 +07:00
if (areaCopy = document.querySelector('div#source-dummydiv'))
areaCopy.innerHTML = singleLineText;
2023-09-03 01:09:27 +07:00
setTimeout(function () {
2023-02-07 03:40:07 +07:00
input.dispatchEvent(new Event("input", { bubbles: true, cancelable: true }));
}, 300);
2019-02-10 20:43:08 +07:00
return;
}
2023-02-12 18:17:17 +07:00
let url = 'https://www.deepl.com/translator#' + langs + encodeURIComponent(singleLineText);
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
}