From 75a04beffa6b84735f2fb6a87ed73a004340f848 Mon Sep 17 00:00:00 2001 From: Gres Date: Tue, 5 Jan 2021 17:03:11 +0300 Subject: [PATCH] Handle empty and same text translation --- translators/baidu.js | 17 ++++++++++++++--- translators/bing.js | 17 ++++++++++++++--- translators/deepl.js | 17 ++++++++++++++--- translators/google.js | 17 ++++++++++++++--- translators/papago.js | 17 ++++++++++++++--- translators/yandex.js | 11 +++++++++++ updates.json | 13 +++++++------ 7 files changed, 88 insertions(+), 21 deletions(-) diff --git a/translators/baidu.js b/translators/baidu.js index 6996c0c..f230688 100644 --- a/translators/baidu.js +++ b/translators/baidu.js @@ -20,14 +20,25 @@ function checkFinished() { function translate(text, from, to) { console.log('start translate', text, from, to) + + if (text.trim().length == 0) { + proxy.setTranslated(''); + return; + } + active = true; let langs = from + '/' + to; if (window.location.href.indexOf('//fanyi.baidu.com/') !== -1 && window.location.href.indexOf(langs) !== -1) { - document.querySelector('textarea#baidu_translate_input').value = text; - document.querySelector('textarea#baidu_translate_input').dispatchEvent( - new Event("input", { bubbles: true, cancelable: true })); + var input = document.querySelector('textarea#baidu_translate_input'); + if (input.value == text) { + console.log('using cached result'); + lastText = ''; + return; + } + input.value = text; + input.dispatchEvent(new Event("input", { bubbles: true, cancelable: true })); return; } diff --git a/translators/bing.js b/translators/bing.js index 1eb4117..494a5cb 100644 --- a/translators/bing.js +++ b/translators/bing.js @@ -20,13 +20,24 @@ function checkFinished() { function translate(text, from, to) { console.log('start translate', text, from, to) + + if (text.trim().length == 0) { + proxy.setTranslated(''); + return; + } + 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 })); + var input = document.querySelector('textarea#tta_input_ta'); + if (input.value == text) { + console.log('using cached result'); + lastText = ''; + return; + } + input.value = text; + input.dispatchEvent(new Event("input", { bubbles: true, cancelable: true })); return; } diff --git a/translators/deepl.js b/translators/deepl.js index a72d83b..04c3e81 100644 --- a/translators/deepl.js +++ b/translators/deepl.js @@ -18,6 +18,12 @@ function checkFinished() { function translate(text, from, to) { console.log('start translate', text, from, to) + + if (text.trim().length == 0) { + proxy.setTranslated(''); + return; + } + from = from == 'zh-CN' ? 'zh' : from; to = to == 'zh-CN' ? 'zh' : to; @@ -36,9 +42,14 @@ function translate(text, from, to) { 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 })); + 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 })); return; } diff --git a/translators/google.js b/translators/google.js index cb203cc..d256263 100644 --- a/translators/google.js +++ b/translators/google.js @@ -21,13 +21,24 @@ function checkFinished() { function translate(text, from, to) { console.log('start translate', text, from, to) + + if (text.trim().length == 0) { + proxy.setTranslated(''); + return; + } + active = true; if (window.location.href.indexOf('//translate.google') !== -1 && window.location.href.indexOf('&tl=' + to + '&') !== -1) { - document.querySelector('textarea.er8xn').value = text; - document.querySelector('textarea.er8xn').dispatchEvent( - new Event("input", { bubbles: true, cancelable: true })); + var input = document.querySelector('textarea.er8xn'); + if (input.value == text) { + console.log('using cached result'); + lastText = ''; + return; + } + input.value = text; + input.dispatchEvent(new Event("input", { bubbles: true, cancelable: true })); return; } let url = 'https://translate.google.com/#view=home&op=translate&sl=auto&tl=' + to + '&text=' + encodeURIComponent(text); diff --git a/translators/papago.js b/translators/papago.js index 2f9f7ce..d05de88 100644 --- a/translators/papago.js +++ b/translators/papago.js @@ -31,6 +31,12 @@ function checkFinished() { function translate(text, from, to) { console.log('start translate', text, from, to) + + if (text.trim().length == 0) { + proxy.setTranslated(''); + return; + } + let supported = ['ko', 'ru', 'en', 'fr', 'pt', 'th', 'ja', 'zh-CN', 'zh-TW', 'de', 'it', 'id', 'es', 'vi', 'hi']; @@ -48,9 +54,14 @@ function translate(text, from, to) { 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 })); + var input = document.querySelector('textarea#txtSource'); + if (input.value == text) { + console.log('using cached result'); + lastText = ''; + return; + } + input.value = text; + input.dispatchEvent(new Event("input", { bubbles: true, cancelable: true })); return; } diff --git a/translators/yandex.js b/translators/yandex.js index c8b891e..25131c2 100644 --- a/translators/yandex.js +++ b/translators/yandex.js @@ -20,10 +20,21 @@ function checkFinished() { function translate(text, from, to) { console.log('start translate', text, from, to) + + if (text.trim().length == 0) { + proxy.setTranslated(''); + return; + } + active = true; let langs = 'lang=' + from + '-' + to; let url = 'https://translate.yandex.ru/?' + langs + '&text=' + encodeURIComponent(text); + if (window.location.href == url) { + console.log('using cached result'); + lastText = ''; + return; + } console.log("setting url", url); window.location = url; } diff --git a/updates.json b/updates.json index 87524a4..469dce5 100644 --- a/updates.json +++ b/updates.json @@ -576,26 +576,27 @@ ,"translators":{ "baidu": {"files":[ - {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/baidu.js", "path":"$translators$/baidu.js", "md5":"93f7bca9b792877350f54a1d47767583", "size":1306} + {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/baidu.js", "path":"$translators$/baidu.js", "md5":"be3eb6d11fa5faebb046c887c9a8f3bd", "size":1501} ]} ,"bing": {"files":[ - {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/bing.js", "path":"$translators$/bing.js", "md5":"5c20fe78c25a4f9e97160fdc3bc4572c", "size":1277} + {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/bing.js", "path":"$translators$/bing.js", "md5":"a982e9aa6cac598f4c9bf4a56386d13e", "size":1481} ]} ,"deepl": {"files":[ - {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/deepl.js", "path":"$translators$/deepl.js", "md5":"a6dfae3f63ca3fa9c7edbfaff87600e4", "size":1596} + {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/deepl.js", "path":"$translators$/deepl.js", "md5":"fff5dba9840208cbb98fc85079014b0d", "size":1754} ]} ,"google": {"files":[ - {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/google.js", "path":"$translators$/google.js", "md5":"74f1064ba8f02c9c7be3821a4bdc557b", "size":1297} + {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/google.js", "path":"$translators$/google.js", "md5":"1ceba6431d757fc2ae028a4ec177542c", "size":1508} ]} ,"google_api": {"files":[ {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/google_api.js", "path":"$translators$/google_api.js", "md5":"90b9b1a5c8dc52fd4a3f28be93442a56", "size":1030} ]} ,"papago": {"files":[ - {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/papago.js", "path":"$translators$/papago.js", "md5":"538bb7280192b18d15d24b07adae2638", "size":1956} + {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/papago.js", "path":"$translators$/papago.js", "md5":"603a56fc23990453942064ec53d1eaa3", "size":2164} ]} ,"yandex": {"files":[ - {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/yandex.js", "path":"$translators$/yandex.js", "md5":"f8c625e8f6ced4a9f5be6791a6ee3f87", "size":957} + {"url":"https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/yandex.js", "path":"$translators$/yandex.js", "md5":"6bc71c24270ca418b193f073e6155d5f", "size":1177} ]} } + }