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.onload = onPageLoad();
function translate (){
var url = 'https://bing.com/translator/?from=auto&to=' + st_wtp.resultLanguage +
'&text=' + st_wtp.sourceText;
window.location = encodeURI(url); window.location = encodeURI(url);
}
function init() {
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.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;
}
let url = 'https://www.deepl.com/translator#' + langs +
text.replace('\n', ' ').replace('|', '');
console.log("setting url", url);
window.location = encodeURI(url); window.location = encodeURI(url);
return;
} }
var input = document.querySelector('.lmt__source_textarea'); function init() {
input.value = st_wtp.sourceText.replace("\n", " "); proxy.translate.connect(translate);
input.dispatchEvent(new Event('change')); setInterval(checkFinished, 300);
} }

View File

@ -12,6 +12,7 @@ function checkFinished () {
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;
@ -27,7 +28,7 @@ function translate (text, from, to){
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);
@ -38,4 +39,3 @@ 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);
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () { xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText); callback(xmlHttp.responseText);
} }
xmlHttp.open("GET", theUrl, true); xmlHttp.open("GET", url, true);
xmlHttp.send(null); 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) {
console.log('received', response);
let object = JSON.parse(response);
let result = '';
object[0].forEach(function (element) { object[0].forEach(function (element) {
result += element[0] + ' '; result += element[0] + ' ';
}); });
console.log(object); proxy.setTranslated(result);
st_wtp.translated (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 checkFinished() { function getText() {
if (!isPageLoaded || !isTranslationFinished || isScheduled) return; let spans = [].slice.call(document.querySelectorAll('#txtTarget span'));
isScheduled = true; let text = spans.reduce(function (res, i) {
setTimeout(function () {
var spans = [].slice.call (document.querySelectorAll ('#txtTarget span'));
var text = spans.reduce (function (res, i) {
return res + i.innerText + ' '; return res + i.innerText + ' ';
}, ''); }, '');
console.log('result text', text);
st_wtp.translated(text); return text.trim()
isTranslationFinished = isScheduled = false;
}, 2000); // wait for gui fill
} }
var timeout = null; function checkFinished() {
function onResourceLoad(url) { if (!active) return;
console.log(url);
if (url.indexOf('apis/n2mt/translate') > -1) { let text = getText()
if (timeout !== null) { if (text === lastText || text === lastText + '...' || text === '')
console.log('clear resource timeout'); return;
clearTimeout(timeout);
} active = false;
timeout = setTimeout(function () { // maybe translation will be updated
console.log('last resource loaded'); setTimeout(function() {
isTranslationFinished = true; text = getText();
if (isPageLoaded) { console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length);
checkFinished(); lastText = text;
} active = false;
proxy.setTranslated(text);
}, 1000); }, 1000);
}
}
st_wtp.resourceLoaded.connect(onResourceLoad);
function onPageLoad() { }
if (window.location.href.indexOf('about:blank') === 0) {
translate (); function translate(text, from, to) {
console.log('start translate', text, from, to)
let supported = ['ko', 'ru', 'en', 'fr', 'pt', 'th', 'ja',
'zh-CN', 'zh-TW', 'de', 'it', 'id', 'es', 'vi', 'hi'];
if (supported.indexOf(from) == -1) {
proxy.setFailed('Source language not supported');
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 + '&';
if (window.location.href.indexOf('//papago.naver.com/') !== -1
&& window.location.href.indexOf(langs) !== -1) {
document.querySelector('textarea#txtSource').value = text
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); window.location = encodeURI(url);
} }
else {
window.location = 'about:blank'; function init() {
} proxy.translate.connect(translate);
setInterval(checkFinished, 300);
} }

View File

@ -4,7 +4,7 @@ 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;
}, ''); }, '');
@ -22,7 +22,7 @@ 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
@ -31,8 +31,9 @@ function translate (text, from, to){
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;
} }

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"}
]} ]}
} }
} }