Added papago translation script

This commit is contained in:
Gres 2019-09-17 23:07:49 +03:00
parent d44c8ef060
commit fa89460466
2 changed files with 74 additions and 0 deletions

69
translators/papago.js Normal file
View File

@ -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';
}
}

View File

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