Update translation scripts

This commit is contained in:
Gres 2020-03-20 21:44:46 +03:00
parent 2bf5515b0b
commit 0da289e16f
7 changed files with 187 additions and 203 deletions

View File

@ -1,41 +1,42 @@
var isPageLoaded = false; var lastText = '';
var isTranslationFinished = false; // async translation request var active = window.location.href !== "about:blank";
var isScheduled = false;
function checkFinished () { function checkFinished() {
if (!isPageLoaded || !isTranslationFinished || isScheduled) return; if (!active) return;
isScheduled = true;
setTimeout(function () { let area = document.querySelector('#tta_output_ta')
var text = document.querySelector ('#tta_output_ta').value; if (!area)
console.log (text); return;
st_wtp.translated (text);
isTranslationFinished = isScheduled = false; let text = area.value.trim();
}, 2000); // wait for gui fill if (text === lastText || text === lastText + ' ...' || text === '' || text === '...')
return;
console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length);
lastText = text;
active = false;
proxy.setTranslated(text);
} }
function onResourceLoad (url) {
if (url.indexOf ('bing.com/translator/?') > -1) { function translate(text, from, to) {
isTranslationFinished = true; console.log('start translate', text, from, to)
if (isPageLoaded) { active = true;
checkFinished ();
} if (window.location.href.indexOf('bing.com/translator') !== -1
} && window.location.href.indexOf('&to=' + to + '&') !== -1) {
} document.querySelector('textarea#tta_input_ta').value = text;
st_wtp.resourceLoaded.connect (onResourceLoad); document.querySelector('textarea#tta_input_ta').dispatchEvent(
function onPageLoad () { new Event("input", { bubbles: true, cancelable: true }));
if (window.location.href.indexOf('about:blank') === 0) {
translate ();
return; return;
} }
isPageLoaded = true; let url = 'https://www.bing.com/translator/?from=auto&to=' + to + '&text=' + text;
if (isTranslationFinished) { console.log("setting url", url);
checkFinished (); window.location = encodeURI(url);
}
}
window.onload = onPageLoad();
function translate (){ }
var url = 'https://bing.com/translator/?from=auto&to=' + st_wtp.resultLanguage +
'&text=' + st_wtp.sourceText; function init() {
window.location = encodeURI (url); proxy.translate.connect(translate);
setInterval(checkFinished, 300);
} }

View File

@ -1,71 +1,54 @@
var isPageLoaded = false; var lastText = '';
var isTranslationFinished = false; // async translation request var active = window.location.href !== "about:blank";
var isScheduled = false;
function checkFinished() { function checkFinished() {
if (!isPageLoaded || !isTranslationFinished || isScheduled) return; if (!active) return;
isScheduled = true;
setTimeout(function () { let area = document.querySelector('textarea[dl-test=translator-target-input]');
var area = document.querySelector('.lmt__target_textarea'); let text = area ? area.value : '';
var text = area ? area.value : '';
console.log('result text', text); if (text === lastText || text === '')
st_wtp.translated(text); return;
isTranslationFinished = isScheduled = false;
}, 2000); // wait for gui fill console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length);
lastText = text;
active = false;
proxy.setTranslated(text);
} }
var timeout = null; function translate(text, from, to) {
function onResourceLoad(url) { console.log('start translate', text, from, to)
if (url.indexOf('www2.deepl.com/jsonrpc') > -1) { from = from == 'zh-CN' ? 'zh' : from;
if (timeout !== null) { to = to == 'zh-CN' ? 'zh' : to;
console.log('clear resource timeout');
clearTimeout(timeout);
}
timeout = setTimeout(function () {
console.log('last resource loaded');
isTranslationFinished = true;
if (isPageLoaded) {
checkFinished();
}
}, 500);
}
}
st_wtp.resourceLoaded.connect(onResourceLoad);
function onPageLoad() { let supported = ['ru', 'en', 'de', 'fr', 'es', 'pt', 'it', 'nl', 'pl', 'ja', 'zh']
console.log('page loaded'); if (supported.indexOf(from) == -1) {
isPageLoaded = true; proxy.setFailed('Source language not supported');
if (isTranslationFinished) { return;
checkFinished();
} }
} if (supported.indexOf(to) == -1) {
window.onload = onPageLoad(); proxy.setFailed('Target language not supported');
function translate() {
var langs = {
'eng': 'en',
"rus": 'ru',
"deu": 'de',
"spa": 'es',
"por": 'pt',
"ita": 'it',
"pol": 'pl'
}
if (langs[st_wtp.sourceLanguage] == undefined) {
st_wtp.translated('');
return; return;
} }
if (window.location.href.indexOf('www.deepl.com/translator') === -1) { active = true;
var url = 'https://www.deepl.com/translator#' +
langs[st_wtp.sourceLanguage] + '/' + st_wtp.resultLanguage + '/' + let langs = from + '/' + to + '/';
st_wtp.sourceText.replace("\n", " "); if (window.location.href.indexOf('www.deepl.com/translator') !== -1
window.location = encodeURI(url); && window.location.href.indexOf(langs) !== -1) {
document.querySelector('textarea[dl-test=translator-source-input]').value = text;
document.querySelector('textarea[dl-test=translator-source-input]').dispatchEvent(
new Event("input", { bubbles: true, cancelable: true }));
return; return;
} }
var input = document.querySelector('.lmt__source_textarea'); let url = 'https://www.deepl.com/translator#' + langs +
input.value = st_wtp.sourceText.replace("\n", " "); text.replace('\n', ' ').replace('|', '');
input.dispatchEvent(new Event('change')); console.log("setting url", url);
window.location = encodeURI(url);
}
function init() {
proxy.translate.connect(translate);
setInterval(checkFinished, 300);
} }

