From fa894604666baaa5426057015cc181be93a62785 Mon Sep 17 00:00:00 2001 From: Gres Date: Tue, 17 Sep 2019 23:07:49 +0300 Subject: [PATCH] Added papago translation script --- translators/papago.js | 69 +++++++++++++++++++++++++++++++++++++++++++ version.json | 5 ++++ 2 files changed, 74 insertions(+) create mode 100644 translators/papago.js diff --git a/translators/papago.js b/translators/papago.js new file mode 100644 index 0000000..125add3 --- /dev/null +++ b/translators/papago.js @@ -0,0 +1,69 @@ +var isPageLoaded = false; +var isTranslationFinished = false; // async translation request +var isScheduled = false; + +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 +} + +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 onPageLoad() { + if (window.location.href.indexOf('about:blank') === 0) { + translate (); + 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); + 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'; + } +} diff --git a/version.json b/version.json index 8dc487c..f3bf204 100644 --- a/version.json +++ b/version.json @@ -36,5 +36,10 @@ "version": 1, "url": "https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/deepl.js", "path": "translators/deepl.js" + }, + "Papago translator": { + "version": 1, + "url": "https://raw.githubusercontent.com/OneMoreGres/ScreenTranslator/master/translators/papago.js", + "path": "translators/papago.js" } }