From 0da289e16f60b05766dc70f4d63d0aee9674598d Mon Sep 17 00:00:00 2001 From: Gres Date: Fri, 20 Mar 2020 21:44:46 +0300 Subject: [PATCH] Update translation scripts --- translators/bing.js | 69 ++++++++++++------------ translators/deepl.js | 99 ++++++++++++++-------------------- translators/google.js | 24 ++++----- translators/google_api.js | 49 +++++++++-------- translators/papago.js | 110 ++++++++++++++++++-------------------- translators/yandex.js | 27 +++++----- updates.json | 12 ++--- 7 files changed, 187 insertions(+), 203 deletions(-) diff --git a/translators/bing.js b/translators/bing.js index 7b16a8b..6e714a9 100644 --- a/translators/bing.js +++ b/translators/bing.js @@ -1,41 +1,42 @@ -var isPageLoaded = false; -var isTranslationFinished = false; // async translation request -var isScheduled = false; +var lastText = ''; +var active = window.location.href !== "about:blank"; -function checkFinished () { - if (!isPageLoaded || !isTranslationFinished || isScheduled) return; - isScheduled = true; - setTimeout(function () { - var text = document.querySelector ('#tta_output_ta').value; - console.log (text); - st_wtp.translated (text); - isTranslationFinished = isScheduled = false; - }, 2000); // wait for gui fill +function checkFinished() { + if (!active) return; + + let area = document.querySelector('#tta_output_ta') + if (!area) + return; + + let text = area.value.trim(); + 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) { - isTranslationFinished = true; - if (isPageLoaded) { - checkFinished (); - } - } -} -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) + active = true; + + if (window.location.href.indexOf('bing.com/translator') !== -1 + && window.location.href.indexOf('&to=' + to + '&') !== -1) { + document.querySelector('textarea#tta_input_ta').value = text; + document.querySelector('textarea#tta_input_ta').dispatchEvent( + new Event("input", { bubbles: true, cancelable: true })); return; } - isPageLoaded = true; - if (isTranslationFinished) { - checkFinished (); - } -} -window.onload = onPageLoad(); + let url = 'https://www.bing.com/translator/?from=auto&to=' + to + '&text=' + text; + console.log("setting url", url); + window.location = encodeURI(url); -function translate (){ - var url = 'https://bing.com/translator/?from=auto&to=' + st_wtp.resultLanguage + - '&text=' + st_wtp.sourceText; - window.location = encodeURI (url); +} + +function init() { + proxy.translate.connect(translate); + setInterval(checkFinished, 300); } diff --git a/translators/deepl.js b/translators/deepl.js index cf31e84..41da8cd 100644 --- a/translators/deepl.js +++ b/translators/deepl.js @@ -1,71 +1,54 @@ -var isPageLoaded = false; -var isTranslationFinished = false; // async translation request -var isScheduled = false; +var lastText = ''; +var active = window.location.href !== "about:blank"; function checkFinished() { - if (!isPageLoaded || !isTranslationFinished || isScheduled) return; - isScheduled = true; - setTimeout(function () { - var area = document.querySelector('.lmt__target_textarea'); - var text = area ? area.value : ''; - console.log('result text', text); - st_wtp.translated(text); - isTranslationFinished = isScheduled = false; - }, 2000); // wait for gui fill + if (!active) return; + + let area = document.querySelector('textarea[dl-test=translator-target-input]'); + let text = area ? area.value : ''; + + if (text === lastText || text === '') + return; + + console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length); + lastText = text; + active = false; + proxy.setTranslated(text); } -var timeout = null; -function onResourceLoad(url) { - if (url.indexOf('www2.deepl.com/jsonrpc') > -1) { - if (timeout !== null) { - 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 translate(text, from, to) { + console.log('start translate', text, from, to) + from = from == 'zh-CN' ? 'zh' : from; + to = to == 'zh-CN' ? 'zh' : to; -function onPageLoad() { - console.log('page loaded'); - isPageLoaded = true; - if (isTranslationFinished) { - checkFinished(); + 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; } -} -window.onload = onPageLoad(); - -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(''); + if (supported.indexOf(to) == -1) { + proxy.setFailed('Target language not supported'); return; } - if (window.location.href.indexOf('www.deepl.com/translator') === -1) { - var url = 'https://www.deepl.com/translator#' + - langs[st_wtp.sourceLanguage] + '/' + st_wtp.resultLanguage + '/' + - st_wtp.sourceText.replace("\n", " "); - window.location = encodeURI(url); + active = true; + + let langs = from + '/' + to + '/'; + 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; } - var input = document.querySelector('.lmt__source_textarea'); - input.value = st_wtp.sourceText.replace("\n", " "); - input.dispatchEvent(new Event('change')); + let url = 'https://www.deepl.com/translator#' + langs + + text.replace('\n', ' ').replace('|', ''); + console.log("setting url", url); + window.location = encodeURI(url); +} + +function init() { + proxy.translate.connect(translate); + setInterval(checkFinished, 300); } diff --git a/translators/google.js b/translators/google.js index 1749acd..9b0f318 100644 --- a/translators/google.js +++ b/translators/google.js @@ -1,41 +1,41 @@ var lastText = ''; var active = window.location.href !== "about:blank"; -function checkFinished () { +function checkFinished() { if (!active) return; - let spans = [].slice.call (document.querySelectorAll ('span.translation > span, #result_box > span')); - let text = spans.reduce (function (res, i) { + let spans = [].slice.call(document.querySelectorAll('span.translation > span, #result_box > span')); + let text = spans.reduce(function (res, i) { return res + ' ' + i.innerText; }, ''); if (text === lastText || text === '') 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; 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) active = true; if (window.location.href.indexOf('//translate.google') !== -1 - && window.location.href.indexOf('&tl='+to+'&') !== -1) { - document.querySelector('textarea#source').value=text; + && window.location.href.indexOf('&tl=' + to + '&') !== -1) { + document.querySelector('textarea#source').value = text; 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; console.log("setting url", url); - window.location = encodeURI (url); + window.location = encodeURI(url); } function init() { - proxy.translate.connect (translate); + proxy.translate.connect(translate); setInterval(checkFinished, 300); } - diff --git a/translators/google_api.js b/translators/google_api.js index cc81d89..34d8fc8 100644 --- a/translators/google_api.js +++ b/translators/google_api.js @@ -1,27 +1,30 @@ -function httpGetAsync(theUrl, callback) -{ - console.log(theUrl); - var xmlHttp = new XMLHttpRequest(); - xmlHttp.onreadystatechange = function() { - if (xmlHttp.readyState == 4 && xmlHttp.status == 200) - callback(xmlHttp.responseText); - } - xmlHttp.open("GET", theUrl, true); - xmlHttp.send(null); +function httpGetAsync(url, callback) { + let xmlHttp = new XMLHttpRequest(); + xmlHttp.onreadystatechange = function () { + if (xmlHttp.readyState == 4 && xmlHttp.status == 200) + callback(xmlHttp.responseText); + } + xmlHttp.open("GET", url, true); + xmlHttp.send(null); } -function translate (){ - var url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=' - + st_wtp.resultLanguage + '&dt=t&q=' + st_wtp.sourceText; - - httpGetAsync(url, function(responce) { - console.log(responce); - var object = JSON.parse(responce); - var result = ''; - object[0].forEach(function(element) { - result += element[0] + ' '; - }); - console.log(object); - st_wtp.translated (result); +function translate(text, from, to) { + console.log('start translate', text, from, to) + + let url = 'https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=' + to + '&dt=t&q=' + text; + console.log("loading url", url); + + httpGetAsync(url, function (response) { + console.log('received', response); + let object = JSON.parse(response); + let result = ''; + object[0].forEach(function (element) { + result += element[0] + ' '; }); + proxy.setTranslated(result); + }); +} + +function init() { + proxy.translate.connect(translate); } diff --git a/translators/papago.js b/translators/papago.js index 125add3..cf0ca9c 100644 --- a/translators/papago.js +++ b/translators/papago.js @@ -1,69 +1,65 @@ -var isPageLoaded = false; -var isTranslationFinished = false; // async translation request -var isScheduled = false; +var lastText = ''; +var active = window.location.href !== "about:blank"; + +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() { - if (!isPageLoaded || !isTranslationFinished || isScheduled) return; - isScheduled = true; - setTimeout(function () { - var spans = [].slice.call (document.querySelectorAll ('#txtTarget span')); - var text = spans.reduce (function (res, i) { - return res + i.innerText + ' '; - }, ''); - console.log('result text', text); - st_wtp.translated(text); - isTranslationFinished = isScheduled = false; - }, 2000); // wait for gui fill + if (!active) return; + + let text = getText() + if (text === lastText || text === lastText + '...' || text === '') + return; + + active = false; + // maybe translation will be updated + setTimeout(function() { + text = getText(); + console.log('translated text', text, 'old', lastText, 'size', text.length, lastText.length); + lastText = text; + active = false; + proxy.setTranslated(text); + }, 1000); + } -var timeout = null; -function onResourceLoad(url) { - console.log(url); - if (url.indexOf('apis/n2mt/translate') > -1) { - 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 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']; -function onPageLoad() { - if (window.location.href.indexOf('about:blank') === 0) { - translate (); + if (supported.indexOf(from) == -1) { + proxy.setFailed('Source language not supported'); return; } - isPageLoaded = true; - if (isTranslationFinished) { - 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); + if (supported.indexOf(to) == -1) { + proxy.setFailed('Target language not supported'); return; } - if (window.location.href.indexOf('https://papago.naver.com/') === -1) { - var url = 'https://papago.naver.com/?sk=auto&tk='+st_wtp.resultLanguage+'&st=' + - st_wtp.sourceText.replace("\n", " "); - window.location = encodeURI(url); - } - else { - window.location = 'about:blank'; + lastText = getText(); // because it can be updated after previous translation + active = true; + 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); +} + +function init() { + proxy.translate.connect(translate); + setInterval(checkFinished, 300); } diff --git a/translators/yandex.js b/translators/yandex.js index 3a3f915..d9f249b 100644 --- a/translators/yandex.js +++ b/translators/yandex.js @@ -1,42 +1,43 @@ var lastText = ''; var active = window.location.href !== "about:blank"; -function checkFinished () { +function checkFinished() { if (!active) return; - var spans = [].slice.call (document.querySelectorAll ('span.translation-chunk')); - let text = spans.reduce (function (res, i) { + let spans = [].slice.call(document.querySelectorAll('span.translation-chunk')); + let text = spans.reduce(function (res, i) { return res + ' ' + i.innerText; }, ''); if (text === lastText || text === '') 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; 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) active = true; - var langs = 'lang=' + from + '-' + to; + let langs = 'lang=' + from + '-' + to; if (window.location.href.indexOf('//translate.yandex') !== -1 - && window.location.href.indexOf(langs) !== -1) { - document.querySelector('textarea#textarea').value=text + && window.location.href.indexOf(langs) !== -1) { + document.querySelector('textarea#textarea').value = text document.querySelector('div#textbox').dispatchEvent( - new Event("input", {bubbles: true, cancelable: true})); + new Event("input", { bubbles: true, cancelable: true })); return; } - var url = 'https://translate.yandex.ru/?' + langs + '&text=' + text; - url = url.replace(new RegExp(' ','g') , '%20') + let url = 'https://translate.yandex.ru/?' + langs + '&text=' + text; + url = url.replace(new RegExp(' ', 'g'), '%20') + console.log("setting url", url); window.location = url; } function init() { - proxy.translate.connect (translate); + proxy.translate.connect(translate); setInterval(checkFinished, 300); } diff --git a/updates.json b/updates.json index dc8caf7..9c5db6f 100644 --- a/updates.json +++ b/updates.json @@ -384,22 +384,22 @@ ,"translators":{ "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":[ - {"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":[ - {"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":[ - {"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":[ - {"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":[ - {"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"} ]} } }