View File

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

View File

@ -1,27 +1,30 @@
function httpGetAsync(theUrl, callback) function httpGetAsync(url, callback) {
{ let xmlHttp = new XMLHttpRequest();
console.log(theUrl); xmlHttp.onreadystatechange = function () {
var xmlHttp = new XMLHttpRequest(); if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
xmlHttp.onreadystatechange = function() { callback(xmlHttp.responseText);
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) }
callback(xmlHttp.responseText); xmlHttp.open("GET", url, true);
} xmlHttp.send(null);
xmlHttp.open("GET", theUrl, true);
xmlHttp.send(null);
} }
function translate (){ function translate(text, from, to) {
var url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=' console.log('start translate', text, from, to)
+ st_wtp.resultLanguage + '&dt=t&q=' + st_wtp.sourceText;
httpGetAsync(url, function(responce) { let url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=' + to + '&dt=t&q=' + text;
console.log(responce); console.log("loading url", url);
var object = JSON.parse(responce);
var result = ''; httpGetAsync(url, function (response) {
object[0].forEach(function(element) { console.log('received', response);
result += element[0] + ' '; let object = JSON.parse(response);
}); let result = '';
console.log(object); object[0].forEach(function (element) {
st_wtp.translated (result); result += element[0] + ' ';
}); });
proxy.setTranslated(result);
});
}
function init() {
proxy.translate.connect(translate);
} }

View File

