-
-
Save 10sr/805548 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
let targetLang = "ja"; // target lang to translate into | |
let alternativeLang = "en"; // if given word is in targetLang, use this instead as a target lang | |
function translate(word, target, next) { | |
next("", "", " getting..."); | |
const base = "https://www.googleapis.com/language/translate/v2?key=%s&q=%s&target=%s"; | |
const apikey = "AIzaSyBq48p8NhFgaJ1DfUJ5ltbwLxeXpjEL86A"; | |
let ep = util.format(base, apikey, encodeURIComponent(word), target); | |
util.httpGet(ep, false, function (res) { | |
if (res.status === 200) { | |
let json = decodeJSON(res.responseText); | |
let srclang = json.data.translations[0].detectedSourceLanguage; | |
if (target == srclang) { | |
lookupword(word, alternativeLang); | |
} else { | |
let result = json.data.translations[0].translatedText; | |
next(srclang, target, result); | |
} | |
} else { | |
next("", "", "ERROR!"); | |
} | |
}); | |
}; | |
function echo(srclang, from, tglang, to){ | |
display.echoStatusBar(srclang + " : " + from + " -> " + tglang + " : " + to); | |
}; | |
function decodeJSON(json) { | |
return util.safeEval("(" + json + ")"); | |
}; | |
function lookupword(word, target){ | |
translate(word, target, function (src, tg, translated) { | |
echo(src, word, tg, translated); | |
}); | |
}; | |
function read (aInitialInput) { | |
let prevText = ""; | |
prompt.reader({ | |
message : "word or sentence to translate:", | |
initialinput : aInitialInput, | |
onChange: function (arg) { | |
let word = arg.textbox.value; | |
if (word !== prevText) { | |
prevText = word; | |
lookupword(word, targetLang); | |
} | |
}, | |
callback: function (s){}, | |
}); | |
}; | |
ext.add("google-itranslate",function(){read(content.document.getSelection() || "");},"google itranslate"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment