Added papago translation script
This commit is contained in:
parent
d44c8ef060
commit
fa89460466
69
translators/papago.js
Normal file
69
translators/papago.js
Normal 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';
|
||||
}
|
||||
}
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user