@ -1,69 +1,65 @@
var isPageLoaded = false; var lastText = '';
var isTranslationFinished = false; // async translation request var active = window.location.href !== "about:blank";
var isScheduled = false;
function getText() {
let spans = [].slice.call(document.querySelectorAll('#txtTarget span'));
let text = spans.reduce(function (res, i) {
return res + i.innerText + ' ';
}, '');
return text.trim()
}
function checkFinished() { function checkFinished() {
if (!isPageLoaded || !isTranslationFinished || isScheduled) return; if (!active) return;
isScheduled = true;
setTimeout(function () { let text = getText()
var spans = [].slice.call (document.querySelectorAll ('#txtTarget span')); if (text === lastText || text === lastText + '...' || text === '')
var text = spans.reduce (function (res, i) { return;
return res + i.innerText + ' ';
}, ''); active = false;
console.log('result text', text); // maybe translation will be updated
st_wtp.translated(text); setTimeout(function() {
isTranslationFinished = isScheduled = false; text = getText();
}, 2000); // wait for gui fill console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length);
lastText = text;
active = false;
proxy.setTranslated(text);
}, 1000);
} }
var timeout = null; function translate(text, from, to) {
function onResourceLoad(url) { console.log('start translate', text, from, to)
console.log(url); let supported = ['ko', 'ru', 'en', 'fr', 'pt', 'th', 'ja',
if (url.indexOf('apis/n2mt/translate') > -1) { 'zh-CN', 'zh-TW', 'de', 'it', 'id', 'es', 'vi', 'hi'];
if (timeout !== null) {
console.log('clear resource timeout');
clearTimeout(timeout);
}
timeout = setTimeout(function () {
console.log('last resource loaded');
isTranslationFinished = true;
if (isPageLoaded) {
checkFinished();
}
}, 1000);
}
}
st_wtp.resourceLoaded.connect(onResourceLoad);
function onPageLoad() { if (supported.indexOf(from) == -1) {
if (window.location.href.indexOf('about:blank') === 0) { proxy.setFailed('Source language not supported');
translate ();
return; return;
} }
isPageLoaded = true; if (supported.indexOf(to) == -1) {
if (isTranslationFinished) { proxy.setFailed('Target language not supported');
checkFinished();
}
}
window.onload = onPageLoad();
function translate() {
console.log(st_wtp.resultLanguage)
var langs = ['ko', 'ru', 'en', 'fr', 'pt', 'th', 'ja',
'zb-CN', 'zn-TW', 'de', 'it', 'id', 'es', 'vi', 'hi'];
if (langs.indexOf(st_wtp.resultLanguage) === -1) {
st_wtp.translated('');
console.log('language not supported by this translator ', st_wtp.resultLanguage);
return; return;
} }
if (window.location.href.indexOf('https://papago.naver.com/') === -1) { lastText = getText(); // because it can be updated after previous translation
var url = 'https://papago.naver.com/?sk=auto&tk='+st_wtp.resultLanguage+'&st=' + active = true;
st_wtp.sourceText.replace("\n", " "); let langs = '?sk=auto&tk=' + to + '&';
window.location = encodeURI(url); if (window.location.href.indexOf('//papago.naver.com/') !== -1
} && window.location.href.indexOf(langs) !== -1) {
else { document.querySelector('textarea#txtSource').value = text
window.location = 'about:blank'; document.querySelector('textarea#txtSource').dispatchEvent(
new Event("input", { bubbles: true, cancelable: true }));
return;
} }
let url = 'https://papago.naver.com/?sk=auto&tk=' + to + '&st=' +
text.replace("\n", " ");
window.location = encodeURI(url);
}
function init() {
proxy.translate.connect(translate);
setInterval(checkFinished, 300);
} }

View File

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

View File

@ -384,22 +384,22 @@
,"translators":{ ,"translators":{
"bing": {"files":[ "bing": {"files":[
{"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/bing.js", "path":"$translators$/bing.js", "md5":"23178a0a19f54c8dc6befd18c67decf7"} {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/bing.js", "path":"$translators$/bing.js", "md5":"fc69c1e05a3462a88131ee0a8422ad89"}
]} ]}
,"google": {"files":[ ,"google": {"files":[
{"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/google.js", "path":"$translators$/google.js", "md5":"232ffcfff1ab743d295e6c8a04695272"} {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/google.js", "path":"$translators$/google.js", "md5":"58128258211838ac23c84d18edc9c268"}
]} ]}
,"google_api": {"files":[ ,"google_api": {"files":[
{"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/google_api.js", "path":"$translators$/google_api.js", "md5":"7fa9fb03d4cfc3e69cd0120c51d82c95"} {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/google_api.js", "path":"$translators$/google_api.js", "md5":"087dfe395c6ee803bf0e69693a27a41a"}
]} ]}
,"yandex": {"files":[ ,"yandex": {"files":[
{"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/yandex.js", "path":"$translators$/yandex.js", "md5":"2c4d64c30fa74b875f48dc99f2745c1f"} {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/yandex.js", "path":"$translators$/yandex.js", "md5":"931306b7cf54c529638a730ad2ada0ef"}
]} ]}
,"deepl": {"files":[ ,"deepl": {"files":[
{"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/deepl.js", "path":"$translators$/deepl.js", "md5":"05ac33e02469710bc69fc20c55773c91"} {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/deepl.js", "path":"$translators$/deepl.js", "md5":"eb333c2e210dc955d523e68339e68b84"}
]} ]}
,"papago": {"files":[ ,"papago": {"files":[
{"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/papago.js", "path":"$translators$/papago.js", "md5":"c3c6bb456a48a47f50e634ee43862434"} {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/papago.js", "path":"$translators$/papago.js", "md5":"27d8031bcab27cf2f14c815c60785f0b"}
]} ]}
} }
